CADI.Common.io
Class BufferedDataIOStream

java.lang.Object
  extended by CADI.Common.io.BufferedDataIOStream
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput

public class BufferedDataIOStream
extends java.lang.Object
implements java.io.DataInput, java.io.DataOutput

Version:
beta 0.1 2007/12/22
Author:
Group on Interactive Coding of Images (GICI)

Field Summary
private  byte[] buf
          An array of bytes used to buffer the input/output data.
private  int count
          The index one greater than the last valid byte in the buffer.
private static int DEFAULT_BUFFER_SIZE
          It is the default size for the buffer when this was not passed
private  boolean EOFInBuffer
          Indicates, when data source is a file, if the End Of File mark is reached.
private  java.io.RandomAccessFile file
           
private  long filePos
          Indicates the offset of the file where data are from.
(package private)  boolean isChangedBuf
          Indicates if the buf has been changed since the last time it was written in the file.
(package private)  boolean isResizeAllowed
          Indicates if the buffer size can be resized.
private  int mode
          It is the buffer access.
private static int MODE_R
           
private static int MODE_RW
           
private static int MODE_RWU
           
private  int offset
          Indicates the first available data in the buffer.
private  int pos
          The index of the next byte to read/write from/to the buffer.
private  boolean useFile
          Indicates the data source, whether data is readed from file or no.
 
Constructor Summary
BufferedDataIOStream(java.io.File file, java.lang.String mode)
           
BufferedDataIOStream(int sz, java.lang.String mode)
          Constructor.
BufferedDataIOStream(java.io.RandomAccessFile file)
           
BufferedDataIOStream(java.lang.String name, java.lang.String mode)
           
 
Method Summary
 long available()
          Returns the number of bytes that can be read from this buffer without blocking.
 void close()
           
 void flush()
          Data stored in the buff must be written in a file.
 int getCapacity()
           
 long getPos()
           
 long length()
          Returns the number of bytes that can be read from this buffer, taking into account any buffering.
 int read()
          Reads the next byte of data from the input stream.
private  int read1()
          Reads the next byte of data from this input stream.
 boolean readBoolean()
           
private  void readBuffer()
           
 byte readByte()
          See the general contract of the readByte method of DataInput.
 char readChar()
           
 double readDouble()
          See the general contract of the readDouble method of DataInput.
 float readFloat()
          See the general contract of the readFloat method of DataInput.
 void readFully(byte[] b)
          See the general contract of the readFully method of DataInput.
 void readFully(byte[] b, int off, int len)
          See the general contract of the readFully method of DataInput.
 int readInt()
           
 java.lang.String readLine()
           
 long readLong()
           
 short readShort()
          Reads two input bytes and returns a short value.
 int readUnsignedByte()
          Reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255.
 int readUnsignedShort()
           
 java.lang.String readUTF()
           
private  void resizeBuffer(int newSize)
           
 void seek(long pos)
           
private  void setMode(java.lang.String mode)
           
 void setResizable(boolean isResizeAllowed)
          Sets if buffer can be resized or not.
 int skipBytes(int n)
           
 long skipBytes(long n)
           
 java.lang.String toString()
          For debugging purpose.
 void write(byte[] b)
          Writes to the output stream all the bytes in array b.
 void write(byte[] b, int off, int len)
          Writes len bytes from array b, in order, to the output stream.
 void write(int b)
          Writes to the output stream the eight low-order bits of the argument b.
 void writeBoolean(boolean v)
          Writes a boolean to the underlying output stream as a 1-byte value.
 void writeByte(int v)
           
 void writeBytes(java.lang.String s)
           
 void writeChar(int v)
           
 void writeChars(java.lang.String s)
           
 void writeDouble(double v)
           
 void writeFloat(float v)
           
 void writeInt(int v)
           
 void writeLong(long v)
           
 void writeShort(int v)
           
 void writeUTF(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

private volatile byte[] buf
An array of bytes used to buffer the input/output data. Elements buf[0] through buf[count-1] are the only bytes that can ever be read/write from/to the buffer; element buf[pos] is the next byte to be read/write.


pos

private int pos
The index of the next byte to read/write from/to the buffer. This value should always be nonnegative and not shorter than offset and not larger than the value of count. The next byte to be read/write from/to the buffer will be buf[pos].


count

private int count
The index one greater than the last valid byte in the buffer. This value should always be nonnegative and not larger than the length of buf. It is one greater than the position of the last byte within buf that can ever be read/write from/to the buffer.


offset

private int offset
Indicates the first available data in the buffer.


file

private java.io.RandomAccessFile file

filePos

private long filePos
Indicates the offset of the file where data are from.


EOFInBuffer

private boolean EOFInBuffer
Indicates, when data source is a file, if the End Of File mark is reached. Then, the buf contains the last bytes of the file.


mode

private int mode
It is the buffer access. Allowed values are:   "r" read   "rw" read & write (if file/buffer exists, removes it before)   "rw+" read & write (if file/buffer exists, updates it)


MODE_R

private static int MODE_R

MODE_RW

private static int MODE_RW

MODE_RWU

private static int MODE_RWU

useFile

private boolean useFile
Indicates the data source, whether data is readed from file or no.


isChangedBuf

boolean isChangedBuf
Indicates if the buf has been changed since the last time it was written in the file.


isResizeAllowed

boolean isResizeAllowed
Indicates if the buffer size can be resized. This attribute will only be taken into account whe data are stored in the buffer, i.e., writeToFile is false.


DEFAULT_BUFFER_SIZE

private static int DEFAULT_BUFFER_SIZE
It is the default size for the buffer when this was not passed

Constructor Detail

BufferedDataIOStream

public BufferedDataIOStream(int sz,
                            java.lang.String mode)
Constructor.

Parameters:
sz - buffer size

BufferedDataIOStream

public BufferedDataIOStream(java.lang.String name,
                            java.lang.String mode)
                     throws java.io.FileNotFoundException
Parameters:
name -
Throws:
java.io.FileNotFoundException

BufferedDataIOStream

public BufferedDataIOStream(java.io.File file,
                            java.lang.String mode)
                     throws java.io.FileNotFoundException
Parameters:
file -
Throws:
java.io.FileNotFoundException

BufferedDataIOStream

public BufferedDataIOStream(java.io.RandomAccessFile file)
                     throws java.io.FileNotFoundException
Parameters:
file -
Throws:
java.io.FileNotFoundException
Method Detail

setResizable

public void setResizable(boolean isResizeAllowed)
Sets if buffer can be resized or not.

Parameters:
isResizeAllowed -

write

public void write(int b)
           throws java.io.IOException
Writes to the output stream the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Specified by:
write in interface java.io.DataOutput
Parameters:
b - the byte to be written.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read()
         throws java.io.EOFException,
                java.io.IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

A subclass must provide an implementation of this method.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException
java.io.EOFException
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b)
           throws java.io.IOException
Writes to the output stream all the bytes in array b. If b is null, a NullPointerException is thrown. If b.length is zero, then no bytes are written. Otherwise, the byte b[0] is written first, then b[1], and so on; the last byte written is b[b.length-1].

Specified by:
write in interface java.io.DataOutput
Parameters:
b - the data.
Throws:
java.io.IOException - if an I/O error occurs.

readFully

public void readFully(byte[] b)
               throws java.io.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
Throws:
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from array b, in order, to the output stream. If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are written. Otherwise, the byte b[off] is written first, then b[off+1], and so on; the last byte written is b[off+len-1].

Specified by:
write in interface java.io.DataOutput
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs.

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.EOFException,
                      java.io.IOException
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFully in interface java.io.DataInput
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
Throws:
java.io.EOFException
java.io.EOFException - if this input stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Specified by:
skipBytes in interface java.io.DataInput
Throws:
java.io.IOException

writeBoolean

public void writeBoolean(boolean v)
                  throws java.io.IOException
Writes a boolean to the underlying output stream as a 1-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0. If no exception is thrown, the counter written is incremented by 1.

Specified by:
writeBoolean in interface java.io.DataOutput
Parameters:
v - a boolean value to be written.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out

readBoolean

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

writeByte

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

readByte

public byte readByte()
              throws java.io.EOFException,
                     java.io.IOException
See the general contract of the readByte method of DataInput.

Bytes for this operation are read from the contained stream.

Specified by:
readByte in interface java.io.DataInput
Returns:
the next byte of this input stream as a signed 8-bit byte.
Throws:
java.io.IOException
java.io.IOException - if an I/O error occurs.
java.io.EOFException

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.EOFException,
                            java.io.IOException
Reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255. This method is suitable for reading the byte written by the writeByte method of interface DataOutput if the argument to writeByte was intended to be a value in the range 0 through 255.

Specified by:
readUnsignedByte in interface java.io.DataInput
Returns:
the unsigned 8-bit value read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

writeShort

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

readShort

public short readShort()
                throws java.io.EOFException,
                       java.io.IOException
Reads two input bytes and returns a short value. Let a be the first byte read and b be the second byte. The value returned is:

(short)((a << 8) | (b & 0xff))
 
This method is suitable for reading the bytes written by the writeShort method of interface DataOutput.

Specified by:
readShort in interface java.io.DataInput
Returns:
the 16-bit value read.
Throws:
java.io.EOFException - if this stream reaches the end before reading all the bytes.
java.io.IOException - if an I/O error occurs.

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.EOFException,
                             java.io.IOException
Specified by:
readUnsignedShort in interface java.io.DataInput
Throws:
java.io.EOFException
java.io.IOException

writeChar

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

readChar

public char readChar()
              throws java.io.EOFException,
                     java.io.IOException
Specified by:
readChar in interface java.io.DataInput
Throws:
java.io.EOFException
java.io.IOException

writeInt

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

readInt

public int readInt()
            throws java.io.EOFException,
                   java.io.IOException
Specified by:
readInt in interface java.io.DataInput
Throws:
java.io.EOFException
java.io.IOException

writeLong

public void writeLong(long v)
               throws java.io.IOException
Specified by:
writeLong in interface java.io.DataOutput
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.EOFException,
                     java.io.IOException
Specified by:
readLong in interface java.io.DataInput
Throws:
java.io.EOFException
java.io.IOException

writeFloat

public void writeFloat(float v)
                throws java.io.IOException
Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
See the general contract of the readFloat method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readFloat in interface java.io.DataInput
Returns:
the next four bytes of this input stream, interpreted as a float.
Throws:
java.io.EOFException - if this input stream reaches the end before reading four bytes.
java.io.IOException - if an I/O error occurs.
See Also:
DataInputStream.readInt(), Float.intBitsToFloat(int)

writeDouble

public void writeDouble(double v)
                 throws java.io.IOException
Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
See the general contract of the readDouble method of DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readDouble in interface java.io.DataInput
Returns:
the next eight bytes of this input stream, interpreted as a double.
Throws:
java.io.EOFException - if this input stream reaches the end before reading eight bytes.
java.io.IOException - if an I/O error occurs.
See Also:
DataInputStream.readLong(), Double.longBitsToDouble(long)

writeBytes

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

writeChars

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

writeUTF

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

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Specified by:
readLine in interface java.io.DataInput
Throws:
java.io.IOException

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Specified by:
readUTF in interface java.io.DataInput
Throws:
java.io.IOException

seek

public void seek(long pos)
          throws java.io.IOException
Parameters:
pos -
Throws:
java.io.IOException

getPos

public long getPos()
Returns:
a long integer that the current postion inside the file or stream

skipBytes

public long skipBytes(long n)
               throws java.io.IOException
Parameters:
n -
Returns:
a long intger that indicates the number od bits skipped
Throws:
java.io.EOFException
java.io.IOException

getCapacity

public int getCapacity()
Returns:
an integer that indicates the capacity of the buffer

available

public long available()
               throws java.io.IOException
Returns the number of bytes that can be read from this buffer without blocking. The value returned is count - pos, which is the number of bytes remaining to be read from the input buffer.

Returns:
the number of bytes that can be read from the buffer without blocking.
Throws:
java.io.IOException

length

public long length()
Returns the number of bytes that can be read from this buffer, taking into account any buffering.

Returns:
The length of the stream, in bytes, or -1 if an I/O error ocurrs.

flush

public final void flush()
                 throws java.io.IOException
Data stored in the buff must be written in a file. Buffer is reseted to its initial state.

Throws:
java.io.IOException - If an I/O error ocurred.

close

public void close()
           throws java.io.IOException
Throws:
java.io.IOException

toString

public java.lang.String toString()
For debugging purpose.

Overrides:
toString in class java.lang.Object

setMode

private void setMode(java.lang.String mode)

read1

private final int read1()
                 throws java.io.IOException,
                        java.io.EOFException
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

This read method cannot block.

Returns:
the next byte of this input stream as a int.
Throws:
java.io.EOFException - if this input stream has reached the end.
java.io.IOException - if an I/O error occurs.

readBuffer

private void readBuffer()
                 throws java.io.EOFException,
                        java.io.IOException
Throws:
java.io.EOFException
java.io.IOException

resizeBuffer

private void resizeBuffer(int newSize)


CADI - Copyright (C) 2007
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Group on Interactive Coding of Images (GICI) (contact)