| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap.util; |
| 39 | |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.io.OutputStream; |
| 43 | import java.io.RandomAccessFile; |
| 44 | import java.io.Serializable; |
| 45 | |
| 46 | /** |
| 47 | * This class is similar to the <CODE>java.lang.StringBuffer</CODE> |
| 48 | * class. Instead of storing a string, an object of this class |
| 49 | * stores an array of bytes. (This is referred to as a "byte buffer".) |
| 50 | * <P> |
| 51 | * |
| 52 | * This class also differs from <CODE>StringBuffer</CODE> in the |
| 53 | * following ways: |
| 54 | * <UL> |
| 55 | * <LI>None of the methods are synchronized. You cannot share a |
| 56 | * byte buffer among multiple threads. |
| 57 | * <LI>Converting to a String requires a copy of the character data. |
| 58 | * <LI>In order to speed up memory allocation, <CODE>Alloc</CODE> |
| 59 | * and <CODE>Recycle</CODE> methods are provided. You can "recycle" |
| 60 | * any <CODE>ByteBuf</CODE> objects you no longer need by using the |
| 61 | * <CODE>Recycle</CODE> method. Calling the <CODE>Alloc</CODE> |
| 62 | * method will reuse objects that have been "recycled." To |
| 63 | * To clear out the cache of these "recycled" objects, use |
| 64 | * the <CODE>EmptyRecycler</CODE> method. |
| 65 | * <LI>Additional "helper" methods are provided (for example, |
| 66 | * functions for comparing data). |
| 67 | * </UL> |
| 68 | */ |
| 69 | public final class ByteBuf implements Serializable { |
| 70 | |
| 71 | static final long serialVersionUID = -786393456618166871L; |
| 72 | private byte value[]; |
| 73 | private int count; |
| 74 | |
| 75 | /** |
| 76 | * Constructs an empty byte buffer with the default length of 16. |
| 77 | */ |
| 78 | public ByteBuf() { |
| 79 | this(16); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Constructs an empty byte buffer with the specified initial length. |
| 84 | * @param length initial number of bytes that this buffer should hold |
| 85 | */ |
| 86 | public ByteBuf(int length) { |
| 87 | value = new byte[length]; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Constructs a byte buffer with the specified initial value. |
| 92 | * The new byte buffer is 16 bytes longer than the initial value. |
| 93 | * @param str initial value that this buffer should hold |
| 94 | */ |
| 95 | public ByteBuf(String str) { |
| 96 | this(str.length() + 16); |
| 97 | append(str); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Constructs a byte buffer with the specified length. |
| 102 | * An initial value is stored in this buffer, starting |
| 103 | * at the specified offset. |
| 104 | * @param bytes array of bytes to initially store in the buffer |
| 105 | * @param offset index where you want the initial value to start in the array |
| 106 | * @param length length of the buffer to allocate |
| 107 | */ |
| 108 | public ByteBuf(byte bytes[], int offset, int length) { |
| 109 | value = new byte[length]; |
| 110 | System.arraycopy(bytes, offset, value, 0, length); |
| 111 | count = length; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the length (character count) of the byte buffer. |
| 116 | */ |
| 117 | public int length() { |
| 118 | return count; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the current capacity of the byte buffer. The capacity |
| 123 | * is the amount of storage available for newly inserted bytes. |
| 124 | * If the capacity is exceeded, more space will be allocated. |
| 125 | */ |
| 126 | public int capacity() { |
| 127 | return value.length; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Ensures that the capacity of the buffer is at least equal to the |
| 132 | * specified minimum capacity. |
| 133 | * @param minimumCapacity the minimum number of bytes that you want |
| 134 | * the byte buffer to hold |
| 135 | */ |
| 136 | public void ensureCapacity(int minimumCapacity) { |
| 137 | int maxCapacity = value.length; |
| 138 | |
| 139 | if (minimumCapacity > maxCapacity) { |
| 140 | int newCapacity = (maxCapacity + 1) * 2; |
| 141 | if (minimumCapacity > newCapacity) { |
| 142 | newCapacity = minimumCapacity; |
| 143 | } |
| 144 | |
| 145 | byte newValue[] = new byte[newCapacity]; |
| 146 | System.arraycopy(value, 0, newValue, 0, count); |
| 147 | value = newValue; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets the length of the byte buffer. If you set a length that is |
| 153 | * shorter than the current length, bytes at the end of the buffer |
| 154 | * are lost. If you increase the length of the buffer, the values |
| 155 | * of the new bytes in the buffer are set to 0. |
| 156 | * @param newLength the new length of the buffer |
| 157 | * @exception StringIndexOutOfBoundsException You have specified |
| 158 | * an invalid length. |
| 159 | */ |
| 160 | public void setLength(int newLength) { |
| 161 | if (newLength < 0) { |
| 162 | throw new StringIndexOutOfBoundsException(newLength); |
| 163 | } |
| 164 | if (count < newLength) { |
| 165 | ensureCapacity(newLength); |
| 166 | for (; count < newLength; count++) { |
| 167 | value[count] = 0; |
| 168 | } |
| 169 | } |
| 170 | count = newLength; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the byte at the specified index. The value of an index |
| 175 | * can range from 0 to length - 1. |
| 176 | * @param index index of the byte to find |
| 177 | * @exception StringIndexOutOfBoundsException You have specified an |
| 178 | * invalid index. |
| 179 | */ |
| 180 | public byte byteAt(int index) { |
| 181 | if ((index < 0) || (index >= count)) { |
| 182 | throw new StringIndexOutOfBoundsException(index); |
| 183 | } |
| 184 | return value[index]; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Copies the bytes (from the section of the byte buffer from the index |
| 189 | * <CODE>srcBegin</CODE> to the index <CODE>srcEnd - 1 </CODE>) |
| 190 | * into the specified byte array. The copied |
| 191 | * bytes are inserted in the byte array at the index specified by |
| 192 | * <CODE>dstBegin</CODE>. Both <CODE>srcBegin</CODE> and |
| 193 | * <CODE>srcEnd</CODE> must be valid indexes in the buffer. |
| 194 | * @param srcBegin index identifying the start of the section |
| 195 | * of the byte buffer to copy |
| 196 | * @param srcEnd index identifying the end of the section |
| 197 | * of the byte buffer to copy. (Copy all bytes |
| 198 | * before the byte identified by this index.) |
| 199 | * @param dst the byte array to copy the data to |
| 200 | * @param dstBegin index of the byte array identifying the |
| 201 | * location to which the byte buffer is copied |
| 202 | * @exception StringIndexOutOfBoundsException You specified an invalid index into the buffer. |
| 203 | */ |
| 204 | public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { |
| 205 | if ((srcBegin < 0) || (srcBegin >= count)) { |
| 206 | throw new StringIndexOutOfBoundsException(srcBegin); |
| 207 | } |
| 208 | if ((srcEnd < 0) || (srcEnd > count)) { |
| 209 | throw new StringIndexOutOfBoundsException(srcEnd); |
| 210 | } |
| 211 | if (srcBegin < srcEnd) { |
| 212 | System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Sets the value of the byte at the specified index. |
| 218 | * @param index the index of the byte to set |
| 219 | * @param b the new value to set |
| 220 | * @exception StringIndexOutOfBoundsException You have specified |
| 221 | * an invalid index. |
| 222 | */ |
| 223 | public void setByteAt(int index, byte b) { |
| 224 | if ((index < 0) || (index >= count)) { |
| 225 | throw new StringIndexOutOfBoundsException(index); |
| 226 | } |
| 227 | value[index] = b; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Appends an object to the end of this byte buffer. |
| 232 | * @param obj the object to append to the buffer |
| 233 | * @return the same <CODE>ByteBuf</CODE> object (not a new object) |
| 234 | * with the string representation of the specified object appended. |
| 235 | */ |
| 236 | public ByteBuf append(Object obj) { |
| 237 | return append(String.valueOf(obj)); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Appends a string to the end of this byte buffer. |
| 242 | * This method appends one byte for each character in the string. |
| 243 | * The upper 8 bits of the character are stripped off. |
| 244 | * <P> |
| 245 | * |
| 246 | * If you want to retain all bits in the character (not just |
| 247 | * the lower 8 bits), use <CODE>append(String.getBytes())</CODE> |
| 248 | * instead. |
| 249 | * @param str the string that you want appended to the buffer |
| 250 | * @return the same <CODE>ByteBuf</CODE> object (not a new object) |
| 251 | * with the specified string appended |
| 252 | */ |
| 253 | public ByteBuf append(String str) { |
| 254 | if (str == null) { |
| 255 | str = String.valueOf(str); |
| 256 | } |
| 257 | |
| 258 | int len = str.length(); |
| 259 | ensureCapacity(count + len); |
| 260 | for (int i=0 ; i<len ; i++) { |
| 261 | value[count++] = (byte) str.charAt(i); |
| 262 | } |
| 263 | return this; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Appends an array of bytes to the end of this byte buffer. |
| 268 | * @param str the array of bytes to append to this buffer |
| 269 | * @return the same <CODE>ByteBuf</CODE> object (not a new object) |
| 270 | * with the specified bytes appended. |
| 271 | */ |
| 272 | public ByteBuf append(byte str[]) { |
| 273 | int len = str.length; |
| 274 | ensureCapacity(count + len); |
| 275 | System.arraycopy(str, 0, value, count, len); |
| 276 | count += len; |
| 277 | return this; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Appends a part of an array of bytes to the end of this byte buffer. |
| 282 | * @param str the array of bytes to append to this buffer |
| 283 | * @param offset the index in the array marking the start of the |
| 284 | * section to copy |
| 285 | * @param len the number of bytes to add |
| 286 | * @return the same <CODE>ByteBuf</CODE> object (not a new object) |
| 287 | * with the specified section of the byte array appended |
| 288 | */ |
| 289 | public ByteBuf append(byte str[], int offset, int len) { |
| 290 | ensureCapacity(count + len); |
| 291 | System.arraycopy(str, offset, value, count, len); |
| 292 | count += len; |
| 293 | return this; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Appends a byte buffer to the end of this byte buffer. |
| 298 | * @param buf the byte buffer to append to this buffer |
| 299 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 300 | * with bytes from the specified byte buffer appended. |
| 301 | */ |
| 302 | public ByteBuf append(ByteBuf buf) { |
| 303 | append(buf.toBytes(), 0, buf.length()); |
| 304 | return this; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Appends a boolean to the end of this byte buffer. |
| 309 | * @param b the boolean value that you want appended to this buffer |
| 310 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 311 | * with bytes from the string representation of the boolean value appended. |
| 312 | */ |
| 313 | public ByteBuf append(boolean b) { |
| 314 | return append(String.valueOf(b)); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Appends a byte to the end of this byte buffer. |
| 319 | * @param ch the byte to append to this buffer |
| 320 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 321 | * with the specified byte appended. |
| 322 | */ |
| 323 | public ByteBuf append(byte b) { |
| 324 | ensureCapacity(count + 1); |
| 325 | value[count++] = b; |
| 326 | return this; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Appends an integer to the end of this byte buffer. |
| 331 | * @param i the integer to append to this byte buffer |
| 332 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 333 | * with the string representation of the specified integer appended. |
| 334 | */ |
| 335 | public ByteBuf append(int i) { |
| 336 | return append(String.valueOf(i)); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Appends a <CODE>long</CODE> value to the end of this byte buffer. |
| 341 | * @param l the <CODE>long</CODE> value to append to this buffer |
| 342 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 343 | * with the string representation of the specified <CODE>long</CODE> |
| 344 | * value appended. |
| 345 | */ |
| 346 | public ByteBuf append(long l) { |
| 347 | return append(String.valueOf(l)); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Appends a <CODE>float</CODE> to the end of this byte buffer. |
| 352 | * @param f the <CODE>float</CODE> value to append to this buffer |
| 353 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 354 | * with the string representation of the specified <CODE>float</CODE> |
| 355 | * value appended. |
| 356 | */ |
| 357 | public ByteBuf append(float f) { |
| 358 | return append(String.valueOf(f)); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Appends a <CODE>double</CODE> to the end of this byte buffer. |
| 363 | * @param d the <CODE>double</CODE> value to append to this buffer |
| 364 | * @return the original <CODE>ByteBuf</CODE> object (not a new object) |
| 365 | * with the string representation of the specified <CODE>double</CODE> |
| 366 | * value appended. |
| 367 | */ |
| 368 | public ByteBuf append(double d) { |
| 369 | return append(String.valueOf(d)); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Returns the data in the byte buffer to a string. |
| 374 | * @return the string representation of the data in the byte buffer. |
| 375 | */ |
| 376 | public String toString() { |
| 377 | return new String(value, 0, count); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Returns the data in the byte buffer as a byte array. |
| 382 | * @return the byte array containing data in the byte buffer. |
| 383 | */ |
| 384 | public byte[] toBytes() { |
| 385 | byte[] b = new byte[count]; |
| 386 | System.arraycopy(value, 0, b, 0, count); |
| 387 | return b; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Invokes the <CODE>InputStream.read</CODE> method and appends the |
| 392 | * the bytes read to this byte buffer. |
| 393 | * @param file the input stream from which to read the bytes |
| 394 | * @param max_bytes the maximum number of bytes to read into the |
| 395 | * byte buffer |
| 396 | * @return the number of bytes read, or -1 if there is no more data |
| 397 | * to read. |
| 398 | * @exception IOException An I/O error has occurred. |
| 399 | */ |
| 400 | public int read(InputStream file, int max_bytes) |
| 401 | throws IOException { |
| 402 | ensureCapacity(count + max_bytes); |
| 403 | int i = file.read(value, count, max_bytes); |
| 404 | if (i > 0) count += i; |
| 405 | return i; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Invokes the <CODE>RandomAccessFile.read</CODE> method, appending |
| 410 | * the bytes read to this byte buffer. |
| 411 | * @param file the <CODE>RandomAccessFile</CODE> object from which |
| 412 | * to read the bytes |
| 413 | * @param max_bytes the maximum number of bytes to read into the |
| 414 | * byte buffer |
| 415 | * @return the number of bytes read, or -1 if there is no more data |
| 416 | * to read. |
| 417 | * @exception IOException An I/O error has occurred. |
| 418 | */ |
| 419 | public int read(RandomAccessFile file, int max_bytes) |
| 420 | throws IOException { |
| 421 | ensureCapacity(count + max_bytes); |
| 422 | int i = file.read(value, count, max_bytes); |
| 423 | if (i > 0) count += i; |
| 424 | return i; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Writes the contents of the byte buffer to the specified output stream. |
| 429 | * @param out the output stream |
| 430 | * @exception IOException An I/O error has occurred. |
| 431 | */ |
| 432 | public void write(OutputStream out) throws IOException { |
| 433 | out.write(value, 0, count); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Writes the contents of the byte buffer to the specified |
| 438 | * <CODE>RandomAccessFile</CODE> object. |
| 439 | * @param out the <CODE>RandomAccessFile</CODE> object |
| 440 | * dexception IOException An I/O error has occurred. |
| 441 | */ |
| 442 | public void write(RandomAccessFile out) throws IOException { |
| 443 | out.write(value, 0, count); |
| 444 | } |
| 445 | } |