1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/ByteBuffer.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,167 @@ 1.4 +/* 1.5 + * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.util; 1.30 + 1.31 +import java.io.*; 1.32 + 1.33 +/** A byte buffer is a flexible array which grows when elements are 1.34 + * appended. There are also methods to append names to byte buffers 1.35 + * and to convert byte buffers to names. 1.36 + * 1.37 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.38 + * you write code that depends on this, you do so at your own risk. 1.39 + * This code and its internal interfaces are subject to change or 1.40 + * deletion without notice.</b> 1.41 + */ 1.42 +public class ByteBuffer { 1.43 + 1.44 + /** An array holding the bytes in this buffer; can be grown. 1.45 + */ 1.46 + public byte[] elems; 1.47 + 1.48 + /** The current number of defined bytes in this buffer. 1.49 + */ 1.50 + public int length; 1.51 + 1.52 + /** Create a new byte buffer. 1.53 + */ 1.54 + public ByteBuffer() { 1.55 + this(64); 1.56 + } 1.57 + 1.58 + /** Create a new byte buffer with an initial elements array 1.59 + * of given size. 1.60 + */ 1.61 + public ByteBuffer(int initialSize) { 1.62 + elems = new byte[initialSize]; 1.63 + length = 0; 1.64 + } 1.65 + 1.66 + private void copy(int size) { 1.67 + byte[] newelems = new byte[size]; 1.68 + System.arraycopy(elems, 0, newelems, 0, elems.length); 1.69 + elems = newelems; 1.70 + } 1.71 + 1.72 + /** Append byte to this buffer. 1.73 + */ 1.74 + public void appendByte(int b) { 1.75 + if (length >= elems.length) copy(elems.length * 2); 1.76 + elems[length++] = (byte)b; 1.77 + } 1.78 + 1.79 + /** Append `len' bytes from byte array, 1.80 + * starting at given `start' offset. 1.81 + */ 1.82 + public void appendBytes(byte[] bs, int start, int len) { 1.83 + while (length + len > elems.length) copy(elems.length * 2); 1.84 + System.arraycopy(bs, start, elems, length, len); 1.85 + length += len; 1.86 + } 1.87 + 1.88 + /** Append all bytes from given byte array. 1.89 + */ 1.90 + public void appendBytes(byte[] bs) { 1.91 + appendBytes(bs, 0, bs.length); 1.92 + } 1.93 + 1.94 + /** Append a character as a two byte number. 1.95 + */ 1.96 + public void appendChar(int x) { 1.97 + while (length + 1 >= elems.length) copy(elems.length * 2); 1.98 + elems[length ] = (byte)((x >> 8) & 0xFF); 1.99 + elems[length+1] = (byte)((x ) & 0xFF); 1.100 + length = length + 2; 1.101 + } 1.102 + 1.103 + /** Append an integer as a four byte number. 1.104 + */ 1.105 + public void appendInt(int x) { 1.106 + while (length + 3 >= elems.length) copy(elems.length * 2); 1.107 + elems[length ] = (byte)((x >> 24) & 0xFF); 1.108 + elems[length+1] = (byte)((x >> 16) & 0xFF); 1.109 + elems[length+2] = (byte)((x >> 8) & 0xFF); 1.110 + elems[length+3] = (byte)((x ) & 0xFF); 1.111 + length = length + 4; 1.112 + } 1.113 + 1.114 + /** Append a long as an eight byte number. 1.115 + */ 1.116 + public void appendLong(long x) { 1.117 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(8); 1.118 + DataOutputStream bufout = new DataOutputStream(buffer); 1.119 + try { 1.120 + bufout.writeLong(x); 1.121 + appendBytes(buffer.toByteArray(), 0, 8); 1.122 + } catch (IOException e) { 1.123 + throw new AssertionError("write"); 1.124 + } 1.125 + } 1.126 + 1.127 + /** Append a float as a four byte number. 1.128 + */ 1.129 + public void appendFloat(float x) { 1.130 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(4); 1.131 + DataOutputStream bufout = new DataOutputStream(buffer); 1.132 + try { 1.133 + bufout.writeFloat(x); 1.134 + appendBytes(buffer.toByteArray(), 0, 4); 1.135 + } catch (IOException e) { 1.136 + throw new AssertionError("write"); 1.137 + } 1.138 + } 1.139 + 1.140 + /** Append a double as a eight byte number. 1.141 + */ 1.142 + public void appendDouble(double x) { 1.143 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(8); 1.144 + DataOutputStream bufout = new DataOutputStream(buffer); 1.145 + try { 1.146 + bufout.writeDouble(x); 1.147 + appendBytes(buffer.toByteArray(), 0, 8); 1.148 + } catch (IOException e) { 1.149 + throw new AssertionError("write"); 1.150 + } 1.151 + } 1.152 + 1.153 + /** Append a name. 1.154 + */ 1.155 + public void appendName(Name name) { 1.156 + appendBytes(name.table.names, name.index, name.len); 1.157 + } 1.158 + 1.159 + /** Reset to zero length. 1.160 + */ 1.161 + public void reset() { 1.162 + length = 0; 1.163 + } 1.164 + 1.165 + /** Convert contents to name. 1.166 + */ 1.167 + public Name toName(Name.Table names) { 1.168 + return names.fromUtf(elems, 0, length); 1.169 + } 1.170 +}