KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > ior > ByteBuffer


1 /*
2  * @(#)ByteBuffer.java 1.6 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.ior ;
9
10
11 public class ByteBuffer {
12     /**
13      * The array buffer into which the components of the ByteBuffer are
14      * stored. The capacity of the ByteBuffer is the length of this array buffer,
15      * and is at least large enough to contain all the ByteBuffer's elements.<p>
16      *
17      * Any array elements following the last element in the ByteBuffer are 0.
18      */

19     protected byte elementData[];
20
21     /**
22      * The number of valid components in this <tt>ByteBuffer</tt> object.
23      * Components <tt>elementData[0]</tt> through
24      * <tt>elementData[elementCount-1]</tt> are the actual items.
25      *
26      * @serial
27      */

28     protected int elementCount;
29
30     /**
31      * The amount by which the capacity of the ByteBuffer is automatically
32      * incremented when its size becomes greater than its capacity. If
33      * the capacity increment is less than or equal to zero, the capacity
34      * of the ByteBuffer is doubled each time it needs to grow.
35      *
36      * @serial
37      */

38     protected int capacityIncrement;
39
40     /**
41      * Constructs an empty ByteBuffer with the specified initial capacity and
42      * capacity increment.
43      *
44      * @param initialCapacity the initial capacity of the ByteBuffer.
45      * @param capacityIncrement the amount by which the capacity is
46      * increased when the ByteBuffer overflows.
47      * @exception IllegalArgumentException if the specified initial capacity
48      * is negative
49      */

50     public ByteBuffer(int initialCapacity, int capacityIncrement) {
51     super();
52         if (initialCapacity < 0)
53             throw new IllegalArgumentException JavaDoc("Illegal Capacity: "+
54                                                initialCapacity);
55     this.elementData = new byte[initialCapacity];
56     this.capacityIncrement = capacityIncrement;
57     }
58
59     /**
60      * Constructs an empty ByteBuffer with the specified initial capacity and
61      * with its capacity increment equal to zero.
62      *
63      * @param initialCapacity the initial capacity of the ByteBuffer.
64      * @exception IllegalArgumentException if the specified initial capacity
65      * is negative
66      */

67     public ByteBuffer(int initialCapacity) {
68     this(initialCapacity, 0);
69     }
70
71     /**
72      * Constructs an empty ByteBuffer so that its internal data array
73      * has size <tt>10</tt> and its standard capacity increment is
74      * zero.
75      */

76     public ByteBuffer() {
77     this(200);
78     }
79
80     /**
81      * Trims the capacity of this ByteBuffer to be the ByteBuffer's current
82      * size. If the capacity of this cector is larger than its current
83      * size, then the capacity is changed to equal the size by replacing
84      * its internal data array, kept in the field <tt>elementData</tt>,
85      * with a smaller one. An application can use this operation to
86      * minimize the storage of a ByteBuffer.
87      */

88     public void trimToSize() {
89     int oldCapacity = elementData.length;
90     if (elementCount < oldCapacity) {
91         byte oldData[] = elementData;
92         elementData = new byte[elementCount];
93         System.arraycopy(oldData, 0, elementData, 0, elementCount);
94     }
95     }
96
97     /**
98      * This implements the unsynchronized semantics of ensureCapacity.
99      * Synchronized methods in this class can internally call this
100      * method for ensuring capacity without incurring the cost of an
101      * extra synchronization.
102      *
103      * @see java.util.ByteBuffer#ensureCapacity(int)
104      */

105     private void ensureCapacityHelper(int minCapacity) {
106     int oldCapacity = elementData.length;
107     if (minCapacity > oldCapacity) {
108         byte oldData[] = elementData;
109         int newCapacity = (capacityIncrement > 0) ?
110         (oldCapacity + capacityIncrement) : (oldCapacity * 2);
111             if (newCapacity < minCapacity) {
112         newCapacity = minCapacity;
113         }
114         elementData = new byte[newCapacity];
115         System.arraycopy(oldData, 0, elementData, 0, elementCount);
116     }
117     }
118
119     /**
120      * Returns the current capacity of this ByteBuffer.
121      *
122      * @return the current capacity (the length of its internal
123      * data arary, kept in the field <tt>elementData</tt>
124      * of this ByteBuffer.
125      */

126     public int capacity() {
127     return elementData.length;
128     }
129
130     /**
131      * Returns the number of components in this ByteBuffer.
132      *
133      * @return the number of components in this ByteBuffer.
134      */

135     public int size() {
136     return elementCount;
137     }
138
139     /**
140      * Tests if this ByteBuffer has no components.
141      *
142      * @return <code>true</code> if and only if this ByteBuffer has
143      * no components, that is, its size is zero;
144      * <code>false</code> otherwise.
145      */

146     public boolean isEmpty() {
147     return elementCount == 0;
148     }
149
150     public void append(byte value)
151     {
152     ensureCapacityHelper(elementCount + 1);
153     elementData[elementCount++] = value;
154     }
155
156     public void append( int value )
157     {
158     ensureCapacityHelper(elementCount + 4);
159     doAppend( value ) ;
160     }
161
162     private void doAppend( int value )
163     {
164     int current = value ;
165     for (int ctr=0; ctr<4; ctr++) {
166         elementData[elementCount+ctr] = (byte)(current & 255) ;
167         current = current >> 8 ;
168     }
169     elementCount += 4 ;
170     }
171
172     public void append( String JavaDoc value )
173     {
174     byte[] data = value.getBytes() ;
175     ensureCapacityHelper( elementCount + data.length + 4 ) ;
176     doAppend( data.length ) ;
177     System.arraycopy( data, 0, elementData, elementCount, data.length ) ;
178     elementCount += data.length ;
179     }
180
181     /**
182      * Returns an array containing all of the elements in this ByteBuffer
183      * in the correct order.
184      *
185      * @since 1.2
186      */

187     public byte[] toArray() {
188     return elementData ;
189     }
190 }
191
Popular Tags