src/share/classes/com/sun/tools/javac/util/ByteBuffer.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 113
eff38cc97183
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.util;
    28 import java.io.*;
    30 /** A byte buffer is a flexible array which grows when elements are
    31  *  appended. There are also methods to append names to byte buffers
    32  *  and to convert byte buffers to names.
    33  *
    34  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    35  *  you write code that depends on this, you do so at your own risk.
    36  *  This code and its internal interfaces are subject to change or
    37  *  deletion without notice.</b>
    38  */
    39 public class ByteBuffer {
    41     /** An array holding the bytes in this buffer; can be grown.
    42      */
    43     public byte[] elems;
    45     /** The current number of defined bytes in this buffer.
    46      */
    47     public int length;
    49     /** Create a new byte buffer.
    50      */
    51     public ByteBuffer() {
    52         this(64);
    53     }
    55     /** Create a new byte buffer with an initial elements array
    56      *  of given size.
    57      */
    58     public ByteBuffer(int initialSize) {
    59         elems = new byte[initialSize];
    60         length = 0;
    61     }
    63     private void copy(int size) {
    64         byte[] newelems = new byte[size];
    65         System.arraycopy(elems, 0, newelems, 0, elems.length);
    66         elems = newelems;
    67     }
    69     /** Append byte to this buffer.
    70      */
    71     public void appendByte(int b) {
    72         if (length >= elems.length) copy(elems.length * 2);
    73         elems[length++] = (byte)b;
    74     }
    76     /** Append `len' bytes from byte array,
    77      *  starting at given `start' offset.
    78      */
    79     public void appendBytes(byte[] bs, int start, int len) {
    80         while (length + len > elems.length) copy(elems.length * 2);
    81         System.arraycopy(bs, start, elems, length, len);
    82         length += len;
    83     }
    85     /** Append all bytes from given byte array.
    86      */
    87     public void appendBytes(byte[] bs) {
    88         appendBytes(bs, 0, bs.length);
    89     }
    91     /** Append a character as a two byte number.
    92      */
    93     public void appendChar(int x) {
    94         while (length + 1 >= elems.length) copy(elems.length * 2);
    95         elems[length  ] = (byte)((x >>  8) & 0xFF);
    96         elems[length+1] = (byte)((x      ) & 0xFF);
    97         length = length + 2;
    98     }
   100     /** Append an integer as a four byte number.
   101      */
   102     public void appendInt(int x) {
   103         while (length + 3 >= elems.length) copy(elems.length * 2);
   104         elems[length  ] = (byte)((x >> 24) & 0xFF);
   105         elems[length+1] = (byte)((x >> 16) & 0xFF);
   106         elems[length+2] = (byte)((x >>  8) & 0xFF);
   107         elems[length+3] = (byte)((x      ) & 0xFF);
   108         length = length + 4;
   109     }
   111     /** Append a long as an eight byte number.
   112      */
   113     public void appendLong(long x) {
   114         ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
   115         DataOutputStream bufout = new DataOutputStream(buffer);
   116         try {
   117             bufout.writeLong(x);
   118             appendBytes(buffer.toByteArray(), 0, 8);
   119         } catch (IOException e) {
   120             throw new AssertionError("write");
   121         }
   122     }
   124     /** Append a float as a four byte number.
   125      */
   126     public void appendFloat(float x) {
   127         ByteArrayOutputStream buffer = new ByteArrayOutputStream(4);
   128         DataOutputStream bufout = new DataOutputStream(buffer);
   129         try {
   130             bufout.writeFloat(x);
   131             appendBytes(buffer.toByteArray(), 0, 4);
   132         } catch (IOException e) {
   133             throw new AssertionError("write");
   134         }
   135     }
   137     /** Append a double as a eight byte number.
   138      */
   139     public void appendDouble(double x) {
   140         ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
   141         DataOutputStream bufout = new DataOutputStream(buffer);
   142         try {
   143             bufout.writeDouble(x);
   144             appendBytes(buffer.toByteArray(), 0, 8);
   145         } catch (IOException e) {
   146             throw new AssertionError("write");
   147         }
   148     }
   150     /** Append a name.
   151      */
   152     public void appendName(Name name) {
   153         appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength());
   154     }
   156     /** Reset to zero length.
   157      */
   158     public void reset() {
   159         length = 0;
   160     }
   162     /** Convert contents to name.
   163      */
   164     public Name toName(Names names) {
   165         return names.fromUtf(elems, 0, length);
   166     }
   167 }

mercurial