Debian Codegraph @

src/com/adobe/internal/xmp/impl/ByteBuffer.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2006 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10
11
12 package com.adobe.internal.xmp.impl;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17
18
19 /**
20 * Byte buffer container including length of valid data.
21 *
22 * @author Stefan Makswit
23 * @version $Revision$
24 * @since 11.10.2006
25 */
26 public class ByteBuffer
27 {
28 /** */
29 private byte[] buffer;
30 /** */
31 private int length;
32 /** */
33 private String encoding = null;
34
35
36 /**
37 * @param initialCapacity the initial capacity for this buffer
38 */
39 public ByteBuffer(int initialCapacity)
40 {
41 this.buffer = new byte[initialCapacity];
42 this.length = 0;
43 }
44
45
46 /**
47 * @param buffer a byte array that will be wrapped with <code>ByteBuffer</code>.
48 */
49 public ByteBuffer(byte[] buffer)
50 {
51 this.buffer = buffer;
52 this.length = buffer.length;
53 }
54
55
56 /**
57 * @param buffer a byte array that will be wrapped with <code>ByteBuffer</code>.
58 * @param length the length of valid bytes in the array
59 */
60 public ByteBuffer(byte[] buffer, int length)
61 {
62 if (length > buffer.length)
63 {
64 throw new ArrayIndexOutOfBoundsException("Valid length exceeds the buffer length.");
65 }
66 this.buffer = buffer;
67 this.length = length;
68 }
69
70
71 /**
72 * Loads the stream into a buffer.
73 *
74 * @param in an InputStream
75 * @throws IOException If the stream cannot be read.
76 */
77 public ByteBuffer(InputStream in) throws IOException
78 {
79 // load stream into buffer
80 int chunk = 16384;
81 this.length = 0;
82 this.buffer = new byte[chunk];
83
84 int read;
85 while ((read = in.read(this.buffer, this.length, chunk)) > 0)
86 {
87 this.length += read;
88 if (read == chunk)
89 {
90 ensureCapacity(length + chunk);
91 }
92 else
93 {
94 break;
95 }
96 }
97 }
98
99
100 /**
101 * @param buffer a byte array that will be wrapped with <code>ByteBuffer</code>.
102 * @param offset the offset of the provided buffer.
103 * @param length the length of valid bytes in the array
104 */
105 public ByteBuffer(byte[] buffer, int offset, int length)
106 {
107 if (length > buffer.length - offset)
108 {
109 throw new ArrayIndexOutOfBoundsException("Valid length exceeds the buffer length.");
110 }
111 this.buffer = new byte[length];
112 System.arraycopy(buffer, offset, this.buffer, 0, length);
113 this.length = length;
114 }
115
116
117 /**
118 * @return Returns a byte stream that is limited to the valid amount of bytes.
119 */
120 public InputStream getByteStream()
121 {
122 return new ByteArrayInputStream(buffer, 0, length);
123 }
124
125
126 /**
127 * @return Returns the length, that means the number of valid bytes, of the buffer;
128 * the inner byte array might be bigger than that.
129 */
130 public int length()
131 {
132 return length;
133 }
134
135
136 // /**
137 // * <em>Note:</em> Only the byte up to length are valid!
138 // * @return Returns the inner byte buffer.
139 // */
140 // public byte[] getBuffer()
141 // {
142 // return buffer;
143 // }
144
145
146 /**
147 * @param index the index to retrieve the byte from
148 * @return Returns a byte from the buffer
149 */
150 public byte byteAt(int index)
151 {
152 if (index < length)
153 {
154 return buffer[index];
155 }
156 else
157 {
158 throw new IndexOutOfBoundsException("The index exceeds the valid buffer area");
159 }
160 }
161
162
163 /**
164 * @param index the index to retrieve a byte as int or char.
165 * @return Returns a byte from the buffer
166 */
167 public int charAt(int index)
168 {
169 if (index < length)
170 {
171 return buffer[index] & 0xFF;
172 }
173 else
174 {
175 throw new IndexOutOfBoundsException("The index exceeds the valid buffer area");
176 }
177 }
178
179
180 /**
181 * Appends a byte to the buffer.
182 * @param b a byte
183 */
184 public void append(byte b)
185 {
186 ensureCapacity(length + 1);
187 buffer[length++] = b;
188 }
189
190
191 /**
192 * Appends a byte array or part of to the buffer.
193 *
194 * @param bytes a byte array
195 * @param offset an offset with
196 * @param len
197 */
198 public void append(byte[] bytes, int offset, int len)
199 {
200 ensureCapacity(length + len);
201 System.arraycopy(bytes, offset, buffer, length, len);
202 length += len;
203 }
204
205
206 /**
207 * Append a byte array to the buffer
208 * @param bytes a byte array
209 */
210 public void append(byte[] bytes)
211 {
212 append(bytes, 0, bytes.length);
213 }
214
215
216 /**
217 * Append another buffer to this buffer.
218 * @param anotherBuffer another <code>ByteBuffer</code>
219 */
220 public void append(ByteBuffer anotherBuffer)
221 {
222 append(anotherBuffer.buffer, 0, anotherBuffer.length);
223 }
224
225
226 /**
227 * Detects the encoding of the byte buffer, stores and returns it.
228 * Only UTF-8, UTF-16LE/BE and UTF-32LE/BE are recognized.
229 * <em>Note:</em> UTF-32 flavors are not supported by Java, the XML-parser will complain.
230 *
231 * @return Returns the encoding string.
232 */
233 public String getEncoding()
234 {
235 if (encoding == null)
236 {
237 // needs four byte at maximum to determine encoding
238 if (length < 2)
239 {
240 // only one byte length must be UTF-8
241 encoding = "UTF-8";
242 }
243 else if (buffer[0] == 0)
244 {
245 // These cases are:
246 // 00 nn -- -- - Big endian UTF-16
247 // 00 00 00 nn - Big endian UTF-32
248 // 00 00 FE FF - Big endian UTF 32
249
250 if (length < 4 || buffer[1] != 0)
251 {
252 encoding = "UTF-16BE";
253 }
254 else if ((buffer[2] & 0xFF) == 0xFE && (buffer[3] & 0xFF) == 0xFF)
255 {
256 encoding = "UTF-32BE";
257 }
258 else
259 {
260 encoding = "UTF-32";
261 }
262 }
263 else if ((buffer[0] & 0xFF) < 0x80)
264 {
265 // These cases are:
266 // nn mm -- -- - UTF-8, includes EF BB BF case
267 // nn 00 -- -- - Little endian UTF-16
268
269 if (buffer[1] != 0)
270 {
271 encoding = "UTF-8";
272 }
273 else if (length < 4 || buffer[2] != 0)
274 {
275 encoding = "UTF-16LE";
276 }
277 else
278 {
279 encoding = "UTF-32LE";
280 }
281 }
282 else
283 {
284 // These cases are:
285 // EF BB BF -- - UTF-8
286 // FE FF -- -- - Big endian UTF-16
287 // FF FE 00 00 - Little endian UTF-32
288 // FF FE -- -- - Little endian UTF-16
289
290 if ((buffer[0] & 0xFF) == 0xEF)
291 {
292 encoding = "UTF-8";
293 }
294 else if ((buffer[0] & 0xFF) == 0xFE)
295 {
296 encoding = "UTF-16"; // in fact BE
297 }
298 else if (length < 4 || buffer[2] != 0)
299 {
300 encoding = "UTF-16"; // in fact LE
301 }
302 else
303 {
304 encoding = "UTF-32"; // in fact LE
305 }
306 }
307 }
308
309 return encoding;
310 }
311
312
313 /**
314 * Ensures the requested capacity by increasing the buffer size when the
315 * current length is exceeded.
316 *
317 * @param requestedLength requested new buffer length
318 */
319 private void ensureCapacity(int requestedLength)
320 {
321 if (requestedLength > buffer.length)
322 {
323 byte[] oldBuf = buffer;
324 buffer = new byte[oldBuf.length * 2];
325 System.arraycopy(oldBuf, 0, buffer, 0, oldBuf.length);
326 }
327 }
328 }