nom.tam.util
Class BufferedFile

java.lang.Object
  extended by nom.tam.util.BufferedFile
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput, ArrayDataInput, ArrayDataOutput, RandomAccess

public class BufferedFile
extends java.lang.Object
implements ArrayDataInput, ArrayDataOutput, RandomAccess


Constructor Summary
BufferedFile(java.io.File file)
          Create a buffered file from a File descriptor
BufferedFile(java.io.File file, java.lang.String mode)
          Create a buffered file from a File descriptor
BufferedFile(java.io.File file, java.lang.String mode, int bufferSize)
          Create a buffered file from a file descriptor
BufferedFile(java.lang.String filename)
          Create a read-only buffered file
BufferedFile(java.lang.String filename, java.lang.String mode)
          Create a buffered file with the given mode.
BufferedFile(java.lang.String filename, java.lang.String mode, int bufferSize)
          Create a buffered file with the given mode and a specified buffer size.
 
Method Summary
 void close()
          Close the file
protected  void finalize()
          Clear up any pending output at cleanup.
 void flush()
          Flush output buffer if necessary.
 java.nio.channels.FileChannel getChannel()
          Get the channel associated with this file.
 java.io.FileDescriptor getFD()
          Get the file descriptor associated with this stream.
 long getFilePointer()
          Get the current offset into the file.
protected  void initialize(java.io.File file, java.lang.String mode, int bufferSize)
           
 long length()
          Get the current length of the file.
protected  int primitiveArrayRecurse(java.lang.Object o)
           
 int read()
          Read a byte
 int read(boolean[] b)
           
 int read(boolean[] b, int start, int length)
           
 int read(byte[] buf)
          Create a buffered file using a mapped /** Read an entire byte array.
 int read(byte[] buf, int offset, int len)
          Read into a segment of a byte array.
 int read(char[] c)
           
 int read(char[] c, int start, int length)
           
 int read(double[] d)
           
 int read(double[] d, int start, int length)
           
 int read(float[] f)
           
 int read(float[] f, int start, int length)
           
 int read(int[] i)
           
 int read(int[] i, int start, int length)
           
 int read(long[] l)
           
 int read(long[] l, int start, int length)
           
 int read(short[] s)
           
 int read(short[] s, int start, int length)
           
 int readArray(java.lang.Object o)
          This routine provides efficient reading of arrays of any primitive type.
 boolean readBoolean()
          Read a boolean
 byte readByte()
          Read a byte
 char readChar()
          Read a char
 double readDouble()
          Read a double.
 float readFloat()
          Read a float.
 void readFully(byte[] b)
          Read a byte array fully.
 void readFully(byte[] b, int off, int len)
          Read a byte array fully.
 int readInt()
          Read an int
 java.lang.String readLine()
          Read a line of input.
 long readLong()
          Read a long.
 short readShort()
          Read a short
 int readUnsignedByte()
          Read an unsigned byte.
 int readUnsignedShort()
          Read an unsigned short.
 java.lang.String readUTF()
          Read a string encoded as a UTF.
 void seek(long offsetFromStart)
          Move to the current offset from the beginning of the file.
 void setLength(long newLength)
          Set the length of the file.
 long skip(long offset)
          Skip from the current position.
 int skipBytes(int toSkip)
          Skip the number of bytes.
 void write(boolean[] b)
          Write an array of booleans.
 void write(boolean[] b, int start, int length)
           
 void write(byte[] buf)
           
 void write(byte[] buf, int offset, int length)
           
 void write(char[] c)
          Write an array of char's.
 void write(char[] c, int start, int length)
           
 void write(double[] d)
          Write an array of doubles.
 void write(double[] d, int start, int length)
           
 void write(float[] f)
          Write an array of floats.
 void write(float[] f, int start, int length)
           
 void write(int buf)
           
 void write(int[] i)
          Write an array of int's.
 void write(int[] i, int start, int length)
           
 void write(long[] l)
          Write an array of longs.
 void write(long[] l, int start, int length)
           
 void write(short[] s)
          Write an array of shorts.
 void write(short[] s, int start, int length)
           
 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 length)
           
 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 writeShort(int s)
          Write a short value.
 void writeUTF(java.lang.String s)
          Write a string as a UTF.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferedFile

public BufferedFile(java.lang.String filename)
             throws java.io.IOException
Create a read-only buffered file

Throws:
java.io.IOException

BufferedFile

public BufferedFile(java.lang.String filename,
                    java.lang.String mode)
             throws java.io.IOException
Create a buffered file with the given mode.

Parameters:
filename - The file to be accessed.
mode - A string composed of "r" and "w" for read and write access.
Throws:
java.io.IOException

BufferedFile

public BufferedFile(java.io.File file)
             throws java.io.IOException
Create a buffered file from a File descriptor

Throws:
java.io.IOException

BufferedFile

public BufferedFile(java.io.File file,
                    java.lang.String mode)
             throws java.io.IOException
Create a buffered file from a File descriptor

Throws:
java.io.IOException

BufferedFile

public BufferedFile(java.lang.String filename,
                    java.lang.String mode,
                    int bufferSize)
             throws java.io.IOException
Create a buffered file with the given mode and a specified buffer size.

Parameters:
filename - The file to be accessed.
mode - A string composed of "r" and "w" indicating read or write access.
buffer - The buffer size to be used. This should be substantially larger than 100 bytes and defaults to 32768 bytes in the other constructors.
Throws:
java.io.IOException

BufferedFile

public BufferedFile(java.io.File file,
                    java.lang.String mode,
                    int bufferSize)
             throws java.io.IOException
Create a buffered file from a file descriptor

Throws:
java.io.IOException
Method Detail

initialize

protected void initialize(java.io.File file,
                          java.lang.String mode,
                          int bufferSize)
                   throws java.io.IOException
Throws:
java.io.IOException

read

public int read(byte[] buf)
         throws java.io.IOException
Create a buffered file using a mapped /** Read an entire byte array. Note BufferedFile will return a partially filled array only at an end-of-file.

Specified by:
read in interface ArrayDataInput
Parameters:
buf - The array to be filled.
Throws:
java.io.IOException

read

public int read(byte[] buf,
                int offset,
                int len)
         throws java.io.IOException
Read into a segment of a byte array.

Specified by:
read in interface ArrayDataInput
Parameters:
buf - The array to be filled.
offset - The starting location for input.
length - The number of bytes to be read. Fewer bytes will be read if an EOF is reached.
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Read a byte

Throws:
java.io.IOException

skip

public long skip(long offset)
          throws java.io.IOException
Skip from the current position.

Specified by:
skip in interface ArrayDataInput
Parameters:
offset - The number of bytes from the current position. This may be negative.
Throws:
java.io.IOException

seek

public void seek(long offsetFromStart)
          throws java.io.IOException
Move to the current offset from the beginning of the file. A user may move past the end of file but this does not extend the file unless data is written there.

Specified by:
seek in interface RandomAccess
Throws:
java.io.IOException

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Read a boolean

Specified by:
readBoolean in interface java.io.DataInput
Returns:
a boolean generated from the next byte in the input.
Throws:
java.io.IOException

readByte

public byte readByte()
              throws java.io.IOException
Read a byte

Specified by:
readByte in interface java.io.DataInput
Returns:
the next byte in the input.
Throws:
java.io.IOException

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Read an unsigned byte.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the unsigned value of the next byte as an integer.
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Read an int

Specified by:
readInt in interface java.io.DataInput
Returns:
an integer read from the input.
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Read a short

Specified by:
readShort in interface java.io.DataInput
Returns:
a short read from the input.
Throws:
java.io.IOException

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Read an unsigned short.

Specified by:
readUnsignedShort in interface java.io.DataInput
Returns:
an unsigned short value as an integer.
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Read a char

Specified by:
readChar in interface java.io.DataInput
Returns:
a char read from the input.
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Read a long.

Specified by:
readLong in interface java.io.DataInput
Returns:
a long value read from the input.
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
Read a float.

Specified by:
readFloat in interface java.io.DataInput
Returns:
a float value read from the input.
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
Read a double.

Specified by:
readDouble in interface java.io.DataInput
Returns:
a double value read from the input.
Throws:
java.io.IOException

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Read a byte array fully. Since the read method of this class reads an entire buffer, the only difference with readFully is that readFully will signal an EOF if the buffer cannot be filled.

Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Read a byte array fully. Since the read method of this class reads an entire buffer, the only difference with readFully is that readFully will signal an EOF if the buffer cannot be filled.

Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

skipBytes

public int skipBytes(int toSkip)
              throws java.io.IOException
Skip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).

Specified by:
skipBytes in interface java.io.DataInput
Throws:
java.io.IOException

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Read a string encoded as a UTF.

Specified by:
readUTF in interface java.io.DataInput
Returns:
the string.
Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Read a line of input.

Specified by:
readLine in interface java.io.DataInput
Returns:
the next line.
Throws:
java.io.IOException

readArray

public int readArray(java.lang.Object o)
              throws java.io.IOException
This routine provides efficient reading of arrays of any primitive type.

Specified by:
readArray in interface ArrayDataInput
Parameters:
o - The object to be read. It must be an array of a primitive type, or an array of Object's.
Throws:
java.io.IOException

primitiveArrayRecurse

protected int primitiveArrayRecurse(java.lang.Object o)
                             throws java.io.IOException
Throws:
java.io.IOException

read

public int read(boolean[] b)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(boolean[] b,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(short[] s)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(short[] s,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(char[] c)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(char[] c,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(int[] i)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(int[] i,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(long[] l)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(long[] l,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(float[] f)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(float[] f,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(double[] d)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

read

public int read(double[] d,
                int start,
                int length)
         throws java.io.IOException
Specified by:
read in interface ArrayDataInput
Throws:
java.io.IOException

write

public void write(int buf)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

write

public void write(byte[] buf)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

write

public void write(byte[] buf,
                  int offset,
                  int length)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flush output buffer if necessary. This method is not present in RandomAccessFile but users may need to call flush to ensure that data has been written.

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

finalize

protected void finalize()
Clear up any pending output at cleanup.

Overrides:
finalize in class java.lang.Object

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.

Specified by:
writeUTF in interface java.io.DataOutput
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 write 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 length)
           throws java.io.IOException
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 length)
           throws java.io.IOException
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 length)
           throws java.io.IOException
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 length)
           throws java.io.IOException
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 length)
           throws java.io.IOException
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 length)
           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 length)
           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 length)
           throws java.io.IOException
Specified by:
write in interface ArrayDataOutput
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close the file

Specified by:
close in interface ArrayDataInput
Specified by:
close in interface ArrayDataOutput
Throws:
java.io.IOException

getFD

public java.io.FileDescriptor getFD()
                             throws java.io.IOException
Get the file descriptor associated with this stream. Note that this returns the file descriptor of the associated RandomAccessFile.

Throws:
java.io.IOException

getChannel

public java.nio.channels.FileChannel getChannel()
Get the channel associated with this file. Note that this returns the channel of the associated RandomAccessFile. Note that since the BufferedFile buffers the I/O's to the underlying file, the offset of the channel may be different than the offset of the BufferedFile. This is different than for a RandomAccessFile where the offsets are guaranteed to be the same.


length

public long length()
            throws java.io.IOException
Get the current length of the file.

Throws:
java.io.IOException

getFilePointer

public long getFilePointer()
Get the current offset into the file.

Specified by:
getFilePointer in interface RandomAccess

setLength

public void setLength(long newLength)
               throws java.io.IOException
Set the length of the file. This method calls the method of the same name in RandomAccessFile which is only available in JDK1.2 and greater. This method may be deleted for compilation with earlier versions.

Parameters:
newLength - The number of bytes at which the file is set.
Throws:
java.io.IOException