001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: XDROutputStream.java,v $
023   Revision 1.2  2004/05/05 21:36:35  markl
024   comment block updates
025
026   Revision 1.1  2004/03/16 07:31:25  markl
027   Javadoc added.
028   ----------------------------------------------------------------------------
029*/
030
031package kiwi.io;
032
033import java.io.*;
034
035/**
036 *  An output stream for encoding data in the XDR encoding format.
037 *
038 * @author Mark Lindner
039 * @since Kiwi 2.0
040 */
041
042public class XDROutputStream extends FilterOutputStream
043  implements XDRDataOutput
044  {
045  private static final int UNIT_SIZE = 4;
046  
047  /** Construct a new <code>XDROutputStream</code> that wraps the given
048   * stream.
049   *
050   * @param out The <code>OutputStream</code> to wrap.
051   */
052  
053  public XDROutputStream(OutputStream out)
054    {
055    super(out);
056    }
057
058  /**
059   */
060
061  public void writeBoolean(boolean value) throws IOException
062    {
063    writeInt(value ? 1 : 0);
064    }
065
066  /**
067   */
068
069  public void writeChar(char value) throws IOException
070    {
071    write((byte)(0xFF & value));
072    write((byte)0);
073    write((byte)0);
074    write((byte)0);
075    }
076
077  /**
078   */
079
080  public void writeShort(short value) throws IOException
081    {
082    write((byte)(0xFF & (value >> 8)));
083    write((byte)(0xFF & value));
084    write((byte)0);
085    write((byte)0);
086    }
087
088  /**
089   */
090
091  public void writeUnsignedShort(int value) throws IOException
092    {
093    // will this work??
094    write((byte)(0xFF & (value >> 8)));
095    write((byte)(0xFF & value));
096    write((byte)0);
097    write((byte)0);
098    }
099
100  /**
101   */
102
103  public void writeInt(int value) throws IOException
104    {
105    write((byte)(0xFF & (value >> 24)));
106    write((byte)(0xFF & (value >> 16)));
107    write((byte)(0xFF & (value >> 8)));
108    write((byte)(0xFF & value));
109    }
110
111  /**
112   */
113  
114  public void writeUnsignedInt(long value) throws IOException
115    {
116    write((byte)(0xFF & (value >> 24)));
117    write((byte)(0xFF & (value >> 16)));
118    write((byte)(0xFF & (value >> 8)));
119    write((byte)(0xFF & value));    
120    }
121
122  /**
123   */
124
125  public void writeLong(long value) throws IOException
126    {
127    write((byte)(0xFF & (value >> 56)));
128    write((byte)(0xFF & (value >> 48)));
129    write((byte)(0xFF & (value >> 40)));
130    write((byte)(0xFF & (value >> 32)));
131    write((byte)(0xFF & (value >> 24)));
132    write((byte)(0xFF & (value >> 16)));
133    write((byte)(0xFF & (value >> 8)));
134    write((byte)(0xFF & value));
135    }
136
137  /**
138   */
139
140  public void writeFloat(float value) throws IOException
141    {
142    writeInt(Float.floatToIntBits(value));
143    }
144
145  /**
146   */
147
148  public void writeDouble(double value) throws IOException
149    {
150    writeLong(Double.doubleToLongBits(value));
151    }
152
153  /**
154   */
155
156  public void writeString(String string) throws IOException
157    {
158    byte[] ascii = string.getBytes();
159    writeInt(ascii.length);
160    writeByteArray(ascii);
161    }
162
163  /**
164   */
165
166  public void writeBooleanArray(boolean[] array) throws IOException
167    {
168    writeBooleanArray(array, 0, array.length);
169    }
170
171  /**
172   */
173
174  public void writeBooleanArray(boolean[] array, int offset, int length)
175    throws IOException
176    {
177    writeInt(length);
178    writeBooleanVector(array, offset, length);
179    }
180
181  /**
182   */
183
184  public void writeBooleanVector(boolean[] array) throws IOException
185    {
186    writeBooleanVector(array, 0, array.length);
187    }
188
189  /**
190   */
191  
192  public void writeBooleanVector(boolean[] array, int offset, int length)
193    throws IOException
194    {
195    for(int i = 0; i < length; i++)
196      writeBoolean(array[offset++]);
197    }
198
199  /**
200   */
201
202  public void writeByteArray(byte[] array) throws IOException
203    {
204    writeByteArray(array, 0, array.length);
205    }
206
207  /**
208   */
209
210  public void writeByteArray(byte[] array, int offset, int length)
211    throws IOException
212    {
213    writeInt(length);
214    writeByteVector(array, offset, length);
215    }
216
217  /**
218   */
219
220  public void writeByteVector(byte[] array) throws IOException
221    {
222    writeByteVector(array, 0, array.length);
223    }
224
225  /**
226   */
227
228  public void writeByteVector(byte[] array, int offset, int length)
229    throws IOException
230    {
231    write(array, offset, length);
232
233    int pad = length % UNIT_SIZE;
234    if(pad != 0)
235      for(int i = (UNIT_SIZE - pad); i >= 0; i--)
236        write((byte)0);
237    }
238
239  /**
240   */
241  
242  public void writeShortArray(short[] array) throws IOException
243    {
244    writeShortArray(array, 0, array.length);
245    }
246
247  /**
248   */
249
250  public void writeShortArray(short[] array, int offset, int length)
251    throws IOException
252    {
253    writeInt(length);
254    writeShortVector(array, offset, length);
255    }
256
257  /**
258   */
259
260  public void writeShortVector(short[] array) throws IOException
261    {
262    writeShortVector(array, 0, array.length);
263    }
264
265  /**
266   */
267
268  public void writeShortVector(short[] array, int offset, int length)
269    throws IOException
270    {
271    for(int i = 0; i < length; i++)
272      writeShort(array[offset++]);    
273    }
274  
275  /**
276   */
277
278  public void writeUnsignedShortArray(int[] array) throws IOException
279    {
280    writeUnsignedShortArray(array, 0, array.length);
281    }
282
283  /**
284   */
285
286  public void writeUnsignedShortArray(int[] array, int offset, int length)
287    throws IOException
288    {
289    writeInt(length);
290    writeUnsignedShortVector(array, offset, length);
291    }
292
293  /**
294   */
295
296  public void writeUnsignedShortVector(int[] array) throws IOException
297    {
298    writeUnsignedShortVector(array, 0, array.length);
299    }
300  
301  /**
302   */
303
304  public void writeUnsignedShortVector(int[] array, int offset, int length)
305    throws IOException
306    {
307    for(int i = 0; i < length; i++)
308      writeUnsignedShort(array[offset++]);    
309    }
310  
311  /**
312   */
313
314  public void writeIntArray(int[] array) throws IOException
315    {
316    writeIntArray(array, 0, array.length);
317    }
318
319  /**
320   */
321
322  public void writeIntArray(int[] array, int offset, int length)
323    throws IOException
324    {
325    writeInt(length);
326    writeIntVector(array, offset, length);
327    }
328
329  /**
330   */
331
332  public void writeIntVector(int[] array) throws IOException
333    {
334    writeIntVector(array, 0, array.length);
335    }
336
337  /**
338   */
339
340  public void writeIntVector(int[] array, int offset, int length)
341    throws IOException
342    {
343    for(int i = 0; i < length; i++)
344      writeInt(array[offset++]);
345    }
346
347  /**
348   */
349
350  public void writeUnsignedIntArray(long[] array) throws IOException
351    {
352    writeUnsignedIntArray(array, 0, array.length);
353    }
354
355  /**
356   */
357
358  public void writeUnsignedIntArray(long[] array, int offset, int length)
359    throws IOException
360    {
361    writeInt(length);
362    writeUnsignedIntVector(array, offset, length);
363    }
364
365  /**
366   */
367
368  public void writeUnsignedIntVector(long[] array) throws IOException
369    {
370    writeUnsignedIntVector(array, 0, array.length);
371    }
372
373  /**
374   */
375
376  public void writeUnsignedIntVector(long[] array, int offset, int length)
377    throws IOException
378    {
379    for(int i = 0; i < length; i++)
380      writeUnsignedInt(array[offset++]);
381    }
382
383  /**
384   */
385
386  public void writeLongArray(long[] array) throws IOException
387    {
388    writeLongArray(array, 0, array.length);
389    }
390
391  /**
392   */
393
394  public void writeLongArray(long[] array, int offset, int length)
395    throws IOException
396    {
397    writeInt(length);
398    writeLongVector(array, offset, length);
399    }
400
401  /**
402   */
403
404  public void writeLongVector(long[] array) throws IOException
405    {
406    writeLongVector(array, 0, array.length);
407    }
408
409  /**
410   */
411
412  public void writeLongVector(long[] array, int offset, int length)
413    throws IOException
414    {
415    for(int i = 0; i < length; i++)
416      writeLong(array[offset++]);    
417    }
418
419  /**
420   */
421
422  public void writeFloatArray(float[] array) throws IOException
423    {
424    writeFloatArray(array, 0, array.length);
425    }
426
427  /**
428   */
429
430  public void writeFloatArray(float[] array, int offset, int length)
431    throws IOException
432    {
433    writeInt(length);
434    writeFloatVector(array, offset, length);
435    }
436
437  /**
438   */
439
440  public void writeFloatVector(float[] array) throws IOException
441    {
442    writeFloatVector(array, 0, array.length);
443    }
444
445  /**
446   */
447
448  public void writeFloatVector(float[] array, int offset, int length)
449    throws IOException
450    {
451    for(int i = 0; i < length; i++)
452      writeFloat(array[offset++]);
453    }
454  
455  /**
456   */
457
458  public void writeDoubleArray(double[] array) throws IOException
459    {
460    writeDoubleArray(array, 0, array.length);
461    }
462
463  /**
464   */
465  
466  public void writeDoubleArray(double[] array, int offset, int length)
467    throws IOException
468    {
469    writeInt(length);
470    writeDoubleVector(array, offset, length);
471    }
472
473  /**
474   */
475
476  public void writeDoubleVector(double[] array) throws IOException
477    {
478    writeDoubleVector(array, 0, array.length);
479    }
480
481  /**
482   */
483
484  public void writeDoubleVector(double[] array, int offset, int length)
485    throws IOException
486    {
487    for(int i = 0; i < length; i++)
488      writeDouble(array[offset++]);
489    }
490
491  }
492
493/* end of source file */