1 7 8 package com.sun.corba.se.impl.ior ; 9 10 11 public class ByteBuffer { 12 19 protected byte elementData[]; 20 21 28 protected int elementCount; 29 30 38 protected int capacityIncrement; 39 40 50 public ByteBuffer(int initialCapacity, int capacityIncrement) { 51 super(); 52 if (initialCapacity < 0) 53 throw new IllegalArgumentException ("Illegal Capacity: "+ 54 initialCapacity); 55 this.elementData = new byte[initialCapacity]; 56 this.capacityIncrement = capacityIncrement; 57 } 58 59 67 public ByteBuffer(int initialCapacity) { 68 this(initialCapacity, 0); 69 } 70 71 76 public ByteBuffer() { 77 this(200); 78 } 79 80 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 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 126 public int capacity() { 127 return elementData.length; 128 } 129 130 135 public int size() { 136 return elementCount; 137 } 138 139 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 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 187 public byte[] toArray() { 188 return elementData ; 189 } 190 } 191 | Popular Tags |