Home | History | Annotate | Download | only in toolbox
      1 /*
      2  * Copyright (C) 2011 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 
     17 package com.android.volley.toolbox;
     18 
     19 import java.io.IOException;
     20 import java.util.Arrays;
     21 
     22 import android.test.AndroidTestCase;
     23 import android.test.suitebuilder.annotation.SmallTest;
     24 
     25 @SmallTest
     26 public class PoolingByteArrayOutputStreamTest extends AndroidTestCase {
     27     public void testPooledOneBuffer() throws IOException {
     28         ByteArrayPool pool = new ByteArrayPool(32768);
     29         writeOneBuffer(pool);
     30         writeOneBuffer(pool);
     31         writeOneBuffer(pool);
     32     }
     33 
     34     public void testPooledIndividualWrites() throws IOException {
     35         ByteArrayPool pool = new ByteArrayPool(32768);
     36         writeBytesIndividually(pool);
     37         writeBytesIndividually(pool);
     38         writeBytesIndividually(pool);
     39     }
     40 
     41     public void testUnpooled() throws IOException {
     42         ByteArrayPool pool = new ByteArrayPool(0);
     43         writeOneBuffer(pool);
     44         writeOneBuffer(pool);
     45         writeOneBuffer(pool);
     46     }
     47 
     48     public void testUnpooledIndividualWrites() throws IOException {
     49         ByteArrayPool pool = new ByteArrayPool(0);
     50         writeBytesIndividually(pool);
     51         writeBytesIndividually(pool);
     52         writeBytesIndividually(pool);
     53     }
     54 
     55     private void writeOneBuffer(ByteArrayPool pool) throws IOException {
     56         byte[] data = new byte[16384];
     57         for (int i = 0; i < data.length; i++) {
     58             data[i] = (byte) (i & 0xff);
     59         }
     60         PoolingByteArrayOutputStream os = new PoolingByteArrayOutputStream(pool);
     61         os.write(data);
     62 
     63         assertTrue(Arrays.equals(data, os.toByteArray()));
     64     }
     65 
     66     private void writeBytesIndividually(ByteArrayPool pool) {
     67         byte[] data = new byte[16384];
     68         for (int i = 0; i < data.length; i++) {
     69             data[i] = (byte) (i & 0xff);
     70         }
     71         PoolingByteArrayOutputStream os = new PoolingByteArrayOutputStream(pool);
     72         for (int i = 0; i < data.length; i++) {
     73             os.write(data[i]);
     74         }
     75 
     76         assertTrue(Arrays.equals(data, os.toByteArray()));
     77     }
     78 }
     79