langtools/src/share/classes/com/sun/tools/javac/util/ByteBuffer.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 14049 3207422a0f9b
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
/** A byte buffer is a flexible array which grows when elements are
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 *  appended. There are also methods to append names to byte buffers
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 *  and to convert byte buffers to names.
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    34
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    35
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
public class ByteBuffer {
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    /** An array holding the bytes in this buffer; can be grown.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    public byte[] elems;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    /** The current number of defined bytes in this buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    public int length;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    /** Create a new byte buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    public ByteBuffer() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        this(64);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /** Create a new byte buffer with an initial elements array
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     *  of given size.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public ByteBuffer(int initialSize) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        elems = new byte[initialSize];
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        length = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private void copy(int size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        byte[] newelems = new byte[size];
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        System.arraycopy(elems, 0, newelems, 0, elems.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        elems = newelems;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** Append byte to this buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public void appendByte(int b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        if (length >= elems.length) copy(elems.length * 2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        elems[length++] = (byte)b;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /** Append `len' bytes from byte array,
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     *  starting at given `start' offset.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public void appendBytes(byte[] bs, int start, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        while (length + len > elems.length) copy(elems.length * 2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        System.arraycopy(bs, start, elems, length, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        length += len;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** Append all bytes from given byte array.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public void appendBytes(byte[] bs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        appendBytes(bs, 0, bs.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /** Append a character as a two byte number.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public void appendChar(int x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        while (length + 1 >= elems.length) copy(elems.length * 2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        elems[length  ] = (byte)((x >>  8) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        elems[length+1] = (byte)((x      ) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        length = length + 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /** Append an integer as a four byte number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    public void appendInt(int x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        while (length + 3 >= elems.length) copy(elems.length * 2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        elems[length  ] = (byte)((x >> 24) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        elems[length+1] = (byte)((x >> 16) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        elems[length+2] = (byte)((x >>  8) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        elems[length+3] = (byte)((x      ) & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        length = length + 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /** Append a long as an eight byte number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public void appendLong(long x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        DataOutputStream bufout = new DataOutputStream(buffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            bufout.writeLong(x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            appendBytes(buffer.toByteArray(), 0, 8);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            throw new AssertionError("write");
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /** Append a float as a four byte number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    public void appendFloat(float x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(4);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        DataOutputStream bufout = new DataOutputStream(buffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            bufout.writeFloat(x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            appendBytes(buffer.toByteArray(), 0, 4);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            throw new AssertionError("write");
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    /** Append a double as a eight byte number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    public void appendDouble(double x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        DataOutputStream bufout = new DataOutputStream(buffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            bufout.writeDouble(x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            appendBytes(buffer.toByteArray(), 0, 8);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            throw new AssertionError("write");
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    /** Append a name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    public void appendName(Name name) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   153
        appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /** Reset to zero length.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    public void reset() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        length = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    /** Convert contents to name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   164
    public Name toName(Names names) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        return names.fromUtf(elems, 0, length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
}