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 /** |
| 10 * Basic operations for writing to a byte array representing a binary file. |
| 11 * |
| 12 * @author Misha Dmitriev |
| 13 * 30 January 2002 |
| 14 */ |
| 15 public class BinaryFileWriter { |
| 16 |
| 17 protected byte[] buf; |
| 18 protected int curBufSize, bufInc, curBufPos, threshold; |
| 19 private boolean bufferIncreaseAllowed = true; |
| 20 |
| 21 protected void initBuf(int initSize) { |
| 22 buf = new byte[initSize]; |
| 23 curBufSize = initSize; |
| 24 bufInc = initSize / 5; |
| 25 curBufPos = 0; |
| 26 threshold = curBufSize - bufInc; |
| 27 } |
| 28 |
| 29 protected void increaseBuf() { |
| 30 if (!bufferIncreaseAllowed) { |
| 31 return; |
| 32 } |
| 33 byte newBuf[] = new byte[curBufSize + bufInc]; |
| 34 System.arraycopy(buf, 0, newBuf, 0, curBufPos); |
| 35 buf = newBuf; |
| 36 curBufSize = buf.length; |
| 37 threshold = curBufSize - bufInc; |
| 38 } |
| 39 |
| 40 // This should be called with false only when we are sure that we set the ex
act size of the buffer |
| 41 // and there is no need to increase it. |
| 42 protected void setBufferIncreaseMode(boolean increaseMode) { |
| 43 bufferIncreaseAllowed = increaseMode; |
| 44 } |
| 45 |
| 46 public byte[] getBuffer() { |
| 47 return buf; |
| 48 } |
| 49 |
| 50 |
| 51 protected void writeByte(byte b) { |
| 52 if (curBufPos > threshold) { |
| 53 increaseBuf(); |
| 54 } |
| 55 buf[curBufPos++] = b; |
| 56 } |
| 57 |
| 58 protected void writeChar(int ch) { |
| 59 buf[curBufPos++] = (byte) ((ch >> 8) & 255); |
| 60 buf[curBufPos++] = (byte) (ch & 255); |
| 61 if (curBufPos > threshold) { |
| 62 increaseBuf(); |
| 63 } |
| 64 } |
| 65 |
| 66 protected void writeInt(int i) { |
| 67 buf[curBufPos++] = (byte) ((i >> 24) & 255); |
| 68 buf[curBufPos++] = (byte) ((i >> 16) & 255); |
| 69 buf[curBufPos++] = (byte) ((i >> 8) & 255); |
| 70 buf[curBufPos++] = (byte) (i & 255); |
| 71 if (curBufPos > threshold) { |
| 72 increaseBuf(); |
| 73 } |
| 74 } |
| 75 |
| 76 protected void writeLong(long l) { |
| 77 buf[curBufPos++] = (byte) ((l >> 56) & 255); |
| 78 buf[curBufPos++] = (byte) ((l >> 48) & 255); |
| 79 buf[curBufPos++] = (byte) ((l >> 40) & 255); |
| 80 buf[curBufPos++] = (byte) ((l >> 32) & 255); |
| 81 buf[curBufPos++] = (byte) ((l >> 24) & 255); |
| 82 buf[curBufPos++] = (byte) ((l >> 16) & 255); |
| 83 buf[curBufPos++] = (byte) ((l >> 8) & 255); |
| 84 buf[curBufPos++] = (byte) (l & 255); |
| 85 if (curBufPos > threshold) { |
| 86 increaseBuf(); |
| 87 } |
| 88 } |
| 89 |
| 90 protected void writeFloat(float f) { |
| 91 int i = Float.floatToIntBits(f); |
| 92 writeInt(i); |
| 93 } |
| 94 |
| 95 protected void writeDouble(double d) { |
| 96 long l = Double.doubleToLongBits(d); |
| 97 writeLong(l); |
| 98 } |
| 99 } |
OLD | NEW |