1/*
2 * Copyright (C) 2010 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
17package java.lang;
18
19import java.nio.charset.Charset;
20
21/**
22 * A cheaper ByteArrayOutputStream for internal use. This class is unsynchronized,
23 * and returns its internal array if it's the right size. This makes String.getBytes("UTF-8")
24 * 10x faster than the baseline non-fast-path implementation instead of 8x faster when using
25 * ByteArrayOutputStream. When GC and uncontended synchronization become cheap, we should be
26 * able to get rid of this class. In the meantime, if you need to add further API, please try
27 * to keep it plug-compatible with ByteArrayOutputStream with an eye to future obsolescence.
28 *
29 * @hide
30 */
31public class UnsafeByteSequence {
32    private byte[] bytes;
33    private int count;
34
35    public UnsafeByteSequence(int initialCapacity) {
36        this.bytes = new byte[initialCapacity];
37    }
38
39    public int size() {
40        return count;
41    }
42
43    /**
44     * Moves the write pointer back to the beginning of the sequence,
45     * but without resizing or reallocating the buffer.
46     */
47    public void rewind() {
48        count = 0;
49    }
50
51    public void write(byte[] buffer, int offset, int length) {
52        if (count + length >= bytes.length) {
53            byte[] newBytes = new byte[(count + length) * 2];
54            System.arraycopy(bytes, 0, newBytes, 0, count);
55            bytes = newBytes;
56        }
57        System.arraycopy(buffer, offset, bytes, count, length);
58        count += length;
59    }
60
61    public void write(int b) {
62        if (count == bytes.length) {
63            byte[] newBytes = new byte[count * 2];
64            System.arraycopy(bytes, 0, newBytes, 0, count);
65            bytes = newBytes;
66        }
67        bytes[count++] = (byte) b;
68    }
69
70    @FindBugsSuppressWarnings("EI_EXPOSE_REP")
71    public byte[] toByteArray() {
72        if (count == bytes.length) {
73            return bytes;
74        }
75        byte[] result = new byte[count];
76        System.arraycopy(bytes, 0, result, 0, count);
77        return result;
78    }
79
80    public String toString(Charset cs) {
81        return new String(bytes, 0, count, cs);
82    }
83}
84