Package net.sourceforge.jiu.util
Class SeekableByteArrayOutputStream
java.lang.Object
java.io.OutputStream
net.sourceforge.jiu.util.SeekableByteArrayOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
An extension of
OutputStream that writes data to an internal
byte array, resizing it when necessary.
Similar to ByteArrayOutputStream, but also enables seeking and truncating.- Since:
- 0.10.0
- Author:
- Marco Schmidt
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]private booleanprivate intprivate intprivate int -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new object of this class, setting initial capacity and increment size to default values.SeekableByteArrayOutputStream(int initialCapacity) Creates a new object of this class, setting initial capacity to the argument value.SeekableByteArrayOutputStream(int initialCapacity, int increment) Creates a new object of this class, setting initial capacity and increment to the argument values. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this output stream.private voidensureSpace(int numBytes) intReturns the current offset in the output stream.intgetSize()Returns the current size of the output stream.private voidincreaseBuffer(int newLength) voidseek(int newOffset) Sets the current position in the output stream to the argument.byte[]Allocates a newbyte[]object, copiesgetSize()bytes from the internal byte array to that new array and returns the array.voidtruncate()Removes all bytes after the current position.voidwrite(byte[] data) Write the complete argument array to this stream.voidwrite(byte[] src, int srcOffset, int num) Write some bytes from the argument array to this stream.voidwrite(int b) Writes the least significant eight bits of the argumentintto the internal array.voidwriteTo(OutputStream out) Writes the bytes in the internal byte array to the argument output stream.Methods inherited from class java.io.OutputStream
flush, nullOutputStream
-
Field Details
-
buffer
private byte[] buffer -
closed
private boolean closed -
incrementStep
private int incrementStep -
offset
private int offset -
size
private int size
-
-
Constructor Details
-
SeekableByteArrayOutputStream
public SeekableByteArrayOutputStream()Creates a new object of this class, setting initial capacity and increment size to default values. -
SeekableByteArrayOutputStream
public SeekableByteArrayOutputStream(int initialCapacity) Creates a new object of this class, setting initial capacity to the argument value. The increment size is set to the initial capacity as well if that value is larger than 0. Otherwise it is set to a default value.- Parameters:
initialCapacity- the number of bytes that are allocated in this constructor (0 or larger)
-
SeekableByteArrayOutputStream
public SeekableByteArrayOutputStream(int initialCapacity, int increment) Creates a new object of this class, setting initial capacity and increment to the argument values.- Parameters:
initialCapacity- the number of bytes that are allocated in this constructor (0 or larger)increment- the number of bytes by which the internal byte array is increased if it is full (1 or larger)
-
-
Method Details
-
close
Closes this output stream. After a call to this method, all write attempts will result in an exception.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
ensureSpace
- Throws:
IOException
-
getPosition
public int getPosition()Returns the current offset in the output stream. Larger than or equal to 0 and smaller than or equal togetSize().- Returns:
- current position in the output stream, 0-based
-
getSize
public int getSize()Returns the current size of the output stream.- Returns:
- size of the output stream in bytes (0 or larger)
-
increaseBuffer
private void increaseBuffer(int newLength) -
seek
Sets the current position in the output stream to the argument.- Parameters:
newOffset- new offset into the file, must be >= 0 and <=getSize()- Throws:
IOException- if the argument is invalid
-
toByteArray
public byte[] toByteArray()Allocates a newbyte[]object, copiesgetSize()bytes from the internal byte array to that new array and returns the array.- Returns:
- a copy of the byte[] data stored internally
-
truncate
public void truncate()Removes all bytes after the current position. After a call to this method,getSize()is equal togetPosition(). -
write
Writes the least significant eight bits of the argumentintto the internal array.- Specified by:
writein classOutputStream- Parameters:
b- int variable that stores the byte value to be written- Throws:
IOException
-
write
Write the complete argument array to this stream. Copies the data to the internal byte array. Simply callswrite(data, 0, data.length);.- Overrides:
writein classOutputStream- Parameters:
data- array to be copied to this stream- Throws:
IOException
-
write
Write some bytes from the argument array to this stream. Copies num bytes starting at src[srcOffset] to this stream.- Overrides:
writein classOutputStream- Parameters:
src- the array from which data is copiedsrcOffset- int index into that array pointing to the first byte to be copiednum- number of bytes to be copied- Throws:
IOException
-
writeTo
Writes the bytes in the internal byte array to the argument output stream. A call to this method has the same effect asbyte[] copy = toByteArray(); out.write(copy, 0, copy.length);
However, you with this method you save the allocation of an additional byte array and the copying to that new array.- Parameters:
out- the output stream to which this stream's content is copied- Throws:
IOException- if out has a problem writing the bytes
-