1 /*
   2  * Copyright (c) 2001, 2002, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.ior ;
  27 
  28 
  29 public class ByteBuffer {
  30     /**
  31      * The array buffer into which the components of the ByteBuffer are
  32      * stored. The capacity of the ByteBuffer is the length of this array buffer,
  33      * and is at least large enough to contain all the ByteBuffer's elements.<p>
  34      *
  35      * Any array elements following the last element in the ByteBuffer are 0.
  36      */
  37     protected byte elementData[];
  38 
  39     /**
  40      * The number of valid components in this {@code ByteBuffer} object.
  41      * Components {@code elementData[0]} through
  42      * {@code elementData[elementCount-1]} are the actual items.
  43      *
  44      * @serial
  45      */
  46     protected int elementCount;
  47 
  48     /**
  49      * The amount by which the capacity of the ByteBuffer is automatically
  50      * incremented when its size becomes greater than its capacity.  If
  51      * the capacity increment is less than or equal to zero, the capacity
  52      * of the ByteBuffer is doubled each time it needs to grow.
  53      *
  54      * @serial
  55      */
  56     protected int capacityIncrement;
  57 
  58     /**
  59      * Constructs an empty ByteBuffer with the specified initial capacity and
  60      * capacity increment.
  61      *
  62      * @param   initialCapacity     the initial capacity of the ByteBuffer.
  63      * @param   capacityIncrement   the amount by which the capacity is
  64      *                              increased when the ByteBuffer overflows.
  65      * @exception IllegalArgumentException if the specified initial capacity
  66      *               is negative
  67      */
  68     public ByteBuffer(int initialCapacity, int capacityIncrement) {
  69         super();
  70         if (initialCapacity < 0)
  71             throw new IllegalArgumentException("Illegal Capacity: "+
  72                                                initialCapacity);
  73         this.elementData = new byte[initialCapacity];
  74         this.capacityIncrement = capacityIncrement;
  75     }
  76 
  77     /**
  78      * Constructs an empty ByteBuffer with the specified initial capacity and
  79      * with its capacity increment equal to zero.
  80      *
  81      * @param   initialCapacity   the initial capacity of the ByteBuffer.
  82      * @exception IllegalArgumentException if the specified initial capacity
  83      *               is negative
  84      */
  85     public ByteBuffer(int initialCapacity) {
  86         this(initialCapacity, 0);
  87     }
  88 
  89     /**
  90      * Constructs an empty ByteBuffer so that its internal data array
  91      * has size {@code 10} and its standard capacity increment is
  92      * zero.
  93      */
  94     public ByteBuffer() {
  95         this(200);
  96     }
  97 
  98     /**
  99      * Trims the capacity of this ByteBuffer to be the ByteBuffer's current
 100      * size. If the capacity of this cector is larger than its current
 101      * size, then the capacity is changed to equal the size by replacing
 102      * its internal data array, kept in the field {@code elementData},
 103      * with a smaller one. An application can use this operation to
 104      * minimize the storage of a ByteBuffer.
 105      */
 106     public void trimToSize() {
 107         int oldCapacity = elementData.length;
 108         if (elementCount < oldCapacity) {
 109             byte oldData[] = elementData;
 110             elementData = new byte[elementCount];
 111             System.arraycopy(oldData, 0, elementData, 0, elementCount);
 112         }
 113     }
 114 
 115     /**
 116      * This implements the unsynchronized semantics of ensureCapacity.
 117      * Synchronized methods in this class can internally call this
 118      * method for ensuring capacity without incurring the cost of an
 119      * extra synchronization.
 120      *
 121      * @see java.util.ByteBuffer#ensureCapacity(int)
 122      */
 123     private void ensureCapacityHelper(int minCapacity) {
 124         int oldCapacity = elementData.length;
 125         if (minCapacity > oldCapacity) {
 126             byte oldData[] = elementData;
 127             int newCapacity = (capacityIncrement > 0) ?
 128                 (oldCapacity + capacityIncrement) : (oldCapacity * 2);
 129             if (newCapacity < minCapacity) {
 130                 newCapacity = minCapacity;
 131             }
 132             elementData = new byte[newCapacity];
 133             System.arraycopy(oldData, 0, elementData, 0, elementCount);
 134         }
 135     }
 136 
 137     /**
 138      * Returns the current capacity of this ByteBuffer.
 139      *
 140      * @return  the current capacity (the length of its internal
 141      *          data arary, kept in the field {@code elementData}
 142      *          of this ByteBuffer.
 143      */
 144     public int capacity() {
 145         return elementData.length;
 146     }
 147 
 148     /**
 149      * Returns the number of components in this ByteBuffer.
 150      *
 151      * @return  the number of components in this ByteBuffer.
 152      */
 153     public int size() {
 154         return elementCount;
 155     }
 156 
 157     /**
 158      * Tests if this ByteBuffer has no components.
 159      *
 160      * @return  {@code true} if and only if this ByteBuffer has
 161      *          no components, that is, its size is zero;
 162      *          {@code false} otherwise.
 163      */
 164     public boolean isEmpty() {
 165         return elementCount == 0;
 166     }
 167 
 168     public void append(byte value)
 169     {
 170         ensureCapacityHelper(elementCount + 1);
 171         elementData[elementCount++] = value;
 172     }
 173 
 174     public void append( int value )
 175     {
 176         ensureCapacityHelper(elementCount + 4);
 177         doAppend( value ) ;
 178     }
 179 
 180     private void doAppend( int value )
 181     {
 182         int current = value ;
 183         for (int ctr=0; ctr<4; ctr++) {
 184             elementData[elementCount+ctr] = (byte)(current & 255) ;
 185             current = current >> 8 ;
 186         }
 187         elementCount += 4 ;
 188     }
 189 
 190     public void append( String value )
 191     {
 192         byte[] data = value.getBytes() ;
 193         ensureCapacityHelper( elementCount + data.length + 4 ) ;
 194         doAppend( data.length ) ;
 195         System.arraycopy( data, 0, elementData, elementCount, data.length ) ;
 196         elementCount += data.length ;
 197     }
 198 
 199     /**
 200      * Returns an array containing all of the elements in this ByteBuffer
 201      * in the correct order.
 202      *
 203      * @since 1.2
 204      */
 205     public byte[] toArray() {
 206         return elementData ;
 207     }
 208 }