ByteBuffer.java revision 608:7e06bf1dcb09
1139825Simp/*
21541Srgrimes * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.  Oracle designates this
81541Srgrimes * particular file as subject to the "Classpath" exception as provided
91541Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101541Srgrimes *
111541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151541Srgrimes * accompanied this code).
161541Srgrimes *
171541Srgrimes * You should have received a copy of the GNU General Public License version
181541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201541Srgrimes *
211541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221541Srgrimes * or visit www.oracle.com if you need additional information or have any
231541Srgrimes * questions.
241541Srgrimes */
251541Srgrimes
261541Srgrimespackage com.sun.corba.se.impl.ior ;
271541Srgrimes
281541Srgrimes
291541Srgrimespublic class ByteBuffer {
301541Srgrimes    /**
311541Srgrimes     * The array buffer into which the components of the ByteBuffer are
321817Sdg     * stored. The capacity of the ByteBuffer is the length of this array buffer,
331541Srgrimes     * and is at least large enough to contain all the ByteBuffer's elements.<p>
341541Srgrimes     *
351541Srgrimes     * Any array elements following the last element in the ByteBuffer are 0.
361541Srgrimes     */
371541Srgrimes    protected byte elementData[];
381541Srgrimes
395455Sdg    /**
401541Srgrimes     * The number of valid components in this <tt>ByteBuffer</tt> object.
411541Srgrimes     * Components <tt>elementData[0]</tt> through
421541Srgrimes     * <tt>elementData[elementCount-1]</tt> are the actual items.
431541Srgrimes     *
441541Srgrimes     * @serial
455455Sdg     */
465455Sdg    protected int elementCount;
475455Sdg
481541Srgrimes    /**
495455Sdg     * The amount by which the capacity of the ByteBuffer is automatically
501541Srgrimes     * incremented when its size becomes greater than its capacity.  If
511541Srgrimes     * the capacity increment is less than or equal to zero, the capacity
521541Srgrimes     * of the ByteBuffer is doubled each time it needs to grow.
531541Srgrimes     *
541541Srgrimes     * @serial
551541Srgrimes     */
561541Srgrimes    protected int capacityIncrement;
571541Srgrimes
581541Srgrimes    /**
591817Sdg     * Constructs an empty ByteBuffer with the specified initial capacity and
6050477Speter     * capacity increment.
611541Srgrimes     *
621541Srgrimes     * @param   initialCapacity     the initial capacity of the ByteBuffer.
631541Srgrimes     * @param   capacityIncrement   the amount by which the capacity is
641541Srgrimes     *                              increased when the ByteBuffer overflows.
651541Srgrimes     * @exception IllegalArgumentException if the specified initial capacity
661541Srgrimes     *               is negative
671541Srgrimes     */
681541Srgrimes    public ByteBuffer(int initialCapacity, int capacityIncrement) {
691541Srgrimes        super();
706816Sdg        if (initialCapacity < 0)
7138517Sdfr            throw new IllegalArgumentException("Illegal Capacity: "+
721541Srgrimes                                               initialCapacity);
731541Srgrimes        this.elementData = new byte[initialCapacity];
741541Srgrimes        this.capacityIncrement = capacityIncrement;
751541Srgrimes    }
761541Srgrimes
771541Srgrimes    /**
781541Srgrimes     * Constructs an empty ByteBuffer with the specified initial capacity and
791541Srgrimes     * with its capacity increment equal to zero.
801541Srgrimes     *
811541Srgrimes     * @param   initialCapacity   the initial capacity of the ByteBuffer.
821541Srgrimes     * @exception IllegalArgumentException if the specified initial capacity
831541Srgrimes     *               is negative
841541Srgrimes     */
851541Srgrimes    public ByteBuffer(int initialCapacity) {
861541Srgrimes        this(initialCapacity, 0);
871541Srgrimes    }
881541Srgrimes
891541Srgrimes    /**
901541Srgrimes     * Constructs an empty ByteBuffer so that its internal data array
911541Srgrimes     * has size <tt>10</tt> and its standard capacity increment is
921541Srgrimes     * zero.
931541Srgrimes     */
941541Srgrimes    public ByteBuffer() {
9546349Salc        this(200);
9646349Salc    }
9746349Salc
9846349Salc    /**
991541Srgrimes     * Trims the capacity of this ByteBuffer to be the ByteBuffer's current
1001541Srgrimes     * size. If the capacity of this cector is larger than its current
10160938Sjake     * size, then the capacity is changed to equal the size by replacing
1021541Srgrimes     * its internal data array, kept in the field <tt>elementData</tt>,
1031541Srgrimes     * with a smaller one. An application can use this operation to
10460938Sjake     * minimize the storage of a ByteBuffer.
10560938Sjake     */
106105407Sdillon    public void trimToSize() {
107105407Sdillon        int oldCapacity = elementData.length;
1081541Srgrimes        if (elementCount < oldCapacity) {
10942957Sdillon            byte oldData[] = elementData;
11012767Sdyson            elementData = new byte[elementCount];
111121511Salc            System.arraycopy(oldData, 0, elementData, 0, elementCount);
11260755Speter        }
11318169Sdyson    }
11418169Sdyson
11518169Sdyson    /**
1165455Sdg     * This implements the unsynchronized semantics of ensureCapacity.
117134184Smarcel     * Synchronized methods in this class can internally call this
1185455Sdg     * method for ensuring capacity without incurring the cost of an
119161125Salc     * extra synchronization.
12013490Sdyson     *
121139338Salc     * @see java.util.ByteBuffer#ensureCapacity(int)
12213490Sdyson     */
12313490Sdyson    private void ensureCapacityHelper(int minCapacity) {
12436735Sdfr        int oldCapacity = elementData.length;
125121511Salc        if (minCapacity > oldCapacity) {
12613490Sdyson            byte oldData[] = elementData;
12736735Sdfr            int newCapacity = (capacityIncrement > 0) ?
128121511Salc                (oldCapacity + capacityIncrement) : (oldCapacity * 2);
12936735Sdfr            if (newCapacity < minCapacity) {
130119354Smarcel                newCapacity = minCapacity;
131121511Salc            }
132119354Smarcel            elementData = new byte[newCapacity];
133119354Smarcel            System.arraycopy(oldData, 0, elementData, 0, elementCount);
134121511Salc        }
135119354Smarcel    }
13636735Sdfr
1371541Srgrimes    /**
1381541Srgrimes     * Returns the current capacity of this ByteBuffer.
139161125Salc     *
140161125Salc     * @return  the current capacity (the length of its internal
141161125Salc     *          data arary, kept in the field <tt>elementData</tt>
142161125Salc     *          of this ByteBuffer.
143161125Salc     */
144161125Salc    public int capacity() {
145161125Salc        return elementData.length;
146161125Salc    }
147161125Salc
148119468Smarcel    /**
149119468Smarcel     * Returns the number of components in this ByteBuffer.
150119468Smarcel     *
151119468Smarcel     * @return  the number of components in this ByteBuffer.
152119468Smarcel     */
153119468Smarcel    public int size() {
154119468Smarcel        return elementCount;
155153940Snetchild    }
156153940Snetchild
157153940Snetchild    /**
158153940Snetchild     * Tests if this ByteBuffer has no components.
159153940Snetchild     *
160153940Snetchild     * @return  <code>true</code> if and only if this ByteBuffer has
161153940Snetchild     *          no components, that is, its size is zero;
162153940Snetchild     *          <code>false</code> otherwise.
163153940Snetchild     */
164153940Snetchild    public boolean isEmpty() {
165153940Snetchild        return elementCount == 0;
166153940Snetchild    }
167153940Snetchild
168153940Snetchild    public void append(byte value)
169153940Snetchild    {
17018169Sdyson        ensureCapacityHelper(elementCount + 1);
171153940Snetchild        elementData[elementCount++] = value;
172153940Snetchild    }
17362568Sjhb
174153940Snetchild    public void append( int value )
175153940Snetchild    {
176153940Snetchild        ensureCapacityHelper(elementCount + 4);
17736326Sdyson        doAppend( value ) ;
178153940Snetchild    }
179153940Snetchild
18018169Sdyson    private void doAppend( int value )
181153940Snetchild    {
182153940Snetchild        int current = value ;
183153940Snetchild        for (int ctr=0; ctr<4; ctr++) {
18418169Sdyson            elementData[elementCount+ctr] = (byte)(current & 255) ;
185153940Snetchild            current = current >> 8 ;
186153940Snetchild        }
187153940Snetchild        elementCount += 4 ;
18818169Sdyson    }
18952647Salc
19052647Salc    public void append( String value )
19118169Sdyson    {
19249326Salc        byte[] data = value.getBytes() ;
19352647Salc        ensureCapacityHelper( elementCount + data.length + 4 ) ;
19418169Sdyson        doAppend( data.length ) ;
195153940Snetchild        System.arraycopy( data, 0, elementData, elementCount, data.length ) ;
196153940Snetchild        elementCount += data.length ;
197153940Snetchild    }
198153940Snetchild
199153940Snetchild    /**
200153940Snetchild     * Returns an array containing all of the elements in this ByteBuffer
201153940Snetchild     * in the correct order.
202153940Snetchild     *
203153940Snetchild     * @since 1.2
204153940Snetchild     */
205153940Snetchild    public byte[] toArray() {
206153940Snetchild        return elementData ;
207153940Snetchild    }
208153940Snetchild}
20999416Salc