Java Utililty Methods ByteBuffer Resize

List of utility methods to do ByteBuffer Resize

Description

The list of methods to do ByteBuffer Resize are organized into topic(s).

Method

booleancheck(BufferedImage image, int resizeWidth, int resizeHeight)
check
return (image.getHeight() > resizeHeight && resizeHeight > 0)
        || (image.getWidth() > resizeWidth && resizeWidth > 0);
ByteBufferincreaseBufferCapatity(ByteBuffer byteBuffer)
increase Buffer Capatity
if (byteBuffer == null) {
    throw new IllegalArgumentException("buffer is null");
int capacity = byteBuffer.capacity() + DEFAULT_INCREASE_BUFF_SIZE;
if (capacity < 0) {
    throw new IllegalArgumentException("capacity can't be negative");
ByteBuffer result = byteBuffer.isDirect() ? ByteBuffer.allocateDirect(capacity)
...
ByteBufferincreaseByteBuffer(ByteBuffer byteBuffer, int increase)
increase ByteBuffer
int capacity = byteBuffer.limit() + increase;
ByteBuffer result = (byteBuffer.isDirect() ? ByteBuffer.allocateDirect(capacity)
        : ByteBuffer.allocate(capacity));
result.order(byteBuffer.order());
byteBuffer.flip();
result.put(byteBuffer);
return result;
BufferedImageperformResize(BufferedImage source, int newWidth, int newHeight)
Resizes an image using nearest filtering.
BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = out.createGraphics();
g2d.setComposite(AlphaComposite.Src);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.drawImage(source, 0, 0, newWidth, newHeight, null);
g2d.dispose();
...
ByteBufferresize(ByteBuffer buffer)
Allocate a larger buffer that contains the data of an existing buffer.
int capacity = buffer.capacity();
if (capacity > 2048)
    capacity += 1024;
else
    capacity *= 2;
ByteBuffer newBuffer = ByteBuffer.allocate(capacity);
buffer.flip();
newBuffer.put(buffer);
...
ByteBufferresize(ByteBuffer oldBuffer, int newSize)
Resize the buffer specific size, cut off the tail
ByteBuffer newBuffer = ByteBuffer.allocate(newSize);
newSize = newSize - 1;
int oldSize = oldBuffer.limit() - 1;
int oldIndex = oldSize;
int to = newSize > oldSize ? newSize - oldSize : 0;
for (int newIndex = newSize; newIndex >= to; newIndex--) {
    newBuffer.put(newIndex, oldBuffer.get(oldIndex));
    oldIndex--;
...
ByteBufferresizeBuffer(final ByteBuffer in)
A helper method to resize byte buffer upon read.
if (in.remaining() < READ_BLOCK) {
    final ByteBuffer result = ByteBuffer.allocate(in.capacity() * 2);
    in.flip();
    result.put(in);
    return result;
return in;
ByteBufferresizeByteBuffer(ByteBuffer buf, long size)
resize Byte Buffer
ByteBuffer ret = ByteBuffer.allocateDirect((int) size);
if (ret != null) {
    if (null != buf) {
        ret.put(buf);
        ret.flip();
return ret;
...