165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/* 265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project 365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License"); 565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License. 665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at 765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * http://www.apache.org/licenses/LICENSE-2.0 965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software 1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS, 1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and 1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License. 1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.util; 1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList; 2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.List; 2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic final class ByteArrayPool { 2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public static final int CHUNK16K = 16 * 1024; 2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public static final int DEFAULT_MAX_NUM = 8; 2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final static ByteArrayPool sChunk16K = new ByteArrayPool(CHUNK16K, DEFAULT_MAX_NUM); 2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final ArrayList<byte[]> mCachedBuf; 3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final int mChunkSize; 3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final int mMaxNum; 3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private ByteArrayPool(int chunkSize, int maxNum) { 3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mChunkSize = chunkSize; 3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mMaxNum = maxNum; 3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mCachedBuf = new ArrayList<byte[]>(mMaxNum); 3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane /** 4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * get singleton of 16KB byte[] pool 4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public static ByteArrayPool get16KBPool() { 4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return sChunk16K; 4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public byte[] allocateChunk() { 4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane synchronized (mCachedBuf) { 4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane int size = mCachedBuf.size(); 4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (size > 0) { 5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return mCachedBuf.remove(size - 1); 5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return new byte[mChunkSize]; 5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void clear() { 5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane synchronized (mCachedBuf) { 5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mCachedBuf.clear(); 5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void releaseChunk(byte[] buf) { 6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (buf == null || buf.length != mChunkSize) { 6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return; 6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane synchronized (mCachedBuf) { 6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mCachedBuf.size() < mMaxNum) { 6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mCachedBuf.add(buf); 6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void releaseChunks(List<byte[]> bufs) { 7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane synchronized (mCachedBuf) { 7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane for (int i = 0, c = bufs.size(); i < c; i++) { 7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mCachedBuf.size() == mMaxNum) { 7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane break; 7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane byte[] buf = bufs.get(i); 8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (buf != null && buf.length == mChunkSize) { 8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mCachedBuf.add(bufs.get(i)); 8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane} 88