OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2002-2008 Sun Microsystems, Inc. All rights reserved |
| 2 * |
| 3 * This program is distributed under the terms of |
| 4 * the GNU General Public License Version 2. See the LICENSE file |
| 5 * at the top of the source tree. |
| 6 */ |
| 7 package org.pantsbuild.jmake; |
| 8 |
| 9 import java.io.DataInputStream; |
| 10 import java.io.ByteArrayInputStream; |
| 11 import java.io.IOException; |
| 12 |
| 13 /** |
| 14 * Basic operations for reading a byte array representing a binary file. |
| 15 * |
| 16 * @author Misha Dmitriev |
| 17 * 10 November 2001 |
| 18 */ |
| 19 public class BinaryFileReader { |
| 20 |
| 21 protected byte[] buf; |
| 22 protected int curBufPos; |
| 23 protected String fileFullPath; // Required only for nice error reports |
| 24 |
| 25 protected void initBuf(byte[] buf, String fileFullPath) { |
| 26 this.buf = buf; |
| 27 curBufPos = 0; |
| 28 this.fileFullPath = fileFullPath; |
| 29 } |
| 30 |
| 31 protected char nextChar() { |
| 32 return (char) (((buf[curBufPos++] & 255) << 8) + (buf[curBufPos++] & 255
)); |
| 33 } |
| 34 |
| 35 protected char getChar(int bufPos) { |
| 36 return (char) (((buf[bufPos] & 255) << 8) + (buf[bufPos+1] & 255)); |
| 37 } |
| 38 |
| 39 protected int nextInt() { |
| 40 return ((buf[curBufPos++] & 255) << 24) + ((buf[curBufPos++] & 255) << 1
6) + |
| 41 ((buf[curBufPos++] & 255) << 8) + (buf[curBufPos++] & 255); |
| 42 } |
| 43 |
| 44 protected int getInt(int bufPos) { |
| 45 return ((buf[bufPos] & 255) << 24) + ((buf[bufPos+1] & 255) << 16) + |
| 46 ((buf[bufPos+2] & 255) << 8) + (buf[bufPos+3] & 255); |
| 47 } |
| 48 |
| 49 protected long nextLong() { |
| 50 long res = getLong(curBufPos); |
| 51 curBufPos += 8; |
| 52 return res; |
| 53 } |
| 54 |
| 55 protected long getLong(int bufPos) { |
| 56 DataInputStream bufin = |
| 57 new DataInputStream(new ByteArrayInputStream(buf, bufPos, 8)); |
| 58 try { |
| 59 return bufin.readLong(); |
| 60 } catch (IOException e) { |
| 61 throw new PrivateException(e); |
| 62 } |
| 63 } |
| 64 |
| 65 protected float nextFloat() { |
| 66 float res = getFloat(curBufPos); |
| 67 curBufPos += 4; |
| 68 return res; |
| 69 } |
| 70 |
| 71 protected float getFloat(int bufPos) { |
| 72 DataInputStream bufin = |
| 73 new DataInputStream(new ByteArrayInputStream(buf, bufPos, 4)); |
| 74 try { |
| 75 return bufin.readFloat(); |
| 76 } catch (IOException e) { |
| 77 throw new PrivateException(e); |
| 78 } |
| 79 } |
| 80 |
| 81 protected double nextDouble() { |
| 82 double res = getDouble(curBufPos); |
| 83 curBufPos += 8; |
| 84 return res; |
| 85 } |
| 86 |
| 87 protected double getDouble(int bufPos) { |
| 88 DataInputStream bufin = |
| 89 new DataInputStream(new ByteArrayInputStream(buf, bufPos, 8)); |
| 90 try { |
| 91 return bufin.readDouble(); |
| 92 } catch (IOException e) { |
| 93 throw new PrivateException(e); |
| 94 } |
| 95 } |
| 96 } |
OLD | NEW |