CADI.Common.io
Class BufferedDataOutputStream

java.lang.Object
  extended by CADI.Common.io.BufferedDataOutputStream
All Implemented Interfaces:
java.io.DataOutput

public class BufferedDataOutputStream
extends java.lang.Object
implements java.io.DataOutput

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

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

Field Summary
(package 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_RESIZE_INCREMENT
           
private static int DEFAULT_BUFFER_SIZE
          It is the default size for the buffer when this was not passed
private  java.io.RandomAccessFile file
           
private  long filePos
          Indicates the offset of the file where data are from.
private  boolean isChangedBuf
          Indicates if the buf has been changed since the last time it was written in the file.
private  boolean isResizeAllowed
          Indicates if the buffer size can be resized.
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 writeToFile
          Indicates the data source, whether data is readed from file or no.
 
Constructor Summary
BufferedDataOutputStream()
          Constructor.
BufferedDataOutputStream(byte[] buf)
          Constructor.
BufferedDataOutputStream(byte[] buf, int offset)
          Constructor.
BufferedDataOutputStream(byte[] buf, int offset, int len)
          Constructor.
BufferedDataOutputStream(java.io.File file)
          Constructor.
BufferedDataOutputStream(int sz)
          Constructor.
BufferedDataOutputStream(java.io.RandomAccessFile file)
          Constructor.
BufferedDataOutputStream(java.lang.String name)
          Constructor.
 
Method Summary
 long available()
          Returns the number of bytes that can be written in this buffer without blocking.
 void close()
          Closes the buffered data output stream
 void flush()
          Data stored in the buff must be written in a file.
 byte[] getBuffer()
          Returns the byte array that contains the stored data.
 byte[] getByteArray()
          Gets the buffer as a byte array.
 byte[] getByteArray(boolean adjust)
          Gets the buffer as a byte array.
 int getOffset()
          Returns the offset attribute.
 long getPos()
          Returns the position where the next byte will be wrote.
 boolean isResizable()
          Returns the isResizeAllowed attribute.
 long length()
          Returns the number of bytes that they have been written in this file, taking into account any buffering.
 void list(java.io.PrintStream out)
          Prints this Buffered Data Output Stream out to the specified output stream.
 void reset()
          Resets the buffer restoring the pointer to its initial position.
private  void resizeBuffer(int newSize)
           
 void setResizable(boolean isResizeAllowed)
          Sets the buffer resizable or not.
 java.lang.String toString()
           
 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

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.


writeToFile

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


isChangedBuf

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


isResizeAllowed

private 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


DEFAULT_BUFFER_RESIZE_INCREMENT

private static int DEFAULT_BUFFER_RESIZE_INCREMENT
Constructor Detail

BufferedDataOutputStream

public BufferedDataOutputStream()
Constructor.


BufferedDataOutputStream

public BufferedDataOutputStream(int sz)
Constructor.

Parameters:
sz - buffer size

BufferedDataOutputStream

public BufferedDataOutputStream(byte[] buf)
Constructor.

Parameters:
buf -

BufferedDataOutputStream

public BufferedDataOutputStream(byte[] buf,
                                int offset)
Constructor.

Parameters:
buf -
offset -

BufferedDataOutputStream

public BufferedDataOutputStream(byte[] buf,
                                int offset,
                                int len)
Constructor.

Parameters:
buf -
offset -
len -

BufferedDataOutputStream

public BufferedDataOutputStream(java.lang.String name)
                         throws java.io.FileNotFoundException
Constructor.

Parameters:
name -
Throws:
java.io.FileNotFoundException

BufferedDataOutputStream

public BufferedDataOutputStream(java.io.File file)
                         throws java.io.FileNotFoundException
Constructor.

Parameters:
file -
Throws:
java.io.FileNotFoundException

BufferedDataOutputStream

public BufferedDataOutputStream(java.io.RandomAccessFile file)
Constructor.

Parameters:
file -
Throws:
java.io.FileNotFoundException
Method Detail

setResizable

public void setResizable(boolean isResizeAllowed)
Sets the buffer resizable or not.

Parameters:
isResizeAllowed - definition in isResizeAllowed

isResizable

public boolean isResizable()
Returns the isResizeAllowed attribute.

Returns:
the isResizeAllowed attribute.

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.

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.

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.

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

writeByte

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

writeShort

public void writeShort(int v)
                throws java.io.IOException
Specified by:
writeShort in interface java.io.DataOutput
Throws:
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

writeInt

public void writeInt(int v)
              throws java.io.IOException
Specified by:
writeInt in interface java.io.DataOutput
Throws:
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

writeFloat

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

writeDouble

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

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

getPos

public long getPos()
Returns the position where the next byte will be wrote.

Returns:
the position where the next byte will be wrote.

available

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

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

length

public long length()
Returns the number of bytes that they have been written in this file, taking into account any buffering.

Returns:
The length of the stream, in bytes.

getBuffer

public byte[] getBuffer()
Returns the byte array that contains the stored data.

The length of the byte array is equal or greather than the amount of stored data. Its real length can be known using the length function.

Moreover, data can be started in any position of the byte array. The position of the first data, offset in the byte array, can be known using through the getOffset function.

Returns:
an one-dimensional byte array which contains the internal buffer.

getOffset

public int getOffset()
Returns the offset attribute.

Returns:
the offset attribute.

getByteArray

public byte[] getByteArray()
Gets the buffer as a byte array. The length of the returned byte array can be equal or greather than the length of stored data, then offset and length should be taken into account.

Returns:
an one-dimensional byte array which contains the internal buffer.

getByteArray

public byte[] getByteArray(boolean adjust)
Gets the buffer as a byte array.

If the adjust parameter is true the length of the returned byte array is adjusted to the number of bytes stored in the array. Data buffer is copied.

Otherwise, the byte array is retorned. In this case the offset and length must be taken into account. Buffer pointer is returned. See offset and length().

When data is stored in a file, buffered data is only retorned.

Parameters:
adjust - if true, the returned byte array is adjusted to the stored data length. Otherwise, the returned byte array can be equal or greather than the stored data length.
Returns:
an one-dimensional byte array which contains the internal buffer.

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.

reset

public void reset()
Resets the buffer restoring the pointer to its initial position.


close

public final void close()
                 throws java.io.IOException
Closes the buffered data output stream

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

list

public void list(java.io.PrintStream out)
Prints this Buffered Data Output Stream out to the specified output stream. This method is useful for debugging.

Parameters:
out - an output stream.

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)