Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.car.apps.common;
     17 
     18 import java.util.ArrayList;
     19 import java.util.List;
     20 
     21 /**
     22  * @hide
     23  */
     24 public final class ByteArrayPool {
     25 
     26     public static final int CHUNK16K = 16 * 1024;
     27     public static final int DEFAULT_MAX_NUM = 8;
     28 
     29     private final static ByteArrayPool sChunk16K = new ByteArrayPool(CHUNK16K, DEFAULT_MAX_NUM);
     30 
     31     private final ArrayList<byte[]> mCachedBuf;
     32     private final int mChunkSize;
     33     private final int mMaxNum;
     34 
     35     private ByteArrayPool(int chunkSize, int maxNum) {
     36         mChunkSize = chunkSize;
     37         mMaxNum = maxNum;
     38         mCachedBuf = new ArrayList<byte[]>(mMaxNum);
     39     }
     40 
     41     /**
     42      * get singleton of 16KB byte[] pool
     43      */
     44     public static ByteArrayPool get16KBPool() {
     45         return sChunk16K;
     46     }
     47 
     48     public byte[] allocateChunk() {
     49         synchronized (mCachedBuf) {
     50             int size = mCachedBuf.size();
     51             if (size > 0) {
     52                 return mCachedBuf.remove(size - 1);
     53             }
     54             return new byte[mChunkSize];
     55         }
     56     }
     57 
     58     public void clear() {
     59         synchronized (mCachedBuf) {
     60             mCachedBuf.clear();
     61         }
     62     }
     63 
     64     public void releaseChunk(byte[] buf) {
     65         if (buf == null || buf.length != mChunkSize) {
     66             return;
     67         }
     68         synchronized (mCachedBuf) {
     69             if (mCachedBuf.size() < mMaxNum) {
     70                 mCachedBuf.add(buf);
     71             }
     72         }
     73     }
     74 
     75     public void releaseChunks(List<byte[]> bufs) {
     76         synchronized (mCachedBuf) {
     77             for (int i = 0, c = bufs.size(); i < c; i++) {
     78                 if (mCachedBuf.size() == mMaxNum) {
     79                     break;
     80                 }
     81                 byte[] buf = bufs.get(i);
     82                 if (buf != null && buf.length == mChunkSize) {
     83                     mCachedBuf.add(bufs.get(i));
     84                 }
     85             }
     86         }
     87     }
     88 
     89 }
     90