org.entityfs.util.io
Class RandomAccessToDataInputOutputAdapter

java.lang.Object
  extended by org.entityfs.util.io.RandomAccessToDataInputOutputAdapter
All Implemented Interfaces:
DataInput, DataOutput, DataSink, DataSource, RandomAccess

public class RandomAccessToDataInputOutputAdapter
extends Object
implements RandomAccess, DataInput, DataOutput

This object adapts any RandomAccess implementation to implement the DataInput and DataOutput interfaces.

Methods defined in the DataInput and DataOutput interfaces throw IOException:s on errors. Methods defined in the RandomAccess interface throw WrappedIOException:s.

The static fromModifiedUtf8(byte[]) and toModifiedUtf(String) can be used to convert strings from and to the modified UTF-8 data format defined in the DataInput interface.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
RandomAccessToDataInputOutputAdapter(RandomAccess adapted)
           
 
Method Summary
 void addCloseObserver(RandomAccessCloseObserver obs)
          Add an observer that is notified when this RandomAccess is closed.
 int available()
          Returns an estimate of the number of bytes that can be read from this data source without blocking by the next invocation of a method for this data source.
 void close()
          Close the sink and free all resources associated with it.
protected  void finalize()
           
 void flush()
          Flush changes made to the sink to the underlying storage.
static String fromModifiedUtf8(byte[] barr)
          Interpret the supplied byte array as modified UTF-8 data as described in the DataInput interface.
 long getFilePointer()
          Get the current offset in the file.
 RandomAccessMode getMode()
          Get the mode that this RandomAccess was opened in.
 long length()
          Get the file's current length.
 int read()
          Read a byte of data from the source.
 int read(byte[] barr)
          Read up to b.length bytes of data from the source into the array.
 int read(byte[] barr, int offset, int len)
          Read up to len bytes of data from the source into the array.
 boolean readBoolean()
           
 byte readByte()
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] barr)
           
 void readFully(byte[] barr, int offset, int len)
           
 int readInt()
           
 String readLine()
           
 long readLong()
           
 short readShort()
           
 int readUnsignedByte()
           
 int readUnsignedShort()
           
 String readUTF()
           
 void seek(long pos)
          Set the file pointer offset, measured from the beginning of the file.
 void setLength(long newlen)
          Set the length of the file.
 int skipBytes(int no)
           
 long skipBytes(long n)
          Attempt to skip over n bytes in the source.
static byte[] toModifiedUtf(String s)
          Convert the supplied string to the modified UTF-8 format described in DataInput.
 void write(byte[] barr)
          Write the contents of the array to the sink, starting at the current file pointer.
 void write(byte[] barr, int offset, int len)
          Write len bytes from the array, starting at the offset off to this sink.
 void write(int b)
          Write a byte to the sink.
 void writeBoolean(boolean b)
           
 void writeByte(int v)
           
 void writeBytes(String s)
           
 void writeChar(int v)
           
 void writeChars(String s)
           
 void writeDouble(double d)
           
 void writeFloat(float f)
           
 void writeInt(int i)
           
 void writeLong(long l)
           
 void writeShort(int v)
           
 void writeUTF(String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccessToDataInputOutputAdapter

public RandomAccessToDataInputOutputAdapter(RandomAccess adapted)
Method Detail

readBoolean

public boolean readBoolean()
                    throws IOException
Specified by:
readBoolean in interface DataInput
Throws:
IOException

writeBoolean

public void writeBoolean(boolean b)
                  throws IOException
Specified by:
writeBoolean in interface DataOutput
Throws:
IOException

readByte

public byte readByte()
              throws IOException
Specified by:
readByte in interface DataInput
Throws:
IOException

readUnsignedByte

public int readUnsignedByte()
                     throws IOException
Specified by:
readUnsignedByte in interface DataInput
Throws:
IOException

writeByte

public void writeByte(int v)
               throws IOException
Specified by:
writeByte in interface DataOutput
Throws:
IOException

writeBytes

public void writeBytes(String s)
                throws IOException
Specified by:
writeBytes in interface DataOutput
Throws:
IOException

readChar

public char readChar()
              throws IOException
Specified by:
readChar in interface DataInput
Throws:
IOException

writeChar

public void writeChar(int v)
               throws IOException
Specified by:
writeChar in interface DataOutput
Throws:
IOException

writeChars

public void writeChars(String s)
                throws IOException
Specified by:
writeChars in interface DataOutput
Throws:
IOException

readDouble

public double readDouble()
                  throws IOException
Specified by:
readDouble in interface DataInput
Throws:
IOException

writeDouble

public void writeDouble(double d)
                 throws IOException
Specified by:
writeDouble in interface DataOutput
Throws:
IOException

readLong

public long readLong()
              throws IOException
Specified by:
readLong in interface DataInput
Throws:
IOException

writeLong

public void writeLong(long l)
               throws IOException
Specified by:
writeLong in interface DataOutput
Throws:
IOException

readFloat

public float readFloat()
                throws IOException
Specified by:
readFloat in interface DataInput
Throws:
IOException

writeFloat

public void writeFloat(float f)
                throws IOException
Specified by:
writeFloat in interface DataOutput
Throws:
IOException

readInt

public int readInt()
            throws IOException
Specified by:
readInt in interface DataInput
Throws:
IOException

writeInt

public void writeInt(int i)
              throws IOException
Specified by:
writeInt in interface DataOutput
Throws:
IOException

readShort

public short readShort()
                throws IOException
Specified by:
readShort in interface DataInput
Throws:
IOException

writeShort

public void writeShort(int v)
                throws IOException
Specified by:
writeShort in interface DataOutput
Throws:
IOException

readUnsignedShort

public int readUnsignedShort()
                      throws IOException
Specified by:
readUnsignedShort in interface DataInput
Throws:
IOException

readFully

public void readFully(byte[] barr)
               throws IOException
Specified by:
readFully in interface DataInput
Throws:
IOException

readFully

public void readFully(byte[] barr,
                      int offset,
                      int len)
               throws IOException
Specified by:
readFully in interface DataInput
Throws:
IOException

skipBytes

public int skipBytes(int no)
              throws IOException
Specified by:
skipBytes in interface DataInput
Parameters:
no - Number of bytes to skip. No bytes are skipped if no is negative.
Throws:
IOException

readLine

public String readLine()
Specified by:
readLine in interface DataInput

fromModifiedUtf8

public static String fromModifiedUtf8(byte[] barr)
                               throws UTFDataFormatException
Interpret the supplied byte array as modified UTF-8 data as described in the DataInput interface.

Parameters:
barr - The modified UTF-8 data.
Returns:
String representation of the modified UTF-8 data.
Throws:
UTFDataFormatException - If the byte array cannot be interpreted as a UTF-8 string.

readUTF

public String readUTF()
               throws IOException
Specified by:
readUTF in interface DataInput
Throws:
IOException

toModifiedUtf

public static byte[] toModifiedUtf(String s)
                            throws UTFDataFormatException
Convert the supplied string to the modified UTF-8 format described in DataInput.

Parameters:
s - The string to convert.
Returns:
The modified UTF-8 representation of the supplied string.
Throws:
UTFDataFormatException - If the supplied string cannot be converted to a byte array that is shorter than 65535 bytes.

writeUTF

public void writeUTF(String s)
              throws IOException
Specified by:
writeUTF in interface DataOutput
Throws:
IOException

getMode

public RandomAccessMode getMode()
Description copied from interface: RandomAccess
Get the mode that this RandomAccess was opened in.

This method does not throw an IllegalStateException if it is called after the RandomAccess has been closed.

Specified by:
getMode in interface RandomAccess
Returns:
The mode that this RandomAccess was opened in.

getFilePointer

public long getFilePointer()
                    throws WrappedIOException
Description copied from interface: RandomAccess
Get the current offset in the file.

Specified by:
getFilePointer in interface RandomAccess
Returns:
The offset from the beginning of the file.
Throws:
WrappedIOException

write

public void write(int b)
           throws WrappedIOException
Description copied from interface: DataSink
Write a byte to the sink.

Specified by:
write in interface DataOutput
Specified by:
write in interface DataSink
Parameters:
b - The byte to write.
Throws:
WrappedIOException - On I/O errors.

write

public void write(byte[] barr)
           throws WrappedIOException
Description copied from interface: DataSink
Write the contents of the array to the sink, starting at the current file pointer.

Specified by:
write in interface DataOutput
Specified by:
write in interface DataSink
Parameters:
barr - The array to write.
Throws:
WrappedIOException - On I/O errors.

write

public void write(byte[] barr,
                  int offset,
                  int len)
           throws WrappedIOException
Description copied from interface: DataSink
Write len bytes from the array, starting at the offset off to this sink.

Specified by:
write in interface DataOutput
Specified by:
write in interface DataSink
Parameters:
barr - The byte array.
offset - The starting offset in the array.
len - The number of bytes to write.
Throws:
WrappedIOException - On I/O errors.

flush

public void flush()
           throws WrappedIOException
Description copied from interface: DataSink
Flush changes made to the sink to the underlying storage. This is optional to implement, but read only sinks should always throw ReadOnlyException.

Specified by:
flush in interface DataSink
Throws:
WrappedIOException - On I/O errors.

read

public int read()
         throws WrappedIOException
Description copied from interface: DataSource
Read a byte of data from the source. The byte is returned as an integer in the range 0 to 255 (0x00-0x0ff).

Specified by:
read in interface DataSource
Returns:
The next byte of data, or -1 if the end of the source has been reached.
Throws:
WrappedIOException - On I/O errors.

read

public int read(byte[] barr)
         throws WrappedIOException
Description copied from interface: DataSource
Read up to b.length bytes of data from the source into the array.

Specified by:
read in interface DataSource
Parameters:
barr - The byte array into which data is read.
Returns:
The total number of bytes read into the array, or -1 if no data could be read because the the end of the source was reached before the read started.
Throws:
WrappedIOException - On I/O errors.

read

public int read(byte[] barr,
                int offset,
                int len)
         throws WrappedIOException
Description copied from interface: DataSource
Read up to len bytes of data from the source into the array.

Specified by:
read in interface DataSource
Parameters:
barr - The byte array into which data is read.
offset - The start offset in the array b at which data is written.
len - The maximum number of bytes to read.
Returns:
The total number of bytes read into the array, or -1 if no data could be read because the end of the source was reached before the read started.
Throws:
WrappedIOException - On I/O errors.

close

public void close()
           throws WrappedIOException
Description copied from interface: DataSink
Close the sink and free all resources associated with it. If the object is already closed, this method does nothing.

Specified by:
close in interface DataSink
Specified by:
close in interface DataSource
Throws:
WrappedIOException - On I/O errors.

addCloseObserver

public void addCloseObserver(RandomAccessCloseObserver obs)
Description copied from interface: RandomAccess
Add an observer that is notified when this RandomAccess is closed. The observers are called just after the RandomAccess has been closed.

Specified by:
addCloseObserver in interface RandomAccess
Parameters:
obs - The observer.

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

available

public int available()
              throws WrappedIOException
Description copied from interface: DataSource
Returns an estimate of the number of bytes that can be read from this data source without blocking by the next invocation of a method for this data source.

Specified by:
available in interface DataSource
Returns:
An estimate of the number of bytes that can be read without blocking.
Throws:
WrappedIOException - On I/O errors.
See Also:
InputStream.available()

length

public long length()
            throws WrappedIOException
Description copied from interface: RandomAccess
Get the file's current length.

Specified by:
length in interface RandomAccess
Returns:
The file's current length.
Throws:
WrappedIOException

setLength

public void setLength(long newlen)
               throws WrappedIOException
Description copied from interface: RandomAccess
Set the length of the file.

If the present length of the file is greater than the l argument, the file will be truncated.

If the new length is bigger than the current length of the file, the file will be extended. The contents of the extended portion of the file are not defined.

The current file pointer is not changed, unless the file pointer offset is greater than the new file length. If so, it will be set to the new length.

Specified by:
setLength in interface RandomAccess
Parameters:
newlen - The new length of the file.
Throws:
WrappedIOException

seek

public void seek(long pos)
          throws WrappedIOException
Description copied from interface: RandomAccess
Set the file pointer offset, measured from the beginning of the file. If the offset is set beyond the end of the file does not change the file length until something is written to it.

Specified by:
seek in interface RandomAccess
Parameters:
pos - The new file pointer offset, measured from the beginning of the file.
Throws:
WrappedIOException
See Also:
DataSource.skipBytes(long)

skipBytes

public long skipBytes(long n)
               throws WrappedIOException
Description copied from interface: DataSource
Attempt to skip over n bytes in the source. If n is negative, no bytes are skipped.

The pointer in the source cannot be moved beyond EOF using this method (unlike RandomAccess.seek(long)).

Specified by:
skipBytes in interface DataSource
Parameters:
n - The number of bytes to increment the source pointer with.
Returns:
The number of bytes actually skipped.
Throws:
WrappedIOException
See Also:
RandomAccess.seek(long)