corba/src/java.corba/share/classes/com/sun/corba/se/impl/ior/ByteBuffer.java
author avstepan
Sun, 13 Sep 2015 23:31:47 +0300
changeset 32688 936c391804a5
parent 25862 a5e25d68f971
permissions -rw-r--r--
8133650: replace some <tt> tags (obsolete in html5) in CORBA docs Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.ior ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
public class ByteBuffer {
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
     * The array buffer into which the components of the ByteBuffer are
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
     * stored. The capacity of the ByteBuffer is the length of this array buffer,
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
     * and is at least large enough to contain all the ByteBuffer's elements.<p>
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
     * Any array elements following the last element in the ByteBuffer are 0.
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
    protected byte elementData[];
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
    /**
32688
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
    40
     * The number of valid components in this {@code ByteBuffer} object.
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
    41
     * Components {@code elementData[0]} through
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
    42
     * {@code elementData[elementCount-1]} are the actual items.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
     * @serial
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    protected int elementCount;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
     * The amount by which the capacity of the ByteBuffer is automatically
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
     * incremented when its size becomes greater than its capacity.  If
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
     * the capacity increment is less than or equal to zero, the capacity
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
     * of the ByteBuffer is doubled each time it needs to grow.
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
     * @serial
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    protected int capacityIncrement;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
     * Constructs an empty ByteBuffer with the specified initial capacity and
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
     * capacity increment.
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
     * @param   initialCapacity     the initial capacity of the ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
     * @param   capacityIncrement   the amount by which the capacity is
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
     *                              increased when the ByteBuffer overflows.
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
     * @exception IllegalArgumentException if the specified initial capacity
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
     *               is negative
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    public ByteBuffer(int initialCapacity, int capacityIncrement) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
        super();
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
        if (initialCapacity < 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
            throw new IllegalArgumentException("Illegal Capacity: "+
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
                                               initialCapacity);
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        this.elementData = new byte[initialCapacity];
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        this.capacityIncrement = capacityIncrement;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
     * Constructs an empty ByteBuffer with the specified initial capacity and
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
     * with its capacity increment equal to zero.
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
     * @param   initialCapacity   the initial capacity of the ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
     * @exception IllegalArgumentException if the specified initial capacity
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
     *               is negative
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
    public ByteBuffer(int initialCapacity) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
        this(initialCapacity, 0);
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
     * Constructs an empty ByteBuffer so that its internal data array
32688
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
    91
     * has size {@code 10} and its standard capacity increment is
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
     * zero.
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
    public ByteBuffer() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
        this(200);
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
     * Trims the capacity of this ByteBuffer to be the ByteBuffer's current
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
     * size. If the capacity of this cector is larger than its current
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
     * size, then the capacity is changed to equal the size by replacing
32688
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
   102
     * its internal data array, kept in the field {@code elementData},
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
     * with a smaller one. An application can use this operation to
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
     * minimize the storage of a ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
    public void trimToSize() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
        int oldCapacity = elementData.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        if (elementCount < oldCapacity) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            byte oldData[] = elementData;
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
            elementData = new byte[elementCount];
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
            System.arraycopy(oldData, 0, elementData, 0, elementCount);
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
     * This implements the unsynchronized semantics of ensureCapacity.
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
     * Synchronized methods in this class can internally call this
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
     * method for ensuring capacity without incurring the cost of an
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
     * extra synchronization.
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
     * @see java.util.ByteBuffer#ensureCapacity(int)
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
    private void ensureCapacityHelper(int minCapacity) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
        int oldCapacity = elementData.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
        if (minCapacity > oldCapacity) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
            byte oldData[] = elementData;
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
            int newCapacity = (capacityIncrement > 0) ?
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
                (oldCapacity + capacityIncrement) : (oldCapacity * 2);
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
            if (newCapacity < minCapacity) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
                newCapacity = minCapacity;
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
            elementData = new byte[newCapacity];
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
            System.arraycopy(oldData, 0, elementData, 0, elementCount);
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
     * Returns the current capacity of this ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
     * @return  the current capacity (the length of its internal
32688
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
   141
     *          data arary, kept in the field {@code elementData}
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
     *          of this ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
    public int capacity() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
        return elementData.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
     * Returns the number of components in this ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
     * @return  the number of components in this ByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
    public int size() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
        return elementCount;
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
     * Tests if this ByteBuffer has no components.
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
     *
32688
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
   160
     * @return  {@code true} if and only if this ByteBuffer has
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
     *          no components, that is, its size is zero;
32688
936c391804a5 8133650: replace some <tt> tags (obsolete in html5) in CORBA docs
avstepan
parents: 25862
diff changeset
   162
     *          {@code false} otherwise.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
    public boolean isEmpty() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
        return elementCount == 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
    public void append(byte value)
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
        ensureCapacityHelper(elementCount + 1);
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
        elementData[elementCount++] = value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
    public void append( int value )
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
        ensureCapacityHelper(elementCount + 4);
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
        doAppend( value ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
    private void doAppend( int value )
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
        int current = value ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        for (int ctr=0; ctr<4; ctr++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
            elementData[elementCount+ctr] = (byte)(current & 255) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
            current = current >> 8 ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
        elementCount += 4 ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
    public void append( String value )
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
        byte[] data = value.getBytes() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        ensureCapacityHelper( elementCount + data.length + 4 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
        doAppend( data.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
        System.arraycopy( data, 0, elementData, elementCount, data.length ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
        elementCount += data.length ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
     * Returns an array containing all of the elements in this ByteBuffer
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
     * in the correct order.
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
     * @since 1.2
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
    public byte[] toArray() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
        return elementData ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
}