duke@1: /* ohair@554: * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: import java.io.*; duke@1: duke@1: /** A byte buffer is a flexible array which grows when elements are duke@1: * appended. There are also methods to append names to byte buffers duke@1: * and to convert byte buffers to names. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class ByteBuffer { duke@1: duke@1: /** An array holding the bytes in this buffer; can be grown. duke@1: */ duke@1: public byte[] elems; duke@1: duke@1: /** The current number of defined bytes in this buffer. duke@1: */ duke@1: public int length; duke@1: duke@1: /** Create a new byte buffer. duke@1: */ duke@1: public ByteBuffer() { duke@1: this(64); duke@1: } duke@1: duke@1: /** Create a new byte buffer with an initial elements array duke@1: * of given size. duke@1: */ duke@1: public ByteBuffer(int initialSize) { duke@1: elems = new byte[initialSize]; duke@1: length = 0; duke@1: } duke@1: duke@1: private void copy(int size) { duke@1: byte[] newelems = new byte[size]; duke@1: System.arraycopy(elems, 0, newelems, 0, elems.length); duke@1: elems = newelems; duke@1: } duke@1: duke@1: /** Append byte to this buffer. duke@1: */ duke@1: public void appendByte(int b) { duke@1: if (length >= elems.length) copy(elems.length * 2); duke@1: elems[length++] = (byte)b; duke@1: } duke@1: duke@1: /** Append `len' bytes from byte array, duke@1: * starting at given `start' offset. duke@1: */ duke@1: public void appendBytes(byte[] bs, int start, int len) { duke@1: while (length + len > elems.length) copy(elems.length * 2); duke@1: System.arraycopy(bs, start, elems, length, len); duke@1: length += len; duke@1: } duke@1: duke@1: /** Append all bytes from given byte array. duke@1: */ duke@1: public void appendBytes(byte[] bs) { duke@1: appendBytes(bs, 0, bs.length); duke@1: } duke@1: duke@1: /** Append a character as a two byte number. duke@1: */ duke@1: public void appendChar(int x) { duke@1: while (length + 1 >= elems.length) copy(elems.length * 2); duke@1: elems[length ] = (byte)((x >> 8) & 0xFF); duke@1: elems[length+1] = (byte)((x ) & 0xFF); duke@1: length = length + 2; duke@1: } duke@1: duke@1: /** Append an integer as a four byte number. duke@1: */ duke@1: public void appendInt(int x) { duke@1: while (length + 3 >= elems.length) copy(elems.length * 2); duke@1: elems[length ] = (byte)((x >> 24) & 0xFF); duke@1: elems[length+1] = (byte)((x >> 16) & 0xFF); duke@1: elems[length+2] = (byte)((x >> 8) & 0xFF); duke@1: elems[length+3] = (byte)((x ) & 0xFF); duke@1: length = length + 4; duke@1: } duke@1: duke@1: /** Append a long as an eight byte number. duke@1: */ duke@1: public void appendLong(long x) { duke@1: ByteArrayOutputStream buffer = new ByteArrayOutputStream(8); duke@1: DataOutputStream bufout = new DataOutputStream(buffer); duke@1: try { duke@1: bufout.writeLong(x); duke@1: appendBytes(buffer.toByteArray(), 0, 8); duke@1: } catch (IOException e) { duke@1: throw new AssertionError("write"); duke@1: } duke@1: } duke@1: duke@1: /** Append a float as a four byte number. duke@1: */ duke@1: public void appendFloat(float x) { duke@1: ByteArrayOutputStream buffer = new ByteArrayOutputStream(4); duke@1: DataOutputStream bufout = new DataOutputStream(buffer); duke@1: try { duke@1: bufout.writeFloat(x); duke@1: appendBytes(buffer.toByteArray(), 0, 4); duke@1: } catch (IOException e) { duke@1: throw new AssertionError("write"); duke@1: } duke@1: } duke@1: duke@1: /** Append a double as a eight byte number. duke@1: */ duke@1: public void appendDouble(double x) { duke@1: ByteArrayOutputStream buffer = new ByteArrayOutputStream(8); duke@1: DataOutputStream bufout = new DataOutputStream(buffer); duke@1: try { duke@1: bufout.writeDouble(x); duke@1: appendBytes(buffer.toByteArray(), 0, 8); duke@1: } catch (IOException e) { duke@1: throw new AssertionError("write"); duke@1: } duke@1: } duke@1: duke@1: /** Append a name. duke@1: */ duke@1: public void appendName(Name name) { jjg@113: appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength()); duke@1: } duke@1: duke@1: /** Reset to zero length. duke@1: */ duke@1: public void reset() { duke@1: length = 0; duke@1: } duke@1: duke@1: /** Convert contents to name. duke@1: */ jjg@113: public Name toName(Names names) { duke@1: return names.fromUtf(elems, 0, length); duke@1: } duke@1: }