01: package it.unimi.dsi.fastutil.io;
02:
03: /*
04: * fastutil: Fast & compact type-specific collections for Java
05: *
06: * Copyright (C) 2003-2008 Sebastiano Vigna
07: *
08: * This library is free software; you can redistribute it and/or modify it
09: * under the terms of the GNU Lesser General Public License as published by the Free
10: * Software Foundation; either version 2.1 of the License, or (at your option)
11: * any later version.
12: *
13: * This library is distributed in the hope that it will be useful, but
14: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16: * for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public License
19: * along with this program; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21: *
22: */
23:
24: import it.unimi.dsi.fastutil.bytes.ByteArrays;
25:
26: import java.io.IOException;
27: import java.io.OutputStream;
28:
29: /** Simple, fast byte-array output stream that exposes the backing array.
30: *
31: * <P>{@link java.io.ByteArrayOutputStream} is nice, but to get its content you
32: * must generate each time a new object. This doesn't happen here.
33: *
34: * <P>This class will automatically enlarge the backing array, doubling its
35: * size whenever new space is needed. The {@link #reset()} method will
36: * mark the content as empty, but will not decrease the capacity: use
37: * {@link #trim()} for that purpose.
38: *
39: * @author Sebastiano Vigna
40: */
41:
42: public class FastByteArrayOutputStream extends OutputStream {
43:
44: /** The array backing the output stream. */
45: public final static int DEFAULT_INITIAL_CAPACITY = 16;
46:
47: /** The array backing the output stream. */
48: public byte[] array;
49:
50: /** The number of valid bytes in {@link #array}. */
51: public int length;
52:
53: /** Creates a new array output stream with an initial capacity of {@link #DEFAULT_INITIAL_CAPACITY} bytes. */
54: public FastByteArrayOutputStream() {
55: this (DEFAULT_INITIAL_CAPACITY);
56: }
57:
58: /** Creates a new array output stream with a given initial capacity.
59: *
60: * @param initialCapacity the initial length of the backing array.
61: */
62: public FastByteArrayOutputStream(final int initialCapacity) {
63: array = new byte[initialCapacity];
64: }
65:
66: /** Creates a new array output stream wrapping a given byte array.
67: *
68: * @param a the byte array to wrap.
69: */
70: public FastByteArrayOutputStream(final byte[] a) {
71: array = a;
72: }
73:
74: /** Marks this array output stream as empty. */
75: public void reset() {
76: length = 0;
77: }
78:
79: /** Ensures that the length of the backing array is equal to {@link #length}. */
80: public void trim() {
81: array = ByteArrays.trim(array, length);
82: }
83:
84: public void write(final int b) {
85: if (length == array.length)
86: array = ByteArrays.grow(array, length + 1);
87: array[length++] = (byte) b;
88: }
89:
90: // TODO: test this
91: public void write(final byte[] b, final int off, final int len)
92: throws IOException {
93: ByteArrays.ensureOffsetLength(b, off, len);
94: if (length + len > array.length)
95: array = ByteArrays.grow(array, length + len, length);
96: System.arraycopy(b, off, array, length, len);
97: length += len;
98: }
99: }
|