Package cn.nukkit.nbt.stream
Class FastByteArrayOutputStream
java.lang.Object
java.io.OutputStream
cn.nukkit.nbt.stream.FastByteArrayOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
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
- Author:
- Sebastiano Vigna
-
Field Summary
Modifier and TypeFieldDescriptionbyte[]
The array backing the output stream.static final int
The array backing the output stream.int
The number of valid bytes inarray
.static final long
-
Constructor Summary
ConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptionstatic void
ensureOffsetLength
(int arrayLength, int offset, int length) static byte[]
grow
(byte[] array, int length) static byte[]
grow
(byte[] array, int length, int preserve) long
length()
long
position()
void
position
(long newPosition) reset()
Marks this array output stream as empty.byte[]
void
write
(byte[] b, int off, int len) void
write
(int b) Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
Field Details
-
ONEOVERPHI
public static final long ONEOVERPHI- See Also:
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITYThe array backing the output stream.- See Also:
-
array
public byte[] arrayThe array backing the output stream. -
length
public int lengthThe number of valid bytes inarray
.
-
-
Constructor Details
-
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 Details
-
reset
Marks this array output stream as empty. -
write
public void write(int b) - Specified by:
write
in classOutputStream
-
ensureOffsetLength
public static void ensureOffsetLength(int arrayLength, int offset, int length) -
grow
public static byte[] grow(byte[] array, int length) -
grow
public static byte[] grow(byte[] array, int length, int preserve) -
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
position
public void position(long newPosition) -
position
public long position() -
length
- Throws:
IOException
-
toByteArray
public byte[] toByteArray()
-