Java ByteBuffer Size decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)

Here you can find the source of decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)

Description

decrease Buffer Capatity

License

Apache License

Parameter

Parameter Description
byteBuffer a parameter
size a parameter
minSize a parameter

Declaration

public static final ByteBuffer decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size,
        final int minSize) 

Method Source Code


//package com.java2s;
/*//from  ww w  .  j  av a2s  .  c  om
 * (C) 2007-2012 Alibaba Group Holding Limited.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * 
     * @param byteBuffer
     * @param size
     * @param minSize
     * @return
     */
    public static final ByteBuffer decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size,
            final int minSize) {
        if (byteBuffer == null) {
            throw new IllegalArgumentException("buffer is null");
        }

        if (minSize <= 0) {
            throw new IllegalArgumentException("minSize must be great than zero");
        }

        if (byteBuffer.capacity() == minSize) {
            return byteBuffer;
        }

        int capacity = byteBuffer.capacity() - size;
        if (capacity < minSize) {
            capacity = minSize;
        }
        if (capacity < byteBuffer.position()) {
            return byteBuffer;
        }
        final ByteBuffer result = byteBuffer.isDirect() ? ByteBuffer.allocateDirect(capacity)
                : ByteBuffer.allocate(capacity);
        result.order(byteBuffer.order());
        byteBuffer.flip();
        result.put(byteBuffer);
        return result;
    }

    public static final void flip(final ByteBuffer[] buffers) {
        if (buffers == null) {
            return;
        }
        for (final ByteBuffer buffer : buffers) {
            if (buffer != null) {
                buffer.flip();
            }
        }
    }
}

Related

  1. collidesWithImage(BufferedImage image, int x, int y, int width, int height, boolean pixelPerfect)
  2. convolve(final BufferedImage image, float[] elements, int theWidth, int theHeight)
  3. cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)
  4. cutAndScaleToRoundSquare(BufferedImage src, int size, int radius)
  5. cutAndScaleToSquare(BufferedImage src, int size)
  6. decryptData(final ByteBuffer buffer, final int size, final int key)
  7. depthToGrayBufferedImage(int[] depth, int width, int height)
  8. doThumb(BufferedImage from, int width, int height)
  9. enhance(int size, ByteBuffer bbuf)