View Javadoc
1 /* 2 * (C) 2002 David Carr david@carr.name 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package net.sourceforge.mflow.impl; 20 21 /*** 22 * A class to facilitate the creation of byte arrays from bytes/byte arrays 23 * 24 * @author <a href="mailto:david@carr.name">David Carr</a> 25 * @version $Revision: 1.4 $ 26 * @todo remove? 27 */ 28 class ByteBuffer { 29 /*** 30 * Private storage for the bytes 31 */ 32 protected byte[] data; 33 /*** 34 * Private storage for the current size 35 */ 36 protected int size; 37 38 /*** 39 * Initializes the size to 0 and the capacity to 1024 40 */ 41 public ByteBuffer() { 42 this.size = 0; 43 this.data = new byte[1024]; 44 } 45 46 /*** 47 * Doubles the capacity 48 */ 49 private void resize() { 50 resizeTo(this.data.length * 2); 51 } 52 53 /*** 54 * If it specifies an increase in capacity, replaces the array with one of the specified length and transfers the data 55 * 56 * @param length the minimum length for the data array 57 */ 58 private void resizeTo(int length) { 59 if (length > this.data.length) { 60 byte[] oldData = this.data; 61 this.data = new byte[length]; 62 for (int i = 0; i < oldData.length; i++) { 63 this.data[i] = this.data[i]; 64 } 65 } 66 } 67 68 /*** 69 * Returns the current capacity of this vector. 70 * 71 * @return the current capacity (the length of its internal data array, kept in the field this.data). 72 */ 73 public int capacity() { 74 return this.data.length; 75 } 76 77 /*** 78 * Removes all of bytes from this ByteBuffer. It will be empty after this call returns, but will have the same capacity. 79 */ 80 public void clear() { 81 for (int i = 0; i < this.data.length; i++) { 82 this.data[i] = 0; 83 } 84 this.size = 0; 85 } 86 87 /*** 88 * Appends a byte to the end of this ByteBuffer, increasing capacity if neccesary 89 * 90 * @param b the byte to append 91 */ 92 public void append(byte b) { 93 if (this.size == this.data.length) { 94 resize(); 95 } 96 this.data[this.size] = b; 97 this.size++; 98 } 99 100 /*** 101 * Appends a byte array to the end of this ByteBuffer, increasing capacity if neccesary 102 * 103 * @param b the byte array to append 104 */ 105 public void append(byte[] b) { 106 append(b, 0, b.length); 107 } 108 109 /*** 110 * Appends a byte array to the end of this ByteBuffer, increasing capacity if neccesary 111 * 112 * @param b the byte array to append 113 * @param offset which index to begin at 114 * @param length how many bytes to append 115 */ 116 public void append(byte[] b, int offset, int length) { 117 if (this.size + length >= this.data.length) { 118 ensureCapacity(this.size + length); 119 } 120 for (int i = 0; i < length; i++) { 121 this.data[this.size + i] = b[i + offset]; 122 } 123 this.size += length; 124 } 125 126 /*** 127 * Returns the byte at the specified position in the ByteBuffer. 128 * 129 * @param index index of the item to return. 130 * @return byte at the specified index 131 * @throws ArrayIndexOutOfBoundsException if index is out of range (index < 0 || index >= size()) 132 */ 133 public byte get(int index) throws ArrayIndexOutOfBoundsException { 134 if (index < 0 || index >= this.size) 135 throw new ArrayIndexOutOfBoundsException(index); 136 return this.data[index]; 137 } 138 139 /*** 140 * Tests if this ByteBuffer contains no bytes. 141 * 142 * @return true if and only if the byte buffer contains no bytes, that is, its size is zero; false otherwise. 143 */ 144 public boolean isEmpty() { 145 return this.size == 0; 146 } 147 148 /*** 149 * Replaces the byte at the specified position in this ByteBuffer with the specified byte. 150 * 151 * @param index index of the byte to replace 152 * @param b byte to be stored at the specified position 153 * @return the byte previously at the specified position 154 * @throws ArrayIndexOutOfBoundsException if index is out of range (index < 0 || index >= size()) 155 */ 156 public byte set(int index, byte b) throws ArrayIndexOutOfBoundsException { 157 if (index < 0 || index >= this.size) 158 throw new ArrayIndexOutOfBoundsException(index); 159 byte oldVal = this.data[index]; 160 this.data[index] = b; 161 return oldVal; 162 } 163 164 /*** 165 * Sets the size of this ByteBuffer. 166 * <br/><br/> 167 * If the new size is greater than the current size, 0 value bytes are added to the end of the ByteBuffer. If the new size is less than the current size, all components at index newSize and greater are discarded. 168 * 169 * @param newSize the new size of the ByteBuffer 170 * @throws ArrayIndexOutOfBoundsException if new size is negative. 171 */ 172 public void setSize(int newSize) throws ArrayIndexOutOfBoundsException { 173 if (newSize < 0) 174 throw new ArrayIndexOutOfBoundsException(newSize - 1); 175 if (newSize < this.size) { 176 for (int i = newSize; i < this.size; i++) { 177 this.data[i] = 0; 178 } 179 } else { 180 ensureCapacity(newSize); 181 } 182 this.size = newSize; 183 } 184 185 /*** 186 * Returns the number of bytes in this ByteBuffer. 187 * 188 * @return the number of bytes in this ByteBuffer. 189 */ 190 public int size() { 191 return this.size; 192 } 193 194 /*** 195 * Increases the capacity of this ByteBuffer, if necessary, to ensure that it can hold at least the number of bytes specified by the minimum capacity argument. 196 * <br/><br/> 197 * If the current capacity of this ByteBuffer is less than minCapacity, then its capacity is increased by replacing its internal data array, kept in the field this.data, with a larger one. 198 * 199 * @param minCapacity the desired minimum capacity 200 */ 201 public void ensureCapacity(int minCapacity) { 202 if (this.data.length < minCapacity) { 203 resizeTo(minCapacity); 204 } 205 } 206 207 /*** 208 * Trims the capacity of this ByteBuffer to be the ByteBuffer's current size. If the capacity of this ByteBuffer is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field this.data, with a smaller one. An application can use this operation to minimize the storage of a ByteBuffer. 209 */ 210 public void trimToSize() { 211 if (this.data.length != this.size) { 212 byte[] oldData = this.data; 213 this.data = new byte[this.size]; 214 for (int i = 0; i < this.size; i++) { 215 this.data[i] = oldData[i]; 216 } 217 } 218 } 219 220 /*** 221 * Returns a byte array containing the contents of the ByteBuffer. 222 * 223 * @return a byte array containing the contents of the ByteBuffer. 224 */ 225 public byte[] toArray() { 226 byte[] ret = new byte[this.size]; 227 for (int i = 0; i < this.size; i++) { 228 ret[i] = this.data[i]; 229 } 230 return ret; 231 } 232 }

This page was automatically generated by Maven