ByteArrayOStreampublic final class ByteArrayOStream extends OutputStream An unsynchronized version of java.io.ByteArrayOutputStream that can expose
the underlying byte array without a defensive clone and can also be converted
to a {@link ByteArrayIStream} without intermediate array copies.
All argument validation is disabled in release mode.
NOTE: copy-on-write not supported |
Fields Summary |
---|
private byte[] | m_buf | private int | m_pos | private static final int | NATIVE_COPY_THRESHOLD |
Constructors Summary |
---|
public ByteArrayOStream(int initialCapacity)Callee takes ownership of 'buf'.
if ($assert.ENABLED)
$assert.ASSERT (initialCapacity >= 0, "negative initial capacity: " + initialCapacity);
m_buf = new byte [initialCapacity];
|
Methods Summary |
---|
public final int | capacity()
return m_buf.length;
| public final void | close()Equivalent to {@link #reset()}.
reset ();
| public final byte[] | copyByteArray()
final int pos = m_pos;
final byte [] result = new byte [pos];
final byte [] mbuf = m_buf;
if (pos < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < pos; ++ i) result [i] = mbuf [i];
else
System.arraycopy (mbuf, 0, result, 0, pos);
return result;
| public final byte[] | getByteArray()
return m_buf;
| public final void | reset()Does not reduce the current capacity.
m_pos = 0;
| public final int | size()
return m_pos;
| public final ByteArrayIStream | toByteIStream()
return new ByteArrayIStream (m_buf, m_pos);
| public final void | write(int b)
final int pos = m_pos;
final int capacity = pos + 1;
byte [] mbuf = m_buf;
final int mbuflen = mbuf.length;
if (mbuflen < capacity)
{
final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
if (pos < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
else
System.arraycopy (mbuf, 0, newbuf, 0, pos);
m_buf = mbuf = newbuf;
}
mbuf [pos] = (byte) b;
m_pos = capacity;
| public final void | write(byte[] buf, int offset, int length)
if ($assert.ENABLED)
$assert.ASSERT ((offset >= 0) && (offset <= buf.length) &&
(length >= 0) && ((offset + length) <= buf.length),
"invalid input (" + buf.length + ", " + offset + ", " + length + ")");
final int pos = m_pos;
final int capacity = pos + length;
byte [] mbuf = m_buf;
final int mbuflen = mbuf.length;
if (mbuflen < capacity)
{
final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
if (pos < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
else
System.arraycopy (mbuf, 0, newbuf, 0, pos);
m_buf = mbuf = newbuf;
}
if (length < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < length; ++ i) mbuf [pos + i] = buf [offset + i];
else
System.arraycopy (buf, offset, mbuf, pos, length);
m_pos = capacity;
| public final void | write2(int b1, int b2)
final int pos = m_pos;
final int capacity = pos + 2;
byte [] mbuf = m_buf;
final int mbuflen = mbuf.length;
if (mbuflen < capacity)
{
final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
if (pos < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
else
System.arraycopy (mbuf, 0, newbuf, 0, pos);
m_buf = mbuf = newbuf;
}
mbuf [pos] = (byte) b1;
mbuf [pos + 1] = (byte) b2;
m_pos = capacity;
| public final void | write3(int b1, int b2, int b3)
final int pos = m_pos;
final int capacity = pos + 3;
byte [] mbuf = m_buf;
final int mbuflen = mbuf.length;
if (mbuflen < capacity)
{
final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
if (pos < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
else
System.arraycopy (mbuf, 0, newbuf, 0, pos);
m_buf = mbuf = newbuf;
}
mbuf [pos] = (byte) b1;
mbuf [pos + 1] = (byte) b2;
mbuf [pos + 2] = (byte) b3;
m_pos = capacity;
| public final void | write4(int b1, int b2, int b3, int b4)
final int pos = m_pos;
final int capacity = pos + 4;
byte [] mbuf = m_buf;
final int mbuflen = mbuf.length;
if (mbuflen < capacity)
{
final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
if (pos < NATIVE_COPY_THRESHOLD)
for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
else
System.arraycopy (mbuf, 0, newbuf, 0, pos);
m_buf = mbuf = newbuf;
}
mbuf [pos] = (byte) b1;
mbuf [pos + 1] = (byte) b2;
mbuf [pos + 2] = (byte) b3;
mbuf [pos + 3] = (byte) b4;
m_pos = capacity;
| public final void | writeTo(java.io.OutputStream out)
out.write (m_buf, 0, m_pos);
|
|