| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.qpid.proton.codec; |
| 20 |
|
| 21 |
import java.nio.BufferUnderflowException; |
| 22 |
import java.nio.ByteBuffer; |
| 23 |
import java.nio.InvalidMarkException; |
| 24 |
import java.nio.ReadOnlyBufferException; |
| 25 |
import java.nio.charset.CharacterCodingException; |
| 26 |
import java.nio.charset.CharsetDecoder; |
| 27 |
import java.nio.charset.StandardCharsets; |
| 28 |
|
| 29 |
/** |
| 30 |
* Interface to abstract a buffer, similar to {@link WritableBuffer} |
| 31 |
*/ |
| 32 |
public interface ReadableBuffer { |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the capacity of the backing buffer of this ReadableBuffer |
| 36 |
* @return the capacity of the backing buffer of this ReadableBuffer |
| 37 |
*/ |
| 38 |
int capacity(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns true if this ReadableBuffer is backed by an array which can be |
| 42 |
* accessed by the {@link #array()} and {@link #arrayOffset()} methods. |
| 43 |
* |
| 44 |
* @return true if the buffer is backed by a primitive array. |
| 45 |
*/ |
| 46 |
boolean hasArray(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns the primitive array that backs this buffer if one exists and the |
| 50 |
* buffer is not read-only. The caller should have checked the {@link #hasArray()} |
| 51 |
* method before calling this method. |
| 52 |
* |
| 53 |
* @return the array that backs this buffer is available. |
| 54 |
* |
| 55 |
* @throws UnsupportedOperationException if this {@link ReadableBuffer} doesn't support array access. |
| 56 |
* @throws ReadOnlyBufferException if the ReadableBuffer is read-only. |
| 57 |
*/ |
| 58 |
byte[] array(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the offset into the backing array of the first element in the buffer. The caller |
| 62 |
* should have checked the {@link #hasArray()} method before calling this method. |
| 63 |
* |
| 64 |
* @return the offset into the backing array of the first element in the buffer. |
| 65 |
* |
| 66 |
* @throws UnsupportedOperationException if this {@link ReadableBuffer} doesn't support array access. |
| 67 |
* @throws ReadOnlyBufferException if the ReadableBuffer is read-only. |
| 68 |
*/ |
| 69 |
int arrayOffset(); |
| 70 |
|
| 71 |
/** |
| 72 |
* Compact the backing storage of this ReadableBuffer, possibly freeing previously-read |
| 73 |
* portions of pooled data or reducing the number of backing arrays if present. |
| 74 |
* <p> |
| 75 |
* This is an optional operation and care should be taken in its implementation. |
| 76 |
* |
| 77 |
* @return a reference to this buffer |
| 78 |
*/ |
| 79 |
ReadableBuffer reclaimRead(); |
| 80 |
|
| 81 |
/** |
| 82 |
* Reads the byte at the current position and advances the position by 1. |
| 83 |
* |
| 84 |
* @return the byte at the current position. |
| 85 |
* |
| 86 |
* @throws BufferUnderflowException if the buffer position has reached the limit. |
| 87 |
*/ |
| 88 |
byte get(); |
| 89 |
|
| 90 |
/** |
| 91 |
* Reads the byte at the given index, the buffer position is not affected. |
| 92 |
* |
| 93 |
* @param index |
| 94 |
* The index in the buffer from which to read the byte. |
| 95 |
* |
| 96 |
* @return the byte value stored at the target index. |
| 97 |
* |
| 98 |
* @throws IndexOutOfBoundsException if the index is not in range for this buffer. |
| 99 |
*/ |
| 100 |
byte get(int index); |
| 101 |
|
| 102 |
/** |
| 103 |
* Reads four bytes from the buffer and returns them as an integer value. The |
| 104 |
* buffer position is advanced by four byes. |
| 105 |
* |
| 106 |
* @return and integer value composed of bytes read from the buffer. |
| 107 |
* |
| 108 |
* @throws BufferUnderflowException if the buffer position has reached the limit. |
| 109 |
*/ |
| 110 |
int getInt(); |
| 111 |
|
| 112 |
/** |
| 113 |
* Reads eight bytes from the buffer and returns them as an long value. The |
| 114 |
* buffer position is advanced by eight byes. |
| 115 |
* |
| 116 |
* @return and long value composed of bytes read from the buffer. |
| 117 |
* |
| 118 |
* @throws BufferUnderflowException if the buffer position has reached the limit. |
| 119 |
*/ |
| 120 |
long getLong(); |
| 121 |
|
| 122 |
/** |
| 123 |
* Reads two bytes from the buffer and returns them as an short value. The |
| 124 |
* buffer position is advanced by two byes. |
| 125 |
* |
| 126 |
* @return and short value composed of bytes read from the buffer. |
| 127 |
* |
| 128 |
* @throws BufferUnderflowException if the buffer position has reached the limit. |
| 129 |
*/ |
| 130 |
short getShort(); |
| 131 |
|
| 132 |
/** |
| 133 |
* Reads four bytes from the buffer and returns them as an float value. The |
| 134 |
* buffer position is advanced by four byes. |
| 135 |
* |
| 136 |
* @return and float value composed of bytes read from the buffer. |
| 137 |
* |
| 138 |
* @throws BufferUnderflowException if the buffer position has reached the limit. |
| 139 |
*/ |
| 140 |
float getFloat(); |
| 141 |
|
| 142 |
/** |
| 143 |
* Reads eight bytes from the buffer and returns them as an double value. The |
| 144 |
* buffer position is advanced by eight byes. |
| 145 |
* |
| 146 |
* @return and double value composed of bytes read from the buffer. |
| 147 |
* |
| 148 |
* @throws BufferUnderflowException if the buffer position has reached the limit. |
| 149 |
*/ |
| 150 |
double getDouble(); |
| 151 |
|
| 152 |
/** |
| 153 |
* A bulk read method that copies bytes from this buffer into the target byte array. |
| 154 |
* |
| 155 |
* @param target |
| 156 |
* The byte array to copy bytes read from this buffer. |
| 157 |
* @param offset |
| 158 |
* The offset into the given array where the copy starts. |
| 159 |
* @param length |
| 160 |
* The number of bytes to copy into the target array. |
| 161 |
* |
| 162 |
* @return a reference to this ReadableBuffer instance. |
| 163 |
* |
| 164 |
* @throws BufferUnderflowException if the are less readable bytes than the array length. |
| 165 |
* @throws IndexOutOfBoundsException if the offset or length values are invalid. |
| 166 |
*/ |
| 167 |
ReadableBuffer get(final byte[] target, final int offset, final int length); |
| 168 |
|
| 169 |
/** |
| 170 |
* A bulk read method that copies bytes from this buffer into the target byte array. |
| 171 |
* |
| 172 |
* @param target |
| 173 |
* The byte array to copy bytes read from this buffer. |
| 174 |
* |
| 175 |
* @return a reference to this ReadableBuffer instance. |
| 176 |
* |
| 177 |
* @throws BufferUnderflowException if the are less readable bytes than the array length. |
| 178 |
*/ |
| 179 |
ReadableBuffer get(final byte[] target); |
| 180 |
|
| 181 |
/** |
| 182 |
* Copy data from this buffer to the target buffer starting from the current |
| 183 |
* position and continuing until either this buffer's remaining bytes are |
| 184 |
* consumed or the target is full. |
| 185 |
* |
| 186 |
* @param target |
| 187 |
* The WritableBuffer to transfer this buffer's data to. |
| 188 |
* |
| 189 |
* @return a reference to this ReadableBuffer instance. |
| 190 |
*/ |
| 191 |
ReadableBuffer get(WritableBuffer target); |
| 192 |
|
| 193 |
/** |
| 194 |
* Creates a new ReadableBuffer instance that is a view of the readable portion of |
| 195 |
* this buffer. The position will be set to zero and the limit and the reported capacity |
| 196 |
* will match the value returned by this buffer's {@link #remaining()} method, the mark |
| 197 |
* will be undefined. |
| 198 |
* |
| 199 |
* @return a new ReadableBuffer that is a view of the readable portion of this buffer. |
| 200 |
*/ |
| 201 |
ReadableBuffer slice(); |
| 202 |
|
| 203 |
/** |
| 204 |
* Sets the buffer limit to the current position and the position is set to zero, the |
| 205 |
* mark value reset to undefined. |
| 206 |
* |
| 207 |
* @return a reference to this {@link ReadableBuffer}. |
| 208 |
*/ |
| 209 |
ReadableBuffer flip(); |
| 210 |
|
| 211 |
/** |
| 212 |
* Sets the current read limit of this buffer to the given value. If the buffer mark |
| 213 |
* value is defined and is larger than the limit the mark will be discarded. If the |
| 214 |
* position is larger than the new limit it will be reset to the new limit. |
| 215 |
* |
| 216 |
* @param limit |
| 217 |
* The new read limit to set for this buffer. |
| 218 |
* |
| 219 |
* @return a reference to this {@link ReadableBuffer}. |
| 220 |
* |
| 221 |
* @throws IllegalArgumentException if the limit value is negative or greater than the capacity. |
| 222 |
*/ |
| 223 |
ReadableBuffer limit(int limit); |
| 224 |
|
| 225 |
/** |
| 226 |
* @return the current value of this buffer's limit. |
| 227 |
*/ |
| 228 |
int limit(); |
| 229 |
|
| 230 |
/** |
| 231 |
* Sets the current position of this buffer to the given value. If the buffer mark |
| 232 |
* value is defined and is larger than the newly set position is must be discarded. |
| 233 |
* |
| 234 |
* @param position |
| 235 |
* The new position to set for this buffer. |
| 236 |
* |
| 237 |
* @return a reference to this {@link ReadableBuffer}. |
| 238 |
* |
| 239 |
* @throws IllegalArgumentException if the position value is negative or greater than the limit. |
| 240 |
*/ |
| 241 |
ReadableBuffer position(int position); |
| 242 |
|
| 243 |
/** |
| 244 |
* @return the current position from which the next read operation will start. |
| 245 |
*/ |
| 246 |
int position(); |
| 247 |
|
| 248 |
/** |
| 249 |
* Mark the current position of this buffer which can be returned to after a |
| 250 |
* read operation by calling {@link #reset()}. |
| 251 |
* |
| 252 |
* @return a reference to this {@link ReadableBuffer}. |
| 253 |
*/ |
| 254 |
ReadableBuffer mark(); |
| 255 |
|
| 256 |
/** |
| 257 |
* Reset the buffer's position to a previously marked value, the mark should remain |
| 258 |
* set after calling this method. |
| 259 |
* |
| 260 |
* @return a reference to this {@link ReadableBuffer}. |
| 261 |
* |
| 262 |
* @throws InvalidMarkException if the mark value is undefined. |
| 263 |
*/ |
| 264 |
ReadableBuffer reset(); |
| 265 |
|
| 266 |
/** |
| 267 |
* Resets the buffer position to zero and clears and previously set mark. |
| 268 |
* |
| 269 |
* @return a reference to this {@link ReadableBuffer}. |
| 270 |
*/ |
| 271 |
ReadableBuffer rewind(); |
| 272 |
|
| 273 |
/** |
| 274 |
* Resets the buffer position to zero and sets the limit to the buffer capacity, |
| 275 |
* the mark value is discarded if set. |
| 276 |
* |
| 277 |
* @return a reference to this {@link ReadableBuffer}. |
| 278 |
*/ |
| 279 |
ReadableBuffer clear(); |
| 280 |
|
| 281 |
/** |
| 282 |
* @return the remaining number of readable bytes in this buffer. |
| 283 |
*/ |
| 284 |
int remaining(); |
| 285 |
|
| 286 |
/** |
| 287 |
* @return true if there are readable bytes still remaining in this buffer. |
| 288 |
*/ |
| 289 |
boolean hasRemaining(); |
| 290 |
|
| 291 |
/** |
| 292 |
* Creates a duplicate {@link ReadableBuffer} to this instance. |
| 293 |
* <p> |
| 294 |
* The duplicated buffer will have the same position, limit and mark as this |
| 295 |
* buffer. The two buffers share the same backing data. |
| 296 |
* |
| 297 |
* @return a duplicate of this {@link ReadableBuffer}. |
| 298 |
*/ |
| 299 |
ReadableBuffer duplicate(); |
| 300 |
|
| 301 |
/** |
| 302 |
* @return a ByteBuffer view of the current readable portion of this buffer. |
| 303 |
*/ |
| 304 |
ByteBuffer byteBuffer(); |
| 305 |
|
| 306 |
/** |
| 307 |
* Reads a UTF-8 encoded String from the buffer starting the decode at the |
| 308 |
* current position and reading until the limit is reached. The position |
| 309 |
* is advanced to the limit once this method returns. If there is no bytes |
| 310 |
* remaining in the buffer when this method is called a null is returned. |
| 311 |
* |
| 312 |
* @return a string decoded from the remaining bytes in this buffer. |
| 313 |
* |
| 314 |
* @throws CharacterCodingException if the encoding is invalid for any reason. |
| 315 |
*/ |
| 316 |
String readUTF8() throws CharacterCodingException; |
| 317 |
|
| 318 |
/** |
| 319 |
* Decodes a String from the buffer using the provided {@link CharsetDecoder} |
| 320 |
* starting the decode at the current position and reading until the limit is |
| 321 |
* reached. The position is advanced to the limit once this method returns. |
| 322 |
* If there is no bytes remaining in the buffer when this method is called a |
| 323 |
* null is returned. |
| 324 |
* |
| 325 |
* @return a string decoded from the remaining bytes in this buffer. |
| 326 |
* |
| 327 |
* @throws CharacterCodingException if the encoding is invalid for any reason. |
| 328 |
*/ |
| 329 |
String readString(CharsetDecoder decoder) throws CharacterCodingException; |
| 330 |
|
| 331 |
final class ByteBufferReader implements ReadableBuffer { |
| 332 |
|
| 333 |
private ByteBuffer buffer; |
| 334 |
|
| 335 |
public static ByteBufferReader allocate(int size) { |
| 336 |
ByteBuffer allocated = ByteBuffer.allocate(size); |
| 337 |
return new ByteBufferReader(allocated); |
| 338 |
} |
| 339 |
|
| 340 |
public static ByteBufferReader wrap(ByteBuffer buffer) { |
| 341 |
return new ByteBufferReader(buffer); |
| 342 |
} |
| 343 |
|
| 344 |
public static ByteBufferReader wrap(byte[] array) { |
| 345 |
return new ByteBufferReader(ByteBuffer.wrap(array)); |
| 346 |
} |
| 347 |
|
| 348 |
public ByteBufferReader(ByteBuffer buffer) { |
| 349 |
this.buffer = buffer; |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public int capacity() { |
| 354 |
return buffer.capacity(); |
| 355 |
} |
| 356 |
|
| 357 |
@Override |
| 358 |
public byte get() { |
| 359 |
return buffer.get(); |
| 360 |
} |
| 361 |
|
| 362 |
@Override |
| 363 |
public byte get(int index) { |
| 364 |
return buffer.get(index); |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public int getInt() { |
| 369 |
return buffer.getInt(); |
| 370 |
} |
| 371 |
|
| 372 |
@Override |
| 373 |
public long getLong() { |
| 374 |
return buffer.getLong(); |
| 375 |
} |
| 376 |
|
| 377 |
@Override |
| 378 |
public short getShort() { |
| 379 |
return buffer.getShort(); |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
public float getFloat() { |
| 384 |
return buffer.getFloat(); |
| 385 |
} |
| 386 |
|
| 387 |
@Override |
| 388 |
public double getDouble() { |
| 389 |
return buffer.getDouble(); |
| 390 |
} |
| 391 |
|
| 392 |
@Override |
| 393 |
public int limit() { |
| 394 |
return buffer.limit(); |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
public ReadableBuffer get(byte[] data, int offset, int length) { |
| 399 |
buffer.get(data, offset, length); |
| 400 |
return this; |
| 401 |
} |
| 402 |
|
| 403 |
@Override |
| 404 |
public ReadableBuffer get(byte[] data) { |
| 405 |
buffer.get(data); |
| 406 |
return this; |
| 407 |
} |
| 408 |
|
| 409 |
@Override |
| 410 |
public ReadableBuffer flip() { |
| 411 |
buffer.flip(); |
| 412 |
return this; |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public ReadableBuffer position(int position) { |
| 417 |
buffer.position(position); |
| 418 |
return this; |
| 419 |
} |
| 420 |
|
| 421 |
@Override |
| 422 |
public ReadableBuffer slice() { |
| 423 |
return new ByteBufferReader(buffer.slice()); |
| 424 |
} |
| 425 |
|
| 426 |
@Override |
| 427 |
public ReadableBuffer limit(int limit) { |
| 428 |
buffer.limit(limit); |
| 429 |
return this; |
| 430 |
} |
| 431 |
|
| 432 |
@Override |
| 433 |
public int remaining() { |
| 434 |
return buffer.remaining(); |
| 435 |
} |
| 436 |
|
| 437 |
@Override |
| 438 |
public int position() { |
| 439 |
return buffer.position(); |
| 440 |
} |
| 441 |
|
| 442 |
@Override |
| 443 |
public boolean hasRemaining() { |
| 444 |
return buffer.hasRemaining(); |
| 445 |
} |
| 446 |
|
| 447 |
@Override |
| 448 |
public ReadableBuffer duplicate() { |
| 449 |
return new ByteBufferReader(buffer.duplicate()); |
| 450 |
} |
| 451 |
|
| 452 |
@Override |
| 453 |
public ByteBuffer byteBuffer() { |
| 454 |
return buffer; |
| 455 |
} |
| 456 |
|
| 457 |
@Override |
| 458 |
public String readUTF8() { |
| 459 |
return StandardCharsets.UTF_8.decode(buffer).toString(); |
| 460 |
} |
| 461 |
|
| 462 |
@Override |
| 463 |
public String readString(CharsetDecoder decoder) throws CharacterCodingException { |
| 464 |
return decoder.decode(buffer).toString(); |
| 465 |
} |
| 466 |
|
| 467 |
@Override |
| 468 |
public boolean hasArray() { |
| 469 |
return buffer.hasArray(); |
| 470 |
} |
| 471 |
|
| 472 |
@Override |
| 473 |
public byte[] array() { |
| 474 |
return buffer.array(); |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
public int arrayOffset() { |
| 479 |
return buffer.arrayOffset(); |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
public ReadableBuffer reclaimRead() { |
| 484 |
// Don't compact ByteBuffer due to the expense of the copy |
| 485 |
return this; |
| 486 |
} |
| 487 |
|
| 488 |
@Override |
| 489 |
public ReadableBuffer mark() { |
| 490 |
buffer.mark(); |
| 491 |
return this; |
| 492 |
} |
| 493 |
|
| 494 |
@Override |
| 495 |
public ReadableBuffer reset() { |
| 496 |
buffer.reset(); |
| 497 |
return this; |
| 498 |
} |
| 499 |
|
| 500 |
@Override |
| 501 |
public ReadableBuffer rewind() { |
| 502 |
buffer.rewind(); |
| 503 |
return this; |
| 504 |
} |
| 505 |
|
| 506 |
@Override |
| 507 |
public ReadableBuffer clear() { |
| 508 |
buffer.clear(); |
| 509 |
return this; |
| 510 |
} |
| 511 |
|
| 512 |
@Override |
| 513 |
public ReadableBuffer get(WritableBuffer target) { |
| 514 |
target.put(buffer); |
| 515 |
return this; |
| 516 |
} |
| 517 |
|
| 518 |
@Override |
| 519 |
public String toString() { |
| 520 |
return buffer.toString(); |
| 521 |
} |
| 522 |
|
| 523 |
@Override |
| 524 |
public int hashCode() { |
| 525 |
return buffer.hashCode(); |
| 526 |
} |
| 527 |
|
| 528 |
@Override |
| 529 |
public boolean equals(Object other) { |
| 530 |
if (this == other) { |
| 531 |
return true; |
| 532 |
} |
| 533 |
|
| 534 |
if (!(other instanceof ReadableBuffer)) { |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
ReadableBuffer readable = (ReadableBuffer) other; |
| 539 |
if (this.remaining() != readable.remaining()) { |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
if (other instanceof CompositeReadableBuffer) { |
| 544 |
return other.equals(this); |
| 545 |
} |
| 546 |
|
| 547 |
return buffer.equals(readable.byteBuffer()); |
| 548 |
} |
| 549 |
} |
| 550 |
} |