Here you can find the source of borrowByteBuffer(final int capacity)
public static final ByteBuffer borrowByteBuffer(final int capacity)
//package com.java2s; /* Copyright (c) 2010 Xiaoyun Zhu * /*from w ww. j a va2 s.c om*/ * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; public class Main { private static final List<ByteBuffer> byteBuffersPoolLarge = new ArrayList<ByteBuffer>(); private static final List<ByteBuffer> byteBuffersPoolMedium = new ArrayList<ByteBuffer>(); private static final List<ByteBuffer> byteBuffersPoolNormal = new ArrayList<ByteBuffer>(); private static final List<ByteBuffer> byteBuffersPoolSmall = new ArrayList<ByteBuffer>(); private static final List<ByteBuffer> byteBuffersPoolVeryLarge = new ArrayList<ByteBuffer>(); public static final int MAX_LINE_BYTES_LARGE = 1024 * 64; public static final int MAX_LINE_BYTES_MEDIUM = 1024 * 32; public static final int MAX_LINE_BYTES_NORMAL = 1024 * 4; public static final int MAX_LINE_BYTES_SMALL = 1024; public static final int MAX_LINE_BYTES_VERY_LARGE = 1024 * 280; public static final ByteBuffer borrowByteBuffer(final int capacity) { if (capacity >= MAX_LINE_BYTES_VERY_LARGE) { return ByteBuffer.allocate(capacity); } else if (capacity >= MAX_LINE_BYTES_LARGE) { return borrowByteBufferVeryLarge(); } else if (capacity >= MAX_LINE_BYTES_MEDIUM) { return borrowByteBufferLarge(); } else if (capacity >= MAX_LINE_BYTES_NORMAL) { return borrowByteBufferMedium(); } else if (capacity >= MAX_LINE_BYTES_SMALL) { return borrowByteBufferNormal(); } else { return borrowByteBufferSmall(); } } public final static ByteBuffer borrowByteBufferVeryLarge() { if (byteBuffersPoolVeryLarge.isEmpty()) { return ByteBuffer.allocate(MAX_LINE_BYTES_VERY_LARGE); } else { return byteBuffersPoolVeryLarge.remove(0); } } public final static ByteBuffer borrowByteBufferLarge() { if (byteBuffersPoolLarge.isEmpty()) { return ByteBuffer.allocate(MAX_LINE_BYTES_LARGE); } else { return byteBuffersPoolLarge.remove(0); } } public final static ByteBuffer borrowByteBufferMedium() { if (byteBuffersPoolMedium.isEmpty()) { return ByteBuffer.allocate(MAX_LINE_BYTES_MEDIUM); } else { return byteBuffersPoolMedium.remove(0); } } public final static ByteBuffer borrowByteBufferNormal() { if (byteBuffersPoolNormal.isEmpty()) { return ByteBuffer.allocate(MAX_LINE_BYTES_NORMAL); } else { return byteBuffersPoolNormal.remove(0); } } public final static ByteBuffer borrowByteBufferSmall() { if (byteBuffersPoolSmall.isEmpty()) { return ByteBuffer.allocate(MAX_LINE_BYTES_SMALL); } else { return byteBuffersPoolSmall.remove(0); } } /** * position is 0 and limit = capacity. * * @param bb * @return */ public final static boolean isEmpty(final ByteBuffer bb) { return (bb.position() == 0 && bb.limit() == bb.capacity()) || bb.limit() == 0; } }