Expands buffer to the given size unless it is already as big or bigger. Buffers are assumed to be in 'write to' mode since there would be no need to expand a buffer while it was in 'read from' mode. @param in Buffer to expand @param newSize The size t which the buffer should be expanded @retu
(ByteBuffer in, int newSize)
| 37 | * was no need for expansion |
| 38 | */ |
| 39 | public static ByteBuffer expand(ByteBuffer in, int newSize) { |
| 40 | if (in.capacity() >= newSize) { |
| 41 | return in; |
| 42 | } |
| 43 | |
| 44 | ByteBuffer out; |
| 45 | boolean direct = false; |
| 46 | if (in.isDirect()) { |
| 47 | out = ByteBuffer.allocateDirect(newSize); |
| 48 | direct = true; |
| 49 | } else { |
| 50 | out = ByteBuffer.allocate(newSize); |
| 51 | } |
| 52 | |
| 53 | // Copy data |
| 54 | in.flip(); |
| 55 | out.put(in); |
| 56 | |
| 57 | if (direct) { |
| 58 | cleanDirectBuffer(in); |
| 59 | } |
| 60 | |
| 61 | return out; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Clean specified direct buffer. This will cause an unavoidable warning on Java 24 and newer. |
no test coverage detected