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 Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,161 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * 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 supported API. 1.38 + * If 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 + /** Append byte to this buffer. 1.67 + */ 1.68 + public void appendByte(int b) { 1.69 + elems = ArrayUtils.ensureCapacity(elems, length); 1.70 + elems[length++] = (byte)b; 1.71 + } 1.72 + 1.73 + /** Append `len' bytes from byte array, 1.74 + * starting at given `start' offset. 1.75 + */ 1.76 + public void appendBytes(byte[] bs, int start, int len) { 1.77 + elems = ArrayUtils.ensureCapacity(elems, length + len); 1.78 + System.arraycopy(bs, start, elems, length, len); 1.79 + length += len; 1.80 + } 1.81 + 1.82 + /** Append all bytes from given byte array. 1.83 + */ 1.84 + public void appendBytes(byte[] bs) { 1.85 + appendBytes(bs, 0, bs.length); 1.86 + } 1.87 + 1.88 + /** Append a character as a two byte number. 1.89 + */ 1.90 + public void appendChar(int x) { 1.91 + elems = ArrayUtils.ensureCapacity(elems, length + 1); 1.92 + elems[length ] = (byte)((x >> 8) & 0xFF); 1.93 + elems[length+1] = (byte)((x ) & 0xFF); 1.94 + length = length + 2; 1.95 + } 1.96 + 1.97 + /** Append an integer as a four byte number. 1.98 + */ 1.99 + public void appendInt(int x) { 1.100 + elems = ArrayUtils.ensureCapacity(elems, length + 3); 1.101 + elems[length ] = (byte)((x >> 24) & 0xFF); 1.102 + elems[length+1] = (byte)((x >> 16) & 0xFF); 1.103 + elems[length+2] = (byte)((x >> 8) & 0xFF); 1.104 + elems[length+3] = (byte)((x ) & 0xFF); 1.105 + length = length + 4; 1.106 + } 1.107 + 1.108 + /** Append a long as an eight byte number. 1.109 + */ 1.110 + public void appendLong(long x) { 1.111 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(8); 1.112 + DataOutputStream bufout = new DataOutputStream(buffer); 1.113 + try { 1.114 + bufout.writeLong(x); 1.115 + appendBytes(buffer.toByteArray(), 0, 8); 1.116 + } catch (IOException e) { 1.117 + throw new AssertionError("write"); 1.118 + } 1.119 + } 1.120 + 1.121 + /** Append a float as a four byte number. 1.122 + */ 1.123 + public void appendFloat(float x) { 1.124 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(4); 1.125 + DataOutputStream bufout = new DataOutputStream(buffer); 1.126 + try { 1.127 + bufout.writeFloat(x); 1.128 + appendBytes(buffer.toByteArray(), 0, 4); 1.129 + } catch (IOException e) { 1.130 + throw new AssertionError("write"); 1.131 + } 1.132 + } 1.133 + 1.134 + /** Append a double as a eight byte number. 1.135 + */ 1.136 + public void appendDouble(double x) { 1.137 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(8); 1.138 + DataOutputStream bufout = new DataOutputStream(buffer); 1.139 + try { 1.140 + bufout.writeDouble(x); 1.141 + appendBytes(buffer.toByteArray(), 0, 8); 1.142 + } catch (IOException e) { 1.143 + throw new AssertionError("write"); 1.144 + } 1.145 + } 1.146 + 1.147 + /** Append a name. 1.148 + */ 1.149 + public void appendName(Name name) { 1.150 + appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength()); 1.151 + } 1.152 + 1.153 + /** Reset to zero length. 1.154 + */ 1.155 + public void reset() { 1.156 + length = 0; 1.157 + } 1.158 + 1.159 + /** Convert contents to name. 1.160 + */ 1.161 + public Name toName(Names names) { 1.162 + return names.fromUtf(elems, 0, length); 1.163 + } 1.164 +}