001: /*
002: * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.util;
027:
028: import java.io.*;
029:
030: /** A byte buffer is a flexible array which grows when elements are
031: * appended. There are also methods to append names to byte buffers
032: * and to convert byte buffers to names.
033: *
034: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
035: * you write code that depends on this, you do so at your own risk.
036: * This code and its internal interfaces are subject to change or
037: * deletion without notice.</b>
038: */
039: @Version("@(#)ByteBuffer.java 1.26 07/05/05")
040: public class ByteBuffer {
041:
042: /** An array holding the bytes in this buffer; can be grown.
043: */
044: public byte[] elems;
045:
046: /** The current number of defined bytes in this buffer.
047: */
048: public int length;
049:
050: /** Create a new byte buffer.
051: */
052: public ByteBuffer() {
053: this (64);
054: }
055:
056: /** Create a new byte buffer with an initial elements array
057: * of given size.
058: */
059: public ByteBuffer(int initialSize) {
060: elems = new byte[initialSize];
061: length = 0;
062: }
063:
064: private void copy(int size) {
065: byte[] newelems = new byte[size];
066: System.arraycopy(elems, 0, newelems, 0, elems.length);
067: elems = newelems;
068: }
069:
070: /** Append byte to this buffer.
071: */
072: public void appendByte(int b) {
073: if (length >= elems.length)
074: copy(elems.length * 2);
075: elems[length++] = (byte) b;
076: }
077:
078: /** Append `len' bytes from byte array,
079: * starting at given `start' offset.
080: */
081: public void appendBytes(byte[] bs, int start, int len) {
082: while (length + len > elems.length)
083: copy(elems.length * 2);
084: System.arraycopy(bs, start, elems, length, len);
085: length += len;
086: }
087:
088: /** Append all bytes from given byte array.
089: */
090: public void appendBytes(byte[] bs) {
091: appendBytes(bs, 0, bs.length);
092: }
093:
094: /** Append a character as a two byte number.
095: */
096: public void appendChar(int x) {
097: while (length + 1 >= elems.length)
098: copy(elems.length * 2);
099: elems[length] = (byte) ((x >> 8) & 0xFF);
100: elems[length + 1] = (byte) ((x) & 0xFF);
101: length = length + 2;
102: }
103:
104: /** Append an integer as a four byte number.
105: */
106: public void appendInt(int x) {
107: while (length + 3 >= elems.length)
108: copy(elems.length * 2);
109: elems[length] = (byte) ((x >> 24) & 0xFF);
110: elems[length + 1] = (byte) ((x >> 16) & 0xFF);
111: elems[length + 2] = (byte) ((x >> 8) & 0xFF);
112: elems[length + 3] = (byte) ((x) & 0xFF);
113: length = length + 4;
114: }
115:
116: /** Append a long as an eight byte number.
117: */
118: public void appendLong(long x) {
119: ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
120: DataOutputStream bufout = new DataOutputStream(buffer);
121: try {
122: bufout.writeLong(x);
123: appendBytes(buffer.toByteArray(), 0, 8);
124: } catch (IOException e) {
125: throw new AssertionError("write");
126: }
127: }
128:
129: /** Append a float as a four byte number.
130: */
131: public void appendFloat(float x) {
132: ByteArrayOutputStream buffer = new ByteArrayOutputStream(4);
133: DataOutputStream bufout = new DataOutputStream(buffer);
134: try {
135: bufout.writeFloat(x);
136: appendBytes(buffer.toByteArray(), 0, 4);
137: } catch (IOException e) {
138: throw new AssertionError("write");
139: }
140: }
141:
142: /** Append a double as a eight byte number.
143: */
144: public void appendDouble(double x) {
145: ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
146: DataOutputStream bufout = new DataOutputStream(buffer);
147: try {
148: bufout.writeDouble(x);
149: appendBytes(buffer.toByteArray(), 0, 8);
150: } catch (IOException e) {
151: throw new AssertionError("write");
152: }
153: }
154:
155: /** Append a name.
156: */
157: public void appendName(Name name) {
158: appendBytes(name.table.names, name.index, name.len);
159: }
160:
161: /** Reset to zero length.
162: */
163: public void reset() {
164: length = 0;
165: }
166:
167: /** Convert contents to name.
168: */
169: public Name toName(Name.Table names) {
170: return names.fromUtf(elems, 0, length);
171: }
172: }
|