1 /* |
1 /* |
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
462 public static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocate(0); |
462 public static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocate(0); |
463 public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0]; |
463 public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0]; |
464 public static final List<ByteBuffer> EMPTY_BB_LIST = List.of(); |
464 public static final List<ByteBuffer> EMPTY_BB_LIST = List.of(); |
465 public static final ByteBufferReference[] EMPTY_BBR_ARRAY = new ByteBufferReference[0]; |
465 public static final ByteBufferReference[] EMPTY_BBR_ARRAY = new ByteBufferReference[0]; |
466 |
466 |
467 public static ByteBuffer slice(ByteBuffer buffer, int amount) { |
467 /** |
|
468 * Returns a slice of size {@code amount} from the given buffer. If the |
|
469 * buffer contains more data than {@code amount}, then the slice's capacity |
|
470 * ( and, but not just, its limit ) is set to {@code amount}. If the buffer |
|
471 * does not contain more data than {@code amount}, then the slice's capacity |
|
472 * will be the same as the given buffer's capacity. |
|
473 */ |
|
474 public static ByteBuffer sliceWithLimitedCapacity(ByteBuffer buffer, int amount) { |
|
475 final int index = buffer.position() + amount; |
|
476 final int limit = buffer.limit(); |
|
477 if (index != limit) { |
|
478 // additional data in the buffer |
|
479 buffer.limit(index); // ensures that the slice does not go beyond |
|
480 } else { |
|
481 // no additional data in the buffer |
|
482 buffer.limit(buffer.capacity()); // allows the slice full capacity |
|
483 } |
|
484 |
468 ByteBuffer newb = buffer.slice(); |
485 ByteBuffer newb = buffer.slice(); |
469 newb.limit(amount); |
486 buffer.position(index); |
470 buffer.position(buffer.position() + amount); |
487 buffer.limit(limit); // restore the original buffer's limit |
|
488 newb.limit(amount); // slices limit to amount (capacity may be greater) |
471 return newb; |
489 return newb; |
472 } |
490 } |
473 |
491 |
474 /** |
492 /** |
475 * Get the Charset from the Content-encoding header. Defaults to |
493 * Get the Charset from the Content-encoding header. Defaults to |