Package org.fusesource.hawtbuf
Class DataByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.fusesource.hawtbuf.DataByteArrayOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.DataOutput,java.io.Flushable,java.lang.AutoCloseable
public class DataByteArrayOutputStream extends java.io.OutputStream implements java.io.DataOutputOptimized ByteArrayOutputStream
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufprivate static intDEFAULT_SIZEprotected AbstractVarIntSupporthelperprotected intpos
-
Constructor Summary
Constructors Constructor Description DataByteArrayOutputStream()Creates 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidensureEnoughBuffer(int newcount)byte[]getData()protected voidonWrite()This method is called after each write to the buffer.intposition()voidposition(int offset)Set the current position for writingvoidreset()reset the output streamprotected voidresize(int newcount)voidrestart()start using a fresh byte arrayvoidrestart(int size)start using a fresh byte arrayintsize()voidskip(int size)BuffertoBuffer()Get a Buffer from the streamvoidwrite(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this byte array output stream.voidwrite(int b)Writes the specified byte to this byte array output stream.voidwrite(Buffer data)voidwriteBoolean(boolean v)voidwriteByte(int v)voidwriteBytes(java.lang.String s)voidwriteChar(int v)voidwriteChars(java.lang.String s)voidwriteDouble(double v)voidwriteFloat(float v)voidwriteInt(int v)voidwriteLong(long v)voidwriteShort(int v)voidwriteUTF(java.lang.String str)voidwriteVarInt(int value)voidwriteVarLong(long value)voidwriteVarSignedInt(int value)voidwriteVarSignedLong(long value)
-
-
-
Field Detail
-
DEFAULT_SIZE
private static final int DEFAULT_SIZE
- See Also:
- Constant Field Values
-
buf
protected byte[] buf
-
pos
protected int pos
-
helper
protected AbstractVarIntSupport helper
-
-
Constructor Detail
-
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:
java.lang.IllegalArgumentException- if size is negative.
-
DataByteArrayOutputStream
public DataByteArrayOutputStream(byte[] buf)
-
DataByteArrayOutputStream
public DataByteArrayOutputStream()
Creates a new byte array output stream.
-
-
Method Detail
-
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
public Buffer toBuffer()
Get a Buffer from the stream- Returns:
- the byte sequence
-
write
public void write(int b) throws java.io.IOExceptionWrites the specified byte to this byte array output stream.- Specified by:
writein interfacejava.io.DataOutput- Specified by:
writein classjava.io.OutputStream- Parameters:
b- the byte to be written.- Throws:
java.io.IOException
-
write
public void write(Buffer data) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOExceptionWriteslenbytes from the specified byte array starting at offsetoffto this byte array output stream.- Specified by:
writein interfacejava.io.DataOutput- Overrides:
writein classjava.io.OutputStream- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
java.io.IOException
-
getData
public byte[] getData()
- Returns:
- the underlying byte[] buffer
-
reset
public void reset()
reset the output stream
-
position
public void position(int offset) throws java.io.IOExceptionSet the current position for writing- Parameters:
offset-- Throws:
java.io.IOException
-
position
public int position()
-
size
public int size()
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Specified by:
writeBooleanin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Specified by:
writeBytein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Specified by:
writeShortin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Specified by:
writeCharin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Specified by:
writeIntin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Specified by:
writeLongin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Specified by:
writeFloatin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Specified by:
writeDoublein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Specified by:
writeBytesin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Specified by:
writeCharsin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String str) throws java.io.IOException- Specified by:
writeUTFin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
ensureEnoughBuffer
private void ensureEnoughBuffer(int newcount)
-
resize
protected void resize(int newcount)
-
onWrite
protected void onWrite() throws java.io.IOExceptionThis 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:
java.io.IOException
-
skip
public void skip(int size) throws java.io.IOException- Throws:
java.io.IOException
-
writeVarInt
public void writeVarInt(int value) throws java.io.IOException- Throws:
java.io.IOException
-
writeVarLong
public void writeVarLong(long value) throws java.io.IOException- Throws:
java.io.IOException
-
writeVarSignedInt
public void writeVarSignedInt(int value) throws java.io.IOException- Throws:
java.io.IOException
-
writeVarSignedLong
public void writeVarSignedLong(long value) throws java.io.IOException- Throws:
java.io.IOException
-
-