/** * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated; * else; buffer has it's position/limit set appropriately. * * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated. * @param outputLength the minimum target size of the buffer * @param allowBufferResize true if resizing (reallocating) the buffer is allowed * @return {@code buf} if it was large enough, else a newly allocated buffer. */ public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) { BufferType bufferType = buf != null ? BufferType.typeOf(buf) : BufferType.ON_HEAP; return ensureCapacity(buf, outputLength, allowBufferResize, bufferType); }
/** * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated; * else; buffer has it's position/limit set appropriately. * * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated. * @param outputLength the minimum target size of the buffer * @param allowBufferResize true if resizing (reallocating) the buffer is allowed * @return {@code buf} if it was large enough, else a newly allocated buffer. */ public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) { BufferType bufferType = buf != null ? BufferType.typeOf(buf) : BufferType.ON_HEAP; return ensureCapacity(buf, outputLength, allowBufferResize, bufferType); }
/** * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated; * else; buffer has it's position/limit set appropriately. * * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated. * @param outputLength the minimum target size of the buffer * @param allowBufferResize true if resizing (reallocating) the buffer is allowed * @return {@code buf} if it was large enough, else a newly allocated buffer. */ public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) { BufferType bufferType = buf != null ? BufferType.typeOf(buf) : BufferType.ON_HEAP; return ensureCapacity(buf, outputLength, allowBufferResize, bufferType); }
/** * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated; * else; buffer has it's position/limit set appropriately. * * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated. * @param outputLength the minimum target size of the buffer * @param allowBufferResize true if resizing (reallocating) the buffer is allowed * @return {@code buf} if it was large enough, else a newly allocated buffer. */ public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) { BufferType bufferType = buf != null ? BufferType.typeOf(buf) : BufferType.ON_HEAP; return ensureCapacity(buf, outputLength, allowBufferResize, bufferType); }
/** * Uncompress the input data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough, * deallocate current, and allocate a large enough buffer. * * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity, * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times). */ public static ByteBuffer uncompress(ByteBuffer inputBuffer, ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor) throws IOException { int outputLength = inputBuffer.getInt(); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, outputLength, allowBufferResize); compressor.uncompress(inputBuffer, outputBuffer); outputBuffer.position(0).limit(outputLength); return outputBuffer; }
/** * Ensure {@code buf} is large enough for {@code outputLength}. If not, it is cleaned up and a new buffer is allocated; * else; buffer has it's position/limit set appropriately. * * @param buf buffer to test the size of; may be null, in which case, a new buffer is allocated. * @param outputLength the minimum target size of the buffer * @param allowBufferResize true if resizing (reallocating) the buffer is allowed * @return {@code buf} if it was large enough, else a newly allocated buffer. */ public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) { BufferType bufferType = buf != null ? BufferType.typeOf(buf) : BufferType.ON_HEAP; return ensureCapacity(buf, outputLength, allowBufferResize, bufferType); }
/** * Uncompress the input data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough, * deallocate current, and allocate a large enough buffer. * * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity, * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times). */ public static ByteBuffer uncompress(ByteBuffer inputBuffer, ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor) throws IOException { int outputLength = inputBuffer.getInt(); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, outputLength, allowBufferResize); compressor.uncompress(inputBuffer, outputBuffer); outputBuffer.position(0).limit(outputLength); return outputBuffer; }
/** * Uncompress the input data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough, * deallocate current, and allocate a large enough buffer. * * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity, * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times). */ public static ByteBuffer uncompress(ByteBuffer inputBuffer, ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor) throws IOException { int outputLength = inputBuffer.getInt(); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, outputLength, allowBufferResize); compressor.uncompress(inputBuffer, outputBuffer); outputBuffer.position(0).limit(outputLength); return outputBuffer; }
/** * Compress the raw data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough, * deallocate current, and allocate a large enough buffer. * Write the two header lengths (plain text length, compressed length) to the beginning of the buffer as we want those * values encapsulated in the encrypted block, as well. * * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity, * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times). */ public static ByteBuffer compress(ByteBuffer inputBuffer, ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor) throws IOException { int inputLength = inputBuffer.remaining(); final int compressedLength = compressor.initialCompressedBufferLength(inputLength); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, compressedLength + COMPRESSED_BLOCK_HEADER_SIZE, allowBufferResize); outputBuffer.putInt(inputLength); compressor.compress(inputBuffer, outputBuffer); outputBuffer.flip(); return outputBuffer; }
/** * Compress the raw data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough, * deallocate current, and allocate a large enough buffer. * Write the two header lengths (plain text length, compressed length) to the beginning of the buffer as we want those * values encapsulated in the encrypted block, as well. * * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity, * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times). */ public static ByteBuffer compress(ByteBuffer inputBuffer, ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor) throws IOException { int inputLength = inputBuffer.remaining(); final int compressedLength = compressor.initialCompressedBufferLength(inputLength); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, compressedLength + COMPRESSED_BLOCK_HEADER_SIZE, allowBufferResize); outputBuffer.putInt(inputLength); compressor.compress(inputBuffer, outputBuffer); outputBuffer.flip(); return outputBuffer; }
if (metadataBuffer.capacity() < ENCRYPTED_BLOCK_HEADER_SIZE) metadataBuffer = ByteBufferUtil.ensureCapacity(metadataBuffer, ENCRYPTED_BLOCK_HEADER_SIZE, true); reusableBuffers.set(metadataBuffer); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, Math.max(plainTextLength, encryptedLength), allowBufferResize); outputBuffer.position(0).limit(encryptedLength); channel.read(outputBuffer);
final int encryptLength = cipher.getOutputSize(plainTextLength); ByteBuffer outputBuffer = inputBuffer.duplicate(); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, encryptLength, allowBufferResize);
/** * Compress the raw data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough, * deallocate current, and allocate a large enough buffer. * Write the two header lengths (plain text length, compressed length) to the beginning of the buffer as we want those * values encapsulated in the encrypted block, as well. * * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity, * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times). */ public static ByteBuffer compress(ByteBuffer inputBuffer, ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor) throws IOException { int inputLength = inputBuffer.remaining(); final int compressedLength = compressor.initialCompressedBufferLength(inputLength); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, compressedLength + COMPRESSED_BLOCK_HEADER_SIZE, allowBufferResize); outputBuffer.putInt(inputLength); compressor.compress(inputBuffer, outputBuffer); outputBuffer.flip(); return outputBuffer; }
final int encryptLength = cipher.getOutputSize(plainTextLength); ByteBuffer outputBuffer = inputBuffer.duplicate(); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, encryptLength, allowBufferResize);
final int encryptLength = cipher.getOutputSize(plainTextLength); ByteBuffer outputBuffer = inputBuffer.duplicate(); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, encryptLength, allowBufferResize);
if (metadataBuffer.capacity() < ENCRYPTED_BLOCK_HEADER_SIZE) metadataBuffer = ByteBufferUtil.ensureCapacity(metadataBuffer, ENCRYPTED_BLOCK_HEADER_SIZE, true); reusableBuffers.set(metadataBuffer); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, Math.max(plainTextLength, encryptedLength), allowBufferResize); outputBuffer.position(0).limit(encryptedLength); channel.read(outputBuffer);
if (metadataBuffer.capacity() < ENCRYPTED_BLOCK_HEADER_SIZE) metadataBuffer = ByteBufferUtil.ensureCapacity(metadataBuffer, ENCRYPTED_BLOCK_HEADER_SIZE, true); reusableBuffers.set(metadataBuffer); outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, Math.max(plainTextLength, encryptedLength), allowBufferResize); outputBuffer.position(0).limit(encryptedLength); channel.read(outputBuffer);