GiciStream
Class BufferedDataInputStream

java.lang.Object
  extended by GiciStream.BufferedDataInputStream
All Implemented Interfaces:
java.io.DataInput
Direct Known Subclasses:
ReadBufferedStream

public class BufferedDataInputStream
extends java.lang.Object
implements java.io.DataInput

This class provides a wrapper for abstracting from the data source, which can be a file or a buffer. The data source is choosed with the class's constructor. If the data source is a file, then, data from file are buffered for a better efficiency in reading.


Field Summary
(package private)  byte[] buf
          An array of bytes used to buffer the input/output data.
(package 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
(package private)  boolean EOFInBuffer
          Indicates, when data source is a file, if the End Of File mark is reached.
(package private)  java.io.RandomAccessFile file
           
(package private)  long filePos
          Indicates the offset of the file where data are from.
(package private)  int mode
          It is the buffer access.
(package private)  int offset
          Indicates the first available data in the buffer.
(package private)  int pos
          The index of the next byte to read/write from/to the buffer.
(package private)  boolean readFromFile
          Indicates the data source, whether data is readed from file or no.
 
Constructor Summary
BufferedDataInputStream(byte[] buf)
           
BufferedDataInputStream(byte[] buf, int offset)
           
BufferedDataInputStream(java.io.File file)
           
BufferedDataInputStream(int sz)
           
BufferedDataInputStream(java.io.RandomAccessFile file)
           
BufferedDataInputStream(java.lang.String name)
           
 
Method Summary
 long available()
          Returns the number of bytes that can be read from this buffer without blocking.
 void close()
           
 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()
           
 void seek(long pos)
           
 int skipBytes(int n)
           
 long skipBytes(long n)
           
 java.lang.String toString()
          For debugging purpose.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

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

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

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

int offset
Indicates the first available data in the buffer.


file

java.io.RandomAccessFile file

filePos

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


EOFInBuffer

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

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)


readFromFile

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


DEFAULT_BUFFER_SIZE

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

Constructor Detail

BufferedDataInputStream

public BufferedDataInputStream(int sz)
Parameters:
sz - buffer size

BufferedDataInputStream

public BufferedDataInputStream(byte[] buf)
Parameters:
buf -

BufferedDataInputStream

public BufferedDataInputStream(byte[] buf,
                               int offset)
Parameters:
buf -
offset -

BufferedDataInputStream

public BufferedDataInputStream(java.lang.String name)
                        throws java.io.FileNotFoundException
Parameters:
name -
Throws:
java.io.FileNotFoundException

BufferedDataInputStream

public BufferedDataInputStream(java.io.File file)
                        throws java.io.FileNotFoundException
Parameters:
file -
Throws:
java.io.FileNotFoundException

BufferedDataInputStream

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

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.

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

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

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Specified by:
readBoolean in interface java.io.DataInput
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.

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

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

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

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

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)

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)

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.

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

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


TER - Copyright (C) 2006
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) - UAB (Spain)