Package org.fusesource.hawtbuf
Class DataByteArrayOutputStream
java.lang.Object
java.io.OutputStream
org.fusesource.hawtbuf.DataByteArrayOutputStream
- All Implemented Interfaces:
Closeable
,DataOutput
,Flushable
,AutoCloseable
Optimized ByteArrayOutputStream
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]
private static final int
protected AbstractVarIntSupport
protected int
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new byte array output stream.DataByteArrayOutputStream
(byte[] buf) DataByteArrayOutputStream
(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
ensureEnoughBuffer
(int newcount) byte[]
getData()
protected void
onWrite()
This method is called after each write to the buffer.int
position()
void
position
(int offset) Set the current position for writingvoid
reset()
reset the output streamprotected void
resize
(int newcount) void
restart()
start using a fresh byte arrayvoid
restart
(int size) start using a fresh byte arrayint
size()
void
skip
(int size) toBuffer()
Get a Buffer from the streamvoid
write
(byte[] b, int off, int len) Writeslen
bytes from the specified byte array starting at offsetoff
to this byte array output stream.void
write
(int b) Writes the specified byte to this byte array output stream.void
void
writeBoolean
(boolean v) void
writeByte
(int v) void
writeBytes
(String s) void
writeChar
(int v) void
writeChars
(String s) void
writeDouble
(double v) void
writeFloat
(float v) void
writeInt
(int v) void
writeLong
(long v) void
writeShort
(int v) void
void
writeVarInt
(int value) void
writeVarLong
(long value) void
writeVarSignedInt
(int value) void
writeVarSignedLong
(long value) Methods inherited from class java.io.OutputStream
close, flush, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.io.DataOutput
write
-
Field Details
-
DEFAULT_SIZE
private static final int DEFAULT_SIZE- See Also:
-
buf
protected byte[] buf -
pos
protected int pos -
helper
-
-
Constructor Details
-
DataByteArrayOutputStream
public DataByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.- Parameters:
size
- the initial size.- Throws:
IllegalArgumentException
- if size is negative.
-
DataByteArrayOutputStream
public DataByteArrayOutputStream(byte[] buf) -
DataByteArrayOutputStream
public DataByteArrayOutputStream()Creates a new byte array output stream.
-
-
Method Details
-
restart
public void restart(int size) start using a fresh byte array- Parameters:
size
-
-
restart
public void restart()start using a fresh byte array -
toBuffer
Get a Buffer from the stream- Returns:
- the byte sequence
-
write
Writes the specified byte to this byte array output stream.- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in classOutputStream
- Parameters:
b
- the byte to be written.- Throws:
IOException
-
write
- Throws:
IOException
-
write
Writeslen
bytes from the specified byte array starting at offsetoff
to this byte array output stream.- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in classOutputStream
- Parameters:
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.- Throws:
IOException
-
getData
public byte[] getData()- Returns:
- the underlying byte[] buffer
-
reset
public void reset()reset the output stream -
position
Set the current position for writing- Parameters:
offset
-- Throws:
IOException
-
position
public int position() -
size
public int size() -
writeBoolean
- Specified by:
writeBoolean
in interfaceDataOutput
- Throws:
IOException
-
writeByte
- Specified by:
writeByte
in interfaceDataOutput
- Throws:
IOException
-
writeShort
- Specified by:
writeShort
in interfaceDataOutput
- Throws:
IOException
-
writeChar
- Specified by:
writeChar
in interfaceDataOutput
- Throws:
IOException
-
writeInt
- Specified by:
writeInt
in interfaceDataOutput
- Throws:
IOException
-
writeLong
- Specified by:
writeLong
in interfaceDataOutput
- Throws:
IOException
-
writeFloat
- Specified by:
writeFloat
in interfaceDataOutput
- Throws:
IOException
-
writeDouble
- Specified by:
writeDouble
in interfaceDataOutput
- Throws:
IOException
-
writeBytes
- Specified by:
writeBytes
in interfaceDataOutput
- Throws:
IOException
-
writeChars
- Specified by:
writeChars
in interfaceDataOutput
- Throws:
IOException
-
writeUTF
- Specified by:
writeUTF
in interfaceDataOutput
- Throws:
IOException
-
ensureEnoughBuffer
private void ensureEnoughBuffer(int newcount) -
resize
protected void resize(int newcount) -
onWrite
This method is called after each write to the buffer. This should allow subclasses to take some action based on the writes, for example flushing data to an external system based on size.- Throws:
IOException
-
skip
- Throws:
IOException
-
writeVarInt
- Throws:
IOException
-
writeVarLong
- Throws:
IOException
-
writeVarSignedInt
- Throws:
IOException
-
writeVarSignedLong
- Throws:
IOException
-