1 /*
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
102 int length = 0;
103 for(;;) {
104 if (length < bytes.length) {
105 int numRead = is.read(bytes, length, bytes.length - length);
106 if (numRead < 0) {
107 break; // end of stream
108 }
109 length += numRead;
110 } else {
111 // See if we are at the end of the stream before we grow the array.
112 int nextByte = is.read();
113 if (nextByte < 0) {
114 break;
115 }
116 int capacity = 2 * bytes.length;
117 if (capacity < 128) {
118 capacity = 128;
119 } else if (capacity < 0x4000) {
120 capacity *= 2; // Grow faster until we reach 16kB.
121 }
122 // TODO Java 6 replace new byte[] and arraycopy(): bytes = Arrays.copyOf(bytes, capacity);
123 byte[] newBytes = new byte[capacity];
124 System.arraycopy(bytes, 0, newBytes, 0, length);
125 bytes = newBytes;
126 bytes[length++] = (byte) nextByte;
127 }
128 }
129 return ByteBuffer.wrap(bytes, 0, length);
130 }
131 catch (IOException e) {
132 throw new UncheckedIOException(e);
133 }
134 }
135
136 /**
137 * Same as readHeader(), but returns a VersionInfo rather than a compact int.
138 */
139 public static VersionInfo readHeaderAndDataVersion(ByteBuffer bytes,
140 int dataFormat,
141 Authenticate authenticate)
142 throws IOException {
143 return getVersionInfoFromCompactInt(readHeader(bytes, dataFormat, authenticate));
144 }
145
247 String.format("; data format %02x%02x%02x%02x, format version %d.%d.%d.%d",
248 bytes.get(12), bytes.get(13), bytes.get(14), bytes.get(15),
249 formatVersion[0] & 0xff, formatVersion[1] & 0xff,
250 formatVersion[2] & 0xff, formatVersion[3] & 0xff));
251 }
252
253 bytes.position(headerSize);
254 return // dataVersion
255 ((int)bytes.get(20) << 24) |
256 ((bytes.get(21) & 0xff) << 16) |
257 ((bytes.get(22) & 0xff) << 8) |
258 (bytes.get(23) & 0xff);
259 }
260
261 public static void skipBytes(ByteBuffer bytes, int skipLength) {
262 if (skipLength > 0) {
263 bytes.position(bytes.position() + skipLength);
264 }
265 }
266
267 /**
268 * Returns a VersionInfo for the bytes in the compact version integer.
269 */
270 public static VersionInfo getVersionInfoFromCompactInt(int version) {
271 return VersionInfo.getInstance(
272 version >>> 24, (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
273 }
274
275 // private variables -------------------------------------------------
276
277 /**
278 * Magic numbers to authenticate the data file
279 */
280 private static final byte MAGIC1 = (byte)0xda;
281 private static final byte MAGIC2 = (byte)0x27;
282
283 /**
284 * File format authentication values
285 */
286 private static final byte CHAR_SET_ = 0;
|
1 /*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
102 int length = 0;
103 for(;;) {
104 if (length < bytes.length) {
105 int numRead = is.read(bytes, length, bytes.length - length);
106 if (numRead < 0) {
107 break; // end of stream
108 }
109 length += numRead;
110 } else {
111 // See if we are at the end of the stream before we grow the array.
112 int nextByte = is.read();
113 if (nextByte < 0) {
114 break;
115 }
116 int capacity = 2 * bytes.length;
117 if (capacity < 128) {
118 capacity = 128;
119 } else if (capacity < 0x4000) {
120 capacity *= 2; // Grow faster until we reach 16kB.
121 }
122 bytes = Arrays.copyOf(bytes, capacity);
123 bytes[length++] = (byte) nextByte;
124 }
125 }
126 return ByteBuffer.wrap(bytes, 0, length);
127 }
128 catch (IOException e) {
129 throw new UncheckedIOException(e);
130 }
131 }
132
133 /**
134 * Same as readHeader(), but returns a VersionInfo rather than a compact int.
135 */
136 public static VersionInfo readHeaderAndDataVersion(ByteBuffer bytes,
137 int dataFormat,
138 Authenticate authenticate)
139 throws IOException {
140 return getVersionInfoFromCompactInt(readHeader(bytes, dataFormat, authenticate));
141 }
142
244 String.format("; data format %02x%02x%02x%02x, format version %d.%d.%d.%d",
245 bytes.get(12), bytes.get(13), bytes.get(14), bytes.get(15),
246 formatVersion[0] & 0xff, formatVersion[1] & 0xff,
247 formatVersion[2] & 0xff, formatVersion[3] & 0xff));
248 }
249
250 bytes.position(headerSize);
251 return // dataVersion
252 ((int)bytes.get(20) << 24) |
253 ((bytes.get(21) & 0xff) << 16) |
254 ((bytes.get(22) & 0xff) << 8) |
255 (bytes.get(23) & 0xff);
256 }
257
258 public static void skipBytes(ByteBuffer bytes, int skipLength) {
259 if (skipLength > 0) {
260 bytes.position(bytes.position() + skipLength);
261 }
262 }
263
264 public static byte[] getBytes(ByteBuffer bytes, int length, int additionalSkipLength) {
265 byte[] dest = new byte[length];
266 bytes.get(dest);
267 if (additionalSkipLength > 0) {
268 skipBytes(bytes, additionalSkipLength);
269 }
270 return dest;
271 }
272
273 public static String getString(ByteBuffer bytes, int length, int additionalSkipLength) {
274 CharSequence cs = bytes.asCharBuffer();
275 String s = cs.subSequence(0, length).toString();
276 skipBytes(bytes, length * 2 + additionalSkipLength);
277 return s;
278 }
279
280 public static char[] getChars(ByteBuffer bytes, int length, int additionalSkipLength) {
281 char[] dest = new char[length];
282 bytes.asCharBuffer().get(dest);
283 skipBytes(bytes, length * 2 + additionalSkipLength);
284 return dest;
285 }
286
287 public static int[] getInts(ByteBuffer bytes, int length, int additionalSkipLength) {
288 int[] dest = new int[length];
289 bytes.asIntBuffer().get(dest);
290 skipBytes(bytes, length * 4 + additionalSkipLength);
291 return dest;
292 }
293
294 /**
295 * Returns a VersionInfo for the bytes in the compact version integer.
296 */
297 public static VersionInfo getVersionInfoFromCompactInt(int version) {
298 return VersionInfo.getInstance(
299 version >>> 24, (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
300 }
301
302 // private variables -------------------------------------------------
303
304 /**
305 * Magic numbers to authenticate the data file
306 */
307 private static final byte MAGIC1 = (byte)0xda;
308 private static final byte MAGIC2 = (byte)0x27;
309
310 /**
311 * File format authentication values
312 */
313 private static final byte CHAR_SET_ = 0;
|