Package it.unimi.dsi.fastutil.io
Class FastByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- it.unimi.dsi.fastutil.io.MeasurableOutputStream
-
- it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
-
- All Implemented Interfaces:
MeasurableStream
,RepositionableStream
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class FastByteArrayOutputStream extends MeasurableOutputStream implements RepositionableStream
Simple, fast byte-array output stream that exposes the backing array.ByteArrayOutputStream
is nice, but to get its content you must generate each time a new object. This doesn't happen here.This class will automatically enlarge the backing array, doubling its size whenever new space is needed. The
reset()
method will mark the content as empty, but will not decrease the capacity: usetrim()
for that purpose.- Author:
- Sebastiano Vigna
-
-
Field Summary
Fields Modifier and Type Field Description byte[]
array
The array backing the output stream.static int
DEFAULT_INITIAL_CAPACITY
The array backing the output stream.int
length
The number of valid bytes inarray
.
-
Constructor Summary
Constructors Constructor Description FastByteArrayOutputStream()
Creates a new array output stream with an initial capacity ofDEFAULT_INITIAL_CAPACITY
bytes.FastByteArrayOutputStream(byte[] a)
Creates a new array output stream wrapping a given byte array.FastByteArrayOutputStream(int initialCapacity)
Creates a new array output stream with a given initial capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
length()
Returns the overall length of this stream (optional operation).long
position()
Returns the current stream position.void
position(long newPosition)
Sets the current stream position.void
reset()
Marks this array output stream as empty.void
trim()
Ensures that the length of the backing array is equal tolength
.void
write(byte[] b, int off, int len)
void
write(int b)
-
-
-
Field Detail
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITY
The array backing the output stream.- See Also:
- Constant Field Values
-
array
public byte[] array
The array backing the output stream.
-
length
public int length
The number of valid bytes inarray
.
-
-
Constructor Detail
-
FastByteArrayOutputStream
public FastByteArrayOutputStream()
Creates a new array output stream with an initial capacity ofDEFAULT_INITIAL_CAPACITY
bytes.
-
FastByteArrayOutputStream
public FastByteArrayOutputStream(int initialCapacity)
Creates a new array output stream with a given initial capacity.- Parameters:
initialCapacity
- the initial length of the backing array.
-
FastByteArrayOutputStream
public FastByteArrayOutputStream(byte[] a)
Creates a new array output stream wrapping a given byte array.- Parameters:
a
- the byte array to wrap.
-
-
Method Detail
-
reset
public void reset()
Marks this array output stream as empty.
-
trim
public void trim()
Ensures that the length of the backing array is equal tolength
.
-
write
public void write(int b)
- Specified by:
write
in classjava.io.OutputStream
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
position
public void position(long newPosition)
Description copied from interface:RepositionableStream
Sets the current stream position.- Specified by:
position
in interfaceRepositionableStream
- Parameters:
newPosition
- the new stream position.
-
position
public long position()
Description copied from interface:RepositionableStream
Returns the current stream position.- Specified by:
position
in interfaceMeasurableStream
- Specified by:
position
in interfaceRepositionableStream
- Returns:
- the current stream position.
-
length
public long length() throws java.io.IOException
Description copied from interface:MeasurableStream
Returns the overall length of this stream (optional operation). In most cases, this will require the stream to perform some extra action, possibly changing the state of the input stream itself (typically, reading all the bytes up to the end, or flushing on output stream). Implementing classes should always document what state will the input stream be in after calling this method, and which kind of exception could be thrown.- Specified by:
length
in interfaceMeasurableStream
- Throws:
java.io.IOException
-
-