nom.tam.util
Class BufferedDataOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.BufferedOutputStream
              extended by nom.tam.util.BufferedDataOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataOutput, java.io.Flushable, ArrayDataOutput

public class BufferedDataOutputStream
extends java.io.BufferedOutputStream
implements ArrayDataOutput

This class is intended for high performance I/O in scientific applications. It combines the functionality of the BufferedOutputStream and the DataOutputStream as well as more efficient handling of arrays. This minimizes the number of method calls that are required to write data. Informal tests of this method show that it can be as much as 10 times faster than using a DataOutputStream layered on a BufferedOutputStream for writing large arrays. The performance gain on scalars or small arrays will be less but there should probably never be substantial degradation of performance.

Note that there is substantial duplication of code to minimize method invocations. However simple output methods were used where empirical tests seemed to indicate that the simpler method did not cost any time. It seems likely that most of these variations will be washed out across different compilers and users who wish to tune the method for their particular system may wish to compare the the implementation of write(int[], int, int) with write(float[], int, int).

Testing and timing for this class is peformed in the nom.tam.util.test.BufferedFileTester class.


Field Summary
 
Fields inherited from class java.io.BufferedOutputStream
buf, count
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
BufferedDataOutputStream(java.io.OutputStream o)
          Use the BufferedOutputStream constructor
BufferedDataOutputStream(java.io.OutputStream o, int bufLength)
          Use the BufferedOutputStream constructor
 
Method Summary
protected  void checkBuf(int need)
           
 void write(boolean[] b)
          Write an array of booleans.
 void write(boolean[] b, int start, int len)
          Write a segment of an array of booleans.
 void write(char[] c)
          Write an array of char's.
 void write(char[] c, int start, int len)
          Write a segment of an array of char's.
 void write(double[] d)
          Write an array of doubles.
 void write(double[] d, int start, int len)
           
 void write(float[] f)
          Write an array of floats.
 void write(float[] f, int start, int len)
           
 void write(int[] i)
          Write an array of int's.
 void write(int[] i, int start, int len)
          Write a segment of an array of int's.
 void write(long[] l)
          Write an array of longs.
 void write(long[] l, int start, int len)
          Write a segement of an array of longs.
 void write(short[] s)
          Write an array of shorts.
 void write(short[] s, int start, int len)
          Write a segment of an array of shorts.
 void write(java.lang.String[] s)
          Write an array of Strings -- equivalent to calling writeBytes for each string.
 void write(java.lang.String[] s, int start, int len)
          Write a segment of an array of Strings.
 void writeArray(java.lang.Object o)
          This routine provides efficient writing of arrays of any primitive type.
 void writeBoolean(boolean b)
          Write a boolean value
 void writeByte(int b)
          Write a byte value.
 void writeBytes(java.lang.String s)
          Write a string using the local protocol to convert char's to bytes.
 void writeChar(int c)
          Write a char value.
 void writeChars(java.lang.String s)
          Write a string as an array of chars.
 void writeDouble(double d)
          Write a double value.
 void writeFloat(float f)
          Write a float value.
 void writeInt(int i)
          Write an integer value.
 void writeLong(long l)
          Write a long value.
 void writePrimitiveArray(java.lang.Object o)
          This routine provides efficient writing of arrays of any primitive type.
 void writeShort(int s)
          Write a short value.
 void writeUTF(java.lang.String s)
          Write a string as a UTF.
 
Methods inherited from class java.io.BufferedOutputStream
flush, write, write
 
Methods inherited from class java.io.FilterOutputStream
close, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface nom.tam.util.ArrayDataOutput
close, flush, write, write
 
Methods inherited from interface java.io.DataOutput
write
 

Constructor Detail

BufferedDataOutputStream

public BufferedDataOutputStream(java.io.OutputStream o)
Use the BufferedOutputStream constructor

Parameters:
o - An open output stream.

BufferedDataOutputStream

public BufferedDataOutputStream(java.io.OutputStream o,
                                int bufLength)
Use the BufferedOutputStream constructor

Parameters:
o - An open output stream.
bufLength - The buffer size.
Method Detail

writeBoolean

public void writeBoolean(boolean b)
                  throws java.io.IOException
Write a boolean value

Specified by:
writeBoolean in interface java.io.DataOutput
Parameters:
b - The value to be written. Externally true is represented as a byte of 1 and false as a byte value of 0.
Throws:
java.io.IOException

writeByte

public void writeByte(int b)
               throws java.io.IOException
Write a byte value.

Specified by:
writeByte in interface java.io.DataOutput
Throws:
java.io.IOException

writeInt

public void writeInt(int i)
              throws java.io.IOException
Write an integer value.

Specified by:
writeInt in interface java.io.DataOutput
Throws:
java.io.IOException

writeShort

public void writeShort(int s)
                throws java.io.IOException
Write a short value.

Specified by:
writeShort in interface java.io.DataOutput
Throws:
java.io.IOException

writeChar

public void writeChar(int c)
               throws java.io.IOException
Write a char value.

Specified by:
writeChar in interface java.io.DataOutput
Throws:
java.io.IOException

writeLong

public void writeLong(long l)
               throws java.io.IOException
Write a long value.

Specified by:
writeLong in interface java.io.DataOutput
Throws:
java.io.IOException

writeFloat

public void writeFloat(float f)
                throws java.io.IOException
Write a float value.

Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException

writeDouble

public void writeDouble(double d)
                 throws java.io.IOException
Write a double value.

Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException
Write a string using the local protocol to convert char's to bytes.

Specified by:
writeBytes in interface java.io.DataOutput
Parameters:
s - The string to be written.
Throws:
java.io.IOException

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Write a string as an array of chars.

Specified by:
writeChars in interface java.io.DataOutput
Throws:
java.io.IOException

writeUTF

public void writeUTF(java.lang.String s)
              throws java.io.IOException
Write a string as a UTF. Note that this class does not handle this situation efficiently since it creates new DataOutputStream to handle each call.

Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.IOException

writePrimitiveArray

public void writePrimitiveArray(java.lang.Object o)
                         throws java.io.IOException
This routine provides efficient writing of arrays of any primitive type. The String class is also handled but it is an error to invoke this method with an object that is not an array of these types. If the array is multidimensional, then it calls itself recursively to write the entire array. Strings are written using the standard 1 byte format (i.e., as in writeBytes). If the array is an array of objects, then writePrimitiveArray will be called for each element of the array.

Parameters:
o - The object to be written. It must be an array of a primitive type, Object, or String.
Throws:
java.io.IOException

writeArray

public void writeArray(java.lang.Object o)
                throws java.io.IOException
This routine provides efficient writing of arrays of any primitive type. The String class is also handled but it is an error to invoke this method with an object that is not an array of these types. If the array is multidimensional, then it calls itself recursively to write the entire array. Strings are written using the standard 1 byte format (i.e., as in writeBytes). If the array is an array of objects, then writePrimitiveArray will be called for each element of the array.

Specified by:
writeArray in interface ArrayDataOutput
Parameters:
o - The object to be written. It must be an array of a primitive type, Object, or String.
Throws:
java.io.IOException - if the argument is not of the proper type

write

public void write(boolean[] b)
           throws java.io.IOException
Write an array of booleans.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(boolean[] b,
                  int start,
                  int len)
           throws java.io.IOException
Write a segment of an array of booleans.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(short[] s)
           throws java.io.IOException
Write an array of shorts.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(short[] s,
                  int start,
                  int len)
           throws java.io.IOException
Write a segment of an array of shorts.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(char[] c)
           throws java.io.IOException
Write an array of char's.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(char[] c,
                  int start,
                  int len)
           throws java.io.IOException
Write a segment of an array of char's.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(int[] i)
           throws java.io.IOException
Write an array of int's.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(int[] i,
                  int start,
                  int len)
           throws java.io.IOException
Write a segment of an array of int's.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(long[] l)
           throws java.io.IOException
Write an array of longs.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(long[] l,
                  int start,
                  int len)
           throws java.io.IOException
Write a segement of an array of longs.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(float[] f)
           throws java.io.IOException
Write an array of floats.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(float[] f,
                  int start,
                  int len)
           throws java.io.IOException
Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(double[] d)
           throws java.io.IOException
Write an array of doubles.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(double[] d,
                  int start,
                  int len)
           throws java.io.IOException
Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(java.lang.String[] s)
           throws java.io.IOException
Write an array of Strings -- equivalent to calling writeBytes for each string.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(java.lang.String[] s,
                  int start,
                  int len)
           throws java.io.IOException
Write a segment of an array of Strings. Equivalent to calling writeBytes for the selected elements.

Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

checkBuf

protected void checkBuf(int need)
                 throws java.io.IOException
Throws:
java.io.IOException