public class BufferedDataOutputStream
extends java.lang.Object
implements java.io.DataOutput
Modifier and Type | Field and Description |
---|---|
(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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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) |
volatile byte[] buf
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.private int pos
offset
and not larger than the value of count
. The next byte to be
read/write from/to the buffer will be buf[pos]
.
private int count
buf
.
It is one greater than the position of
the last byte within buf
that
can ever be read/write from/to the buffer.private int offset
private java.io.RandomAccessFile file
private long filePos
private boolean writeToFile
private boolean isChangedBuf
private boolean isResizeAllowed
writeToFile
is false.private static int DEFAULT_BUFFER_SIZE
private static int DEFAULT_BUFFER_RESIZE_INCREMENT
public BufferedDataOutputStream()
public BufferedDataOutputStream(int sz)
sz
- buffer sizepublic BufferedDataOutputStream(byte[] buf)
buf
- public BufferedDataOutputStream(byte[] buf, int offset)
buf
- offset
- public BufferedDataOutputStream(byte[] buf, int offset, int len)
buf
- offset
- len
- public BufferedDataOutputStream(java.lang.String name) throws java.io.FileNotFoundException
name
- java.io.FileNotFoundException
public BufferedDataOutputStream(java.io.File file) throws java.io.FileNotFoundException
file
- java.io.FileNotFoundException
public BufferedDataOutputStream(java.io.RandomAccessFile file)
file
- java.io.FileNotFoundException
public void setResizable(boolean isResizeAllowed)
isResizeAllowed
- definition in isResizeAllowed
public boolean isResizable()
isResizeAllowed
attribute.isResizeAllowed
attribute.public void write(int b) throws java.io.IOException
b
.
The 24 high-order bits of b
are ignored.write
in interface java.io.DataOutput
b
- the byte to be written.java.io.IOException
- if an I/O error occurs.public void write(byte[] b) throws java.io.IOException
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]
.write
in interface java.io.DataOutput
b
- the data.java.io.IOException
- if an I/O error occurs.public void write(byte[] b, int off, int len) throws java.io.IOException
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]
.write
in interface java.io.DataOutput
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.java.io.IOException
- if an I/O error occurs.public void writeBoolean(boolean v) throws java.io.IOException
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
.writeBoolean
in interface java.io.DataOutput
v
- a boolean
value to be written.java.io.IOException
- if an I/O error occurs.FilterOutputStream.out
public void writeByte(int v) throws java.io.IOException
writeByte
in interface java.io.DataOutput
java.io.IOException
public void writeShort(int v) throws java.io.IOException
writeShort
in interface java.io.DataOutput
java.io.IOException
public void writeChar(int v) throws java.io.IOException
writeChar
in interface java.io.DataOutput
java.io.IOException
public void writeInt(int v) throws java.io.IOException
writeInt
in interface java.io.DataOutput
java.io.IOException
public void writeLong(long v) throws java.io.IOException
writeLong
in interface java.io.DataOutput
java.io.IOException
public void writeFloat(float v) throws java.io.IOException
writeFloat
in interface java.io.DataOutput
java.io.IOException
public void writeDouble(double v) throws java.io.IOException
writeDouble
in interface java.io.DataOutput
java.io.IOException
public void writeBytes(java.lang.String s) throws java.io.IOException
writeBytes
in interface java.io.DataOutput
java.io.IOException
public void writeChars(java.lang.String s) throws java.io.IOException
writeChars
in interface java.io.DataOutput
java.io.IOException
public void writeUTF(java.lang.String s) throws java.io.IOException
writeUTF
in interface java.io.DataOutput
java.io.IOException
public long getPos()
public long available() throws java.io.IOException
count - pos
, which
is the number of bytes remaining to be written in the output buffer.java.io.IOException
public long length()
public byte[] getBuffer()
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.
public byte[] getByteArray()
public byte[] getByteArray(boolean adjust)
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.
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.public final void flush() throws java.io.IOException
buff
must be written in a file.
Buffer is reseted to its initial state.java.io.IOException
- If an I/O error ocurred.public void reset()
public final void close() throws java.io.IOException
java.io.IOException
- If an I/O error ocurred.public java.lang.String toString()
toString
in class java.lang.Object
public void list(java.io.PrintStream out)
out
- an output stream.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)