Java tutorial
//package com.java2s; /* Copyright (c) 2010 Xiaoyun Zhu * * 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 int MAX_BUFFER_SIZE = 10; 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 final static void giveBack(ByteBuffer bb) { if (bb != null) { final int capacity = bb.capacity(); if (capacity == MAX_LINE_BYTES_SMALL) { if (!byteBuffersPoolSmall.contains(bb) && byteBuffersPoolSmall.size() < MAX_BUFFER_SIZE) { byteBuffersPoolSmall.add(bb); } } else if (capacity == MAX_LINE_BYTES_MEDIUM) { if (!byteBuffersPoolMedium.contains(bb) && byteBuffersPoolMedium.size() < MAX_BUFFER_SIZE) { byteBuffersPoolMedium.add(bb); } } else if (capacity == MAX_LINE_BYTES_NORMAL) { if (!byteBuffersPoolNormal.contains(bb) && byteBuffersPoolNormal.size() < MAX_BUFFER_SIZE) { byteBuffersPoolNormal.add(bb); } } else if (capacity == MAX_LINE_BYTES_LARGE) { if (!byteBuffersPoolLarge.contains(bb) && byteBuffersPoolLarge.size() < MAX_BUFFER_SIZE) { byteBuffersPoolLarge.add(bb); } } else if (capacity == MAX_LINE_BYTES_VERY_LARGE) { if (!byteBuffersPoolLarge.contains(bb) && byteBuffersPoolLarge.size() < 2) { byteBuffersPoolLarge.add(bb); } } else { throw new IllegalArgumentException("Non compatible byte buffer size: " + capacity); } bb.clear(); } } public final static boolean contains(final ByteBuffer bb1, final ByteBuffer bb2) { return contains(bb1.array(), 0, bb1.limit(), bb2.array(), 0, bb2.limit()); } /** * * @param text * @param offset1 * @param end1 * absolute * @param s * @return */ public final static boolean contains(final byte[] array1, final int offset1, final int end1, final byte[] array2, final int offset2, final int end2) { final int len1 = end1 - offset1; final int len2 = end2 - offset2; if (len2 == 0) { return true; } else if (len1 >= len2) { byte b; int idx = offset2; for (int i = offset1; i < end1; i++) { b = array1[i]; if (b == array2[idx]) { if (++idx >= end2) { return true; } } else { idx = offset2; } } } return false; } public final static boolean contains(final byte[] array1, final int offset1, final int end2, final byte[] array2) { return contains(array1, offset1, end2, array2, 0, array2.length); } }