001: /*
002: * @(#)ByteBuffer.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution of
007: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.compiler;
010:
011: import java.io.IOException;
012: import java.io.OutputStream;
013:
014: public class ByteBuffer {
015: byte[] buffer;
016:
017: int size = 0;
018:
019: public ByteBuffer() {
020: this (64);
021: }
022:
023: public ByteBuffer(int size) {
024: buffer = new byte[size];
025: }
026:
027: public void add(byte b) {
028: if (size + 1 > buffer.length) {
029: byte[] newBuf = new byte[(size + 1) * 2];
030: System.arraycopy(buffer, 0, newBuf, 0, buffer.length);
031: buffer = newBuf;
032: }
033: buffer[size++] = b;
034: }
035:
036: public void add(short s) {
037: set(s, size);
038: }
039:
040: public void set(short s, int offset) {
041: if (offset + 2 > buffer.length) {
042: byte[] newBuf = new byte[(offset + 2) * 2];
043: System.arraycopy(buffer, 0, newBuf, 0, size);
044: buffer = newBuf;
045: }
046: buffer[offset] = (byte) (s >> 8);
047: buffer[offset + 1] = (byte) s;
048: if (offset + 2 > size) {
049: size = offset + 2;
050: }
051: }
052:
053: public void add(int s) {
054: set(s, size);
055: }
056:
057: public void set(int s, int offset) {
058: if (offset + 4 > buffer.length) {
059: byte[] newBuf = new byte[(offset + 4) * 2];
060: System.arraycopy(buffer, 0, newBuf, 0, buffer.length);
061: buffer = newBuf;
062: }
063: buffer[offset] = (byte) (s >> 24);
064: buffer[offset + 1] = (byte) (s >> 16);
065: buffer[offset + 2] = (byte) (s >> 8);
066: buffer[offset + 3] = (byte) s;
067: if (offset + 4 > size) {
068: size = offset + 4;
069: }
070: }
071:
072: public void add(long l) {
073: if (size + 8 > buffer.length) {
074: byte[] newBuf = new byte[(size + 8) * 2];
075: System.arraycopy(buffer, 0, newBuf, 0, buffer.length);
076: buffer = newBuf;
077: }
078: buffer[size++] = (byte) (l >> 56);
079: buffer[size++] = (byte) (l >> 48);
080: buffer[size++] = (byte) (l >> 40);
081: buffer[size++] = (byte) (l >> 32);
082: buffer[size++] = (byte) (l >> 24);
083: buffer[size++] = (byte) (l >> 16);
084: buffer[size++] = (byte) (l >> 8);
085: buffer[size++] = (byte) l;
086: }
087:
088: public void add(byte[] b, int offset, int len) {
089: add(b, offset, len, size);
090: }
091:
092: public void add(byte[] b, int offset, int len, int dst_offset) {
093: if (dst_offset + len > buffer.length) {
094: byte[] newBuf = new byte[(dst_offset + 1) * 2 + len];
095: System.arraycopy(buffer, 0, newBuf, 0, size);
096: buffer = newBuf;
097: }
098: System.arraycopy(b, offset, buffer, dst_offset, len);
099: if (dst_offset + len > size) {
100: size = dst_offset + len;
101: }
102: }
103:
104: public void prepend(ByteBuffer buf) {
105: int len = buf.size();
106: /***/
107: if (len + size > buffer.length) {
108: byte[] newBuf = new byte[len * 2 + size];
109: System.arraycopy(buffer, 0, newBuf, len, size);
110: System.arraycopy(buf.buffer, 0, newBuf, 0, len);
111: buffer = newBuf;
112: } else {
113: System.arraycopy(buffer, 0, buffer, len, size);
114: System.arraycopy(buf.buffer, 0, buffer, 0, len);
115: }
116: /***/
117: size += len;
118: }
119:
120: public void append(ByteBuffer buf) {
121: add(buf.buffer, 0, buf.size());
122: }
123:
124: public void set(byte b, int offset) {
125: buffer[offset] = b;
126: }
127:
128: public void copyTo(int offset, byte[] dest, int off, int len) {
129: System.arraycopy(buffer, offset, dest, off, len);
130: }
131:
132: public void setSize(int size) {
133: this .size = size;
134: }
135:
136: public int size() {
137: return size;
138: }
139:
140: public void writeTo(OutputStream out) throws IOException {
141: out.write(buffer, 0, size);
142: }
143: }
|