summaryrefslogtreecommitdiffstats
path: root/core/java/android/webkit/ByteArrayBuilder.java
blob: 145411cf7a6a65a9d2d583aeebe953af254e11f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.webkit;

import java.util.LinkedList;
import java.util.ListIterator;

/** Utility class optimized for accumulating bytes, and then spitting
    them back out.  It does not optimize for returning the result in a
    single array, though this is supported in the API. It is fastest
    if the retrieval can be done via iterating through chunks.

    Things to add:
      - consider dynamically increasing our min_capacity,
        as we see mTotalSize increase
*/
class ByteArrayBuilder {

    private static final int DEFAULT_CAPACITY = 8192;

    private LinkedList<Chunk> mChunks;

    /** free pool */
    private LinkedList<Chunk> mPool;

    private int mMinCapacity;

    public ByteArrayBuilder() {
        init(0);
    }

    public ByteArrayBuilder(int minCapacity) {
        init(minCapacity);
    }

    private void init(int minCapacity) {
        mChunks = new LinkedList<Chunk>();
        mPool = new LinkedList<Chunk>();

        if (minCapacity <= 0) {
            minCapacity = DEFAULT_CAPACITY;
        }
        mMinCapacity = minCapacity;
    }

    public void append(byte[] array) {
        append(array, 0, array.length);
    }

    public synchronized void append(byte[] array, int offset, int length) {
        while (length > 0) {
            Chunk c = appendChunk(length);
            int amount = Math.min(length, c.mArray.length - c.mLength);
            System.arraycopy(array, offset, c.mArray, c.mLength, amount);
            c.mLength += amount;
            length -= amount;
            offset += amount;
        }
    }

    /**
     * The fastest way to retrieve the data is to iterate through the
     * chunks.  This returns the first chunk.  Note: this pulls the
     * chunk out of the queue.  The caller must call releaseChunk() to
     * dispose of it.
     */
    public synchronized Chunk getFirstChunk() {
        if (mChunks.isEmpty()) return null;
        return mChunks.removeFirst();
    }

    /**
     * recycles chunk
     */
    public synchronized void releaseChunk(Chunk c) {
        c.mLength = 0;
        mPool.addLast(c);
    }

    public boolean isEmpty() {
        return mChunks.isEmpty();
    }

    public int size() {
        return mChunks.size();
    }

    public int getByteSize() {
        int total = 0;
        ListIterator<Chunk> it = mChunks.listIterator(0);
        while (it.hasNext()) {
            Chunk c = it.next();
            total += c.mLength;
        }
        return total;
    }

    public synchronized void clear() {
        Chunk c = getFirstChunk();
        while (c != null) {
            releaseChunk(c);
            c = getFirstChunk();
        }
    }

    private Chunk appendChunk(int length) {
        if (length < mMinCapacity) {
            length = mMinCapacity;
        }

        Chunk c;
        if (mChunks.isEmpty()) {
            c = obtainChunk(length);
        } else {
            c = mChunks.getLast();
            if (c.mLength == c.mArray.length) {
                c = obtainChunk(length);
            }
        }
        return c;
    }

    private Chunk obtainChunk(int length) {
        Chunk c;
        if (mPool.isEmpty()) {
            c = new Chunk(length);
        } else {
            c = mPool.removeFirst();
        }
        mChunks.addLast(c);
        return c;
    }

    public static class Chunk {
        public byte[]  mArray;
        public int     mLength;

        public Chunk(int length) {
            mArray = new byte[length];
            mLength = 0;
        }
    }
}