Package com.gigaspaces.internal.io
Class GSByteArrayOutputStream
java.lang.Object
java.io.OutputStream
com.gigaspaces.internal.io.GSByteArrayOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
PbsOutputStream
This class is an optimized porting of java.io.ByteArrayOutputStream: a) All methods are not
synchronized. b) Most safety checks have been removed. c) ensureCapacity method have been added,
for low level optimizations. Naturally, this class and all of its methods are not thread safe.
- Since:
- 6.6
- Author:
- niv
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new byte array output stream.GSByteArrayOutputStream(int capacity) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidcopyToBuffer(ByteBuffer buffer) Copy internal bufferbooleanensureCapacity(int delta) byte[]Gets internal buffersintThe current buffer capacity.byte[]Gets the underlying buffer if its size is at most two times more than the actual data written on the buffer.intgetCount()get buffer countvoidreset()Resets thecountfield of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.voidsetBuffer(byte[] buf) Set stream buffer, and reset the counter.voidsetBuffer(byte[] buffer, int count) Set stream buffer and set the counter.voidsetSize(int size) Set the buffer size.intsize()Returns the current size of the buffer.byte[]Creates a newly allocated byte array.voidwrite(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.voidwriteByte(byte b) voidwriteByte(byte v, int position) Writes the provided value at the specified position.voidwriteInt(int v, int position) Writes the provided value at the specified position.voidwriteShort(short v, int position) Writes the provided value at the specified position.voidwriteTo(OutputStream out) Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method usingout.write(buf, 0, count).Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
Field Details
-
_buffer
protected byte[] _bufferThe buffer where data is stored. -
_count
protected int _countThe number of valid bytes in the buffer.
-
-
Constructor Details
-
GSByteArrayOutputStream
public GSByteArrayOutputStream()Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary. -
GSByteArrayOutputStream
public GSByteArrayOutputStream(int capacity) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.- Parameters:
capacity- the initial capacity.- Throws:
IllegalArgumentException- if size is negative.
-
-
Method Details
-
copyToBuffer
Copy internal buffer- Parameters:
arr- target buffersrcPos- start position
-
setBuffer
public void setBuffer(byte[] buf) Set stream buffer, and reset the counter.- Parameters:
buf- new buffer
-
setBuffer
public void setBuffer(byte[] buffer, int count) Set stream buffer and set the counter.- Parameters:
buf- new buffercount- amount of valid bytes
-
getBuffer
public byte[] getBuffer()Gets internal buffers- Returns:
- internal buffer
-
getCount
public int getCount()get buffer count- Returns:
- count
-
setSize
public void setSize(int size) Set the buffer size.- Parameters:
size- amount of valid bytes
-
size
public int size()Returns the current size of the buffer.- Returns:
- the value of the
countfield, which is the number of valid bytes in this output stream. - See Also:
-
getCapacity
public int getCapacity()The current buffer capacity.- Returns:
- buffer current capacity
-
write
public void write(int b) Writes the specified byte to this byte array output stream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to be written.
-
writeByte
public void writeByte(byte b) -
writeByte
public void writeByte(byte v, int position) Writes the provided value at the specified position. The previous data is overridden. Unsafe operation - it's the user's responsibility to ensure position is not overflow. -
writeShort
public void writeShort(short v, int position) Writes the provided value at the specified position. The previous data is overridden. Unsafe operation - it's the user's responsibility to ensure position is not overflow. -
writeInt
public void writeInt(int v, int position) Writes the provided value at the specified position. The previous data is overridden. Unsafe operation - it's the user's responsibility to ensure position is not overflow. -
write
public void write(byte[] b, int off, int len) Writeslenbytes from the specified byte array starting at offsetoffto this byte array output stream.- Overrides:
writein classOutputStream- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.
-
ensureCapacity
public boolean ensureCapacity(int delta) -
writeTo
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method usingout.write(buf, 0, count).- Parameters:
out- the output stream to which to write the data.- Throws:
IOException- if an I/O error occurs.
-
reset
public void reset()Resets thecountfield of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.- See Also:
-
toByteArray
public byte[] toByteArray()Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.- Returns:
- the current contents of this output stream, as a byte array.
- See Also:
-
getCompactBuffer
public byte[] getCompactBuffer()Gets the underlying buffer if its size is at most two times more than the actual data written on the buffer. If this is not the case, creates a newly allocated byte array. Its size is the current size of the actual content of this output stream and the valid contents of the buffer have been copied into it. If the last 3 consecutive calls have resulted in new buffer creation, the underlying buffer is replaced with the last created buffer while keeping the same count position.
-