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

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