Here you can find the source of decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)
Parameter | Description |
---|---|
byteBuffer | a parameter |
size | a parameter |
minSize | a parameter |
public static final ByteBuffer decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)
//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(); } } } }