Package com.cedarsoftware.util
Class FastByteArrayOutputStream
java.lang.Object
java.io.OutputStream
com.cedarsoftware.util.FastByteArrayOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
Faster version of ByteArrayOutputStream that does not have synchronized methods and
also provides direct access to its internal buffer so that it does not need to be
duplicated when read.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 1K (1024 bytes).FastByteArrayOutputStream
(int capacity) Construct a new FastByteArrayOutputStream with the passed in capacity, and a default delta (1024).FastByteArrayOutputStream
(int capacity, int delta) Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 'capacity'. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Reset the stream so it can be used again.byte[]
int
size()
The logical size of the byte[] this stream represents, not its physical size, which could be larger.toString()
void
write
(byte[] bytes, int offset, int len) Writeslen
bytes from the specified byte array starting at offsetoff
to this stream.void
write
(int b) Writes the specified byte to this byte array output stream.void
writeTo
(byte[] dest) Copy the internal byte[] to the passed in byte[].void
writeTo
(OutputStream out) Convenience method to copy the contained byte[] to the passed in OutputStream.Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
Field Details
-
buffer
protected byte[] buffer -
size
protected int size -
delta
protected int delta
-
-
Constructor Details
-
FastByteArrayOutputStream
public FastByteArrayOutputStream()Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 1K (1024 bytes). The delta increment is x2. -
FastByteArrayOutputStream
public FastByteArrayOutputStream(int capacity) Construct a new FastByteArrayOutputStream with the passed in capacity, and a default delta (1024). The delta increment is x2.- Parameters:
capacity
- int size of internal buffer
-
FastByteArrayOutputStream
public FastByteArrayOutputStream(int capacity, int delta) Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 'capacity'.- Parameters:
capacity
- int capacity (internal buffer size), must be > 0delta
- int delta, size to increase the internal buffer by when limit reached. If the value is negative, then the internal buffer is doubled in size when additional capacity is needed.
-
-
Method Details
-
getBuffer
public byte[] getBuffer()- Returns:
- byte[], the internal byte buffer. Remember, the length of this array is likely larger than 'size' (whats been written to it). Therefore, use this byte[] along with 0 to size() to fetch the contents of this buffer without creating a new byte[].
-
write
public void write(int b) Writes the specified byte to this byte array output stream.- Specified by:
write
in classOutputStream
- Parameters:
b
- the byte to be written.
-
write
public void write(byte[] bytes, int offset, int len) Writeslen
bytes from the specified byte array starting at offsetoff
to this stream.- Overrides:
write
in classOutputStream
- Parameters:
bytes
- byte[] the data to write to this stream.offset
- the start offset in the data.len
- the number of bytes to write.
-
writeTo
Convenience method to copy the contained byte[] to the passed in OutputStream. You could also code out.write(fastBa.getBuffer(), 0, fastBa.size())- Parameters:
out
- OutputStream target- Throws:
IOException
- if one occurs
-
writeTo
public void writeTo(byte[] dest) Copy the internal byte[] to the passed in byte[]. No new space is allocated.- Parameters:
dest
- byte[] target
-
toString
-
clear
public void clear()Reset the stream so it can be used again. The size() will be 0, but the internal storage is still allocated. -
size
public int size()The logical size of the byte[] this stream represents, not its physical size, which could be larger.- Returns:
- int the number of bytes written to this stream
-