1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro.util; |
25 | |
|
26 | |
import java.io.IOException; |
27 | |
import java.io.OutputStream; |
28 | |
import java.io.UnsupportedEncodingException; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
final public class ByteBufferOutputStream extends OutputStream |
34 | |
{ |
35 | |
|
36 | |
private byte[] _buf; |
37 | |
private int _pos; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public ByteBufferOutputStream (int size) |
43 | 1734 | { |
44 | 1734 | _buf = new byte[size]; |
45 | 1734 | _pos = 0; |
46 | 1734 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public void reset () |
53 | |
{ |
54 | 1788 | _pos = 0; |
55 | 1788 | } |
56 | |
|
57 | |
public void write (int i) |
58 | |
{ |
59 | 0 | write((byte) i); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public void write (byte[] b) |
66 | |
{ |
67 | 0 | int len = b.length; |
68 | 0 | ensureCapacity(len); |
69 | 0 | System.arraycopy(b, 0, _buf, _pos, len); |
70 | 0 | _pos += len; |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void write (byte[] b, int offset, int len) |
77 | |
{ |
78 | 233016 | ensureCapacity(len); |
79 | 233016 | System.arraycopy(b, 0, _buf, _pos, len); |
80 | 233016 | _pos += len; |
81 | 233016 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void write (byte b) |
87 | |
{ |
88 | 0 | ensureCapacity(1); |
89 | 0 | _buf[_pos] = b; |
90 | 0 | _pos++; |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public final void ensureCapacity (int len) |
97 | |
{ |
98 | 233016 | if (_buf.length < _pos + len) |
99 | |
{ |
100 | 456 | int blen = _buf.length; |
101 | |
|
102 | 912 | while (blen < _pos + len) |
103 | |
{ |
104 | 456 | blen *= 2; |
105 | |
} |
106 | |
|
107 | 456 | byte[] tmp = new byte[blen]; |
108 | 456 | System.arraycopy(_buf, 0, tmp, 0, _pos); |
109 | 456 | _buf = tmp; |
110 | |
} |
111 | 233016 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public int size () |
117 | |
{ |
118 | 0 | return _pos; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public byte[] getBuffer () |
129 | |
{ |
130 | 0 | return _buf; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public byte[] getBytes () |
139 | |
{ |
140 | 0 | byte[] ret = new byte[_pos]; |
141 | 0 | System.arraycopy(_buf, 0, ret, 0, _pos); |
142 | 0 | return ret; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public String toString () |
149 | |
{ |
150 | 0 | return new String(_buf, 0, _pos); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public String toString (String encoding) throws UnsupportedEncodingException |
157 | |
{ |
158 | 642 | return new String(_buf, 0, _pos, encoding); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public void writeTo (OutputStream out) throws IOException |
165 | |
{ |
166 | 1150 | out.write(_buf, 0, _pos); |
167 | 1136 | } |
168 | |
|
169 | |
public static void main (String arg[]) |
170 | |
{ |
171 | |
try |
172 | |
{ |
173 | |
|
174 | 0 | ByteBufferOutputStream bb = new ByteBufferOutputStream(5); |
175 | |
|
176 | 0 | bb.write((byte) 'T'); |
177 | 0 | bb.write((byte) 'h'); |
178 | 0 | bb.write((byte) 'i'); |
179 | 0 | bb.write((byte) 's'); |
180 | 0 | bb.write((byte) ' '); |
181 | 0 | bb.write((byte) 'i'); |
182 | 0 | bb.write((byte) 's'); |
183 | 0 | bb.write((byte) ' '); |
184 | 0 | bb.write((byte) 'a'); |
185 | 0 | bb.write((byte) ' '); |
186 | 0 | bb.write((byte) 't'); |
187 | 0 | bb.write((byte) 'e'); |
188 | 0 | bb.write((byte) 's'); |
189 | 0 | bb.write((byte) 't'); |
190 | 0 | bb.write((byte) '\n'); |
191 | 0 | bb.write("This is the second line of the byte buffer".getBytes()); |
192 | 0 | bb.write((byte) '\n'); |
193 | 0 | bb.write("This is the third line\n".getBytes()); |
194 | 0 | bb.write("This is the fourth line\n".getBytes()); |
195 | 0 | bb.write((byte) 'E'); |
196 | 0 | bb.write((byte) 'N'); |
197 | 0 | bb.write((byte) 'D'); |
198 | 0 | bb.write((byte) '.'); |
199 | 0 | bb.write((byte) '\n'); |
200 | |
|
201 | 0 | System.out.println("Byte buffer as a string: [" + bb + "]"); |
202 | 0 | System.out.print("Byte buffer written to a stream: ["); |
203 | 0 | bb.writeTo(System.out); |
204 | 0 | System.out.print("]"); |
205 | 0 | System.out.println("DONE"); |
206 | |
} |
207 | 0 | catch (Exception e) |
208 | |
{ |
209 | 0 | e.printStackTrace(); |
210 | 0 | } |
211 | 0 | } |
212 | |
} |
213 | |
|