0N/A/*
553N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.util;
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/** A byte buffer is a flexible array which grows when elements are
0N/A * appended. There are also methods to append names to byte buffers
0N/A * and to convert byte buffers to names.
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A */
0N/Apublic class ByteBuffer {
0N/A
0N/A /** An array holding the bytes in this buffer; can be grown.
0N/A */
0N/A public byte[] elems;
0N/A
0N/A /** The current number of defined bytes in this buffer.
0N/A */
0N/A public int length;
0N/A
0N/A /** Create a new byte buffer.
0N/A */
0N/A public ByteBuffer() {
0N/A this(64);
0N/A }
0N/A
0N/A /** Create a new byte buffer with an initial elements array
0N/A * of given size.
0N/A */
0N/A public ByteBuffer(int initialSize) {
0N/A elems = new byte[initialSize];
0N/A length = 0;
0N/A }
0N/A
0N/A private void copy(int size) {
0N/A byte[] newelems = new byte[size];
0N/A System.arraycopy(elems, 0, newelems, 0, elems.length);
0N/A elems = newelems;
0N/A }
0N/A
0N/A /** Append byte to this buffer.
0N/A */
0N/A public void appendByte(int b) {
0N/A if (length >= elems.length) copy(elems.length * 2);
0N/A elems[length++] = (byte)b;
0N/A }
0N/A
0N/A /** Append `len' bytes from byte array,
0N/A * starting at given `start' offset.
0N/A */
0N/A public void appendBytes(byte[] bs, int start, int len) {
0N/A while (length + len > elems.length) copy(elems.length * 2);
0N/A System.arraycopy(bs, start, elems, length, len);
0N/A length += len;
0N/A }
0N/A
0N/A /** Append all bytes from given byte array.
0N/A */
0N/A public void appendBytes(byte[] bs) {
0N/A appendBytes(bs, 0, bs.length);
0N/A }
0N/A
0N/A /** Append a character as a two byte number.
0N/A */
0N/A public void appendChar(int x) {
0N/A while (length + 1 >= elems.length) copy(elems.length * 2);
0N/A elems[length ] = (byte)((x >> 8) & 0xFF);
0N/A elems[length+1] = (byte)((x ) & 0xFF);
0N/A length = length + 2;
0N/A }
0N/A
0N/A /** Append an integer as a four byte number.
0N/A */
0N/A public void appendInt(int x) {
0N/A while (length + 3 >= elems.length) copy(elems.length * 2);
0N/A elems[length ] = (byte)((x >> 24) & 0xFF);
0N/A elems[length+1] = (byte)((x >> 16) & 0xFF);
0N/A elems[length+2] = (byte)((x >> 8) & 0xFF);
0N/A elems[length+3] = (byte)((x ) & 0xFF);
0N/A length = length + 4;
0N/A }
0N/A
0N/A /** Append a long as an eight byte number.
0N/A */
0N/A public void appendLong(long x) {
0N/A ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
0N/A DataOutputStream bufout = new DataOutputStream(buffer);
0N/A try {
0N/A bufout.writeLong(x);
0N/A appendBytes(buffer.toByteArray(), 0, 8);
0N/A } catch (IOException e) {
0N/A throw new AssertionError("write");
0N/A }
0N/A }
0N/A
0N/A /** Append a float as a four byte number.
0N/A */
0N/A public void appendFloat(float x) {
0N/A ByteArrayOutputStream buffer = new ByteArrayOutputStream(4);
0N/A DataOutputStream bufout = new DataOutputStream(buffer);
0N/A try {
0N/A bufout.writeFloat(x);
0N/A appendBytes(buffer.toByteArray(), 0, 4);
0N/A } catch (IOException e) {
0N/A throw new AssertionError("write");
0N/A }
0N/A }
0N/A
0N/A /** Append a double as a eight byte number.
0N/A */
0N/A public void appendDouble(double x) {
0N/A ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
0N/A DataOutputStream bufout = new DataOutputStream(buffer);
0N/A try {
0N/A bufout.writeDouble(x);
0N/A appendBytes(buffer.toByteArray(), 0, 8);
0N/A } catch (IOException e) {
0N/A throw new AssertionError("write");
0N/A }
0N/A }
0N/A
0N/A /** Append a name.
0N/A */
0N/A public void appendName(Name name) {
112N/A appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength());
0N/A }
0N/A
0N/A /** Reset to zero length.
0N/A */
0N/A public void reset() {
0N/A length = 0;
0N/A }
0N/A
0N/A /** Convert contents to name.
0N/A */
112N/A public Name toName(Names names) {
0N/A return names.fromUtf(elems, 0, length);
0N/A }
0N/A}