Package com.cedarsoftware.util
Class FastByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.cedarsoftware.util.FastByteArrayOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public class FastByteArrayOutputStream extends OutputStream
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.
-
-
Constructor Summary
Constructors Constructor Description FastByteArrayOutputStream()
Construct 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Reset the stream so it can be used again.byte[]
getBuffer()
int
size()
The logical size of the byte[] this stream represents, not its physical size, which could be larger.String
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
-
-
-
-
Constructor Detail
-
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
-
-
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 Detail
-
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
public void writeTo(OutputStream out) throws IOException
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
-
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
public String 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.
-
-