1. /*
  2. * @(#)ByteBuffer.java 1.4 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.ior ;
  8. public class ByteBuffer {
  9. /**
  10. * The array buffer into which the components of the ByteBuffer are
  11. * stored. The capacity of the ByteBuffer is the length of this array buffer,
  12. * and is at least large enough to contain all the ByteBuffer's elements.<p>
  13. *
  14. * Any array elements following the last element in the ByteBuffer are 0.
  15. */
  16. protected byte elementData[];
  17. /**
  18. * The number of valid components in this <tt>ByteBuffer</tt> object.
  19. * Components <tt>elementData[0]</tt> through
  20. * <tt>elementData[elementCount-1]</tt> are the actual items.
  21. *
  22. * @serial
  23. */
  24. protected int elementCount;
  25. /**
  26. * The amount by which the capacity of the ByteBuffer is automatically
  27. * incremented when its size becomes greater than its capacity. If
  28. * the capacity increment is less than or equal to zero, the capacity
  29. * of the ByteBuffer is doubled each time it needs to grow.
  30. *
  31. * @serial
  32. */
  33. protected int capacityIncrement;
  34. /**
  35. * Constructs an empty ByteBuffer with the specified initial capacity and
  36. * capacity increment.
  37. *
  38. * @param initialCapacity the initial capacity of the ByteBuffer.
  39. * @param capacityIncrement the amount by which the capacity is
  40. * increased when the ByteBuffer overflows.
  41. * @exception IllegalArgumentException if the specified initial capacity
  42. * is negative
  43. */
  44. public ByteBuffer(int initialCapacity, int capacityIncrement) {
  45. super();
  46. if (initialCapacity < 0)
  47. throw new IllegalArgumentException("Illegal Capacity: "+
  48. initialCapacity);
  49. this.elementData = new byte[initialCapacity];
  50. this.capacityIncrement = capacityIncrement;
  51. }
  52. /**
  53. * Constructs an empty ByteBuffer with the specified initial capacity and
  54. * with its capacity increment equal to zero.
  55. *
  56. * @param initialCapacity the initial capacity of the ByteBuffer.
  57. * @exception IllegalArgumentException if the specified initial capacity
  58. * is negative
  59. */
  60. public ByteBuffer(int initialCapacity) {
  61. this(initialCapacity, 0);
  62. }
  63. /**
  64. * Constructs an empty ByteBuffer so that its internal data array
  65. * has size <tt>10</tt> and its standard capacity increment is
  66. * zero.
  67. */
  68. public ByteBuffer() {
  69. this(200);
  70. }
  71. /**
  72. * Trims the capacity of this ByteBuffer to be the ByteBuffer's current
  73. * size. If the capacity of this cector is larger than its current
  74. * size, then the capacity is changed to equal the size by replacing
  75. * its internal data array, kept in the field <tt>elementData</tt>,
  76. * with a smaller one. An application can use this operation to
  77. * minimize the storage of a ByteBuffer.
  78. */
  79. public void trimToSize() {
  80. int oldCapacity = elementData.length;
  81. if (elementCount < oldCapacity) {
  82. byte oldData[] = elementData;
  83. elementData = new byte[elementCount];
  84. System.arraycopy(oldData, 0, elementData, 0, elementCount);
  85. }
  86. }
  87. /**
  88. * This implements the unsynchronized semantics of ensureCapacity.
  89. * Synchronized methods in this class can internally call this
  90. * method for ensuring capacity without incurring the cost of an
  91. * extra synchronization.
  92. *
  93. * @see java.util.ByteBuffer#ensureCapacity(int)
  94. */
  95. private void ensureCapacityHelper(int minCapacity) {
  96. int oldCapacity = elementData.length;
  97. if (minCapacity > oldCapacity) {
  98. byte oldData[] = elementData;
  99. int newCapacity = (capacityIncrement > 0) ?
  100. (oldCapacity + capacityIncrement) : (oldCapacity * 2);
  101. if (newCapacity < minCapacity) {
  102. newCapacity = minCapacity;
  103. }
  104. elementData = new byte[newCapacity];
  105. System.arraycopy(oldData, 0, elementData, 0, elementCount);
  106. }
  107. }
  108. /**
  109. * Returns the current capacity of this ByteBuffer.
  110. *
  111. * @return the current capacity (the length of its internal
  112. * data arary, kept in the field <tt>elementData</tt>
  113. * of this ByteBuffer.
  114. */
  115. public int capacity() {
  116. return elementData.length;
  117. }
  118. /**
  119. * Returns the number of components in this ByteBuffer.
  120. *
  121. * @return the number of components in this ByteBuffer.
  122. */
  123. public int size() {
  124. return elementCount;
  125. }
  126. /**
  127. * Tests if this ByteBuffer has no components.
  128. *
  129. * @return <code>true</code> if and only if this ByteBuffer has
  130. * no components, that is, its size is zero;
  131. * <code>false</code> otherwise.
  132. */
  133. public boolean isEmpty() {
  134. return elementCount == 0;
  135. }
  136. public void append(byte value)
  137. {
  138. ensureCapacityHelper(elementCount + 1);
  139. elementData[elementCount++] = value;
  140. }
  141. public void append( int value )
  142. {
  143. ensureCapacityHelper(elementCount + 4);
  144. doAppend( value ) ;
  145. }
  146. private void doAppend( int value )
  147. {
  148. int current = value ;
  149. for (int ctr=0; ctr<4; ctr++) {
  150. elementData[elementCount+ctr] = (byte)(current & 255) ;
  151. current = current >> 8 ;
  152. }
  153. elementCount += 4 ;
  154. }
  155. public void append( String value )
  156. {
  157. byte[] data = value.getBytes() ;
  158. ensureCapacityHelper( elementCount + data.length + 4 ) ;
  159. doAppend( data.length ) ;
  160. System.arraycopy( data, 0, elementData, elementCount, data.length ) ;
  161. elementCount += data.length ;
  162. }
  163. /**
  164. * Returns an array containing all of the elements in this ByteBuffer
  165. * in the correct order.
  166. *
  167. * @since 1.2
  168. */
  169. public byte[] toArray() {
  170. return elementData ;
  171. }
  172. }