1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.util.cache;
17
18 import java.io.InputStream;
19 import java.io.OutputStream;
20
21
22
23
24
25 public class FastByteArrayOutputStream extends OutputStream {
26
27
28
29 protected byte[] buf = null;
30 protected int size = 0;
31
32
33
34
35 public FastByteArrayOutputStream() {
36 this(5 * 1024);
37 }
38
39
40
41
42 public FastByteArrayOutputStream(int initSize) {
43 this.size = 0;
44 this.buf = new byte[initSize];
45 }
46
47
48
49
50 private void verifyBufferSize(int sz) {
51 if (sz > buf.length) {
52 byte[] old = buf;
53 buf = new byte[Math.max(sz, 2 * buf.length )];
54 System.arraycopy(old, 0, buf, 0, old.length);
55 old = null;
56 }
57 }
58
59 public int getSize() {
60 return size;
61 }
62
63
64
65
66
67
68 public byte[] getByteArray() {
69 return buf;
70 }
71
72 public final void write(byte b[]) {
73 verifyBufferSize(size + b.length);
74 System.arraycopy(b, 0, buf, size, b.length);
75 size += b.length;
76 }
77
78 public final void write(byte b[], int off, int len) {
79 verifyBufferSize(size + len);
80 System.arraycopy(b, off, buf, size, len);
81 size += len;
82 }
83
84 public final void write(int b) {
85 verifyBufferSize(size + 1);
86 buf[size++] = (byte) b;
87 }
88
89 public void reset() {
90 size = 0;
91 }
92
93
94
95
96 public InputStream getInputStream() {
97 return new FastByteArrayInputStream(buf, size);
98 }
99
100 }