Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 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 org.junit.Test;
20
 
21
import java.io.IOException;
22
import java.util.Arrays;
23
 
24
import static org.junit.Assert.assertTrue;
25
 
26
public class PoolingByteArrayOutputStreamTest {
27
    @Test public void pooledOneBuffer() throws IOException {
28
        ByteArrayPool pool = new ByteArrayPool(32768);
29
        writeOneBuffer(pool);
30
        writeOneBuffer(pool);
31
        writeOneBuffer(pool);
32
    }
33
 
34
    @Test public void pooledIndividualWrites() throws IOException {
35
        ByteArrayPool pool = new ByteArrayPool(32768);
36
        writeBytesIndividually(pool);
37
        writeBytesIndividually(pool);
38
        writeBytesIndividually(pool);
39
    }
40
 
41
    @Test public void unpooled() throws IOException {
42
        ByteArrayPool pool = new ByteArrayPool(0);
43
        writeOneBuffer(pool);
44
        writeOneBuffer(pool);
45
        writeOneBuffer(pool);
46
    }
47
 
48
    @Test public void unpooledIndividualWrites() 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
}