1/*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.java2d.xr;
27
28import java.util.*;
29
30/**
31 * Growable int array, designed to allow subclasses to emulate
32 * the behaviour of value types.
33 *
34 * @author Clemens Eisserer
35 */
36
37public class GrowableByteArray
38{
39
40        byte[] array;
41        int size;
42        int cellSize;
43
44        public GrowableByteArray(int cellSize, int initialSize)
45        {
46                array = new byte[initialSize];
47                size = 0;
48                this.cellSize = cellSize;
49        }
50
51        private int getNextCellIndex()
52        {
53                int oldSize = size;
54                size += cellSize;
55
56                if (size >= array.length)
57                {
58                        growArray();
59                }
60
61                return oldSize;
62        }
63
64        /**
65         * @return a direct reference to the backing array.
66         */
67        public byte[] getArray()
68        {
69                return array;
70        }
71
72        /**
73         * @return a copy of the backing array.
74         */
75        public byte[] getSizedArray()
76        {
77                return Arrays.copyOf(array, getSize());
78        }
79
80        public final int getByte(int index)
81        {
82                return array[getCellIndex(index)];
83        }
84
85        /**
86         * Returns the index of the next free cell,
87         * and grows the backing arrays if required.
88         */
89        public final int getNextIndex()
90        {
91                return getNextCellIndex() / cellSize;
92        }
93
94        protected final int getCellIndex(int cellIndex)
95        {
96                return cellSize * cellIndex;
97        }
98
99        public final void addByte(byte i)
100        {
101            int nextIndex = getNextIndex();
102            array[nextIndex] = i;
103        }
104
105        /**
106         * @return The number of stored cells.
107         */
108        public final int getSize()
109        {
110                return size / cellSize;
111        }
112
113        public void clear()
114        {
115                size = 0;
116        }
117
118        protected void growArray()
119        {
120                int newSize = Math.max(array.length * 2, 10);
121                byte[] oldArray = array;
122                array = new byte[newSize];
123
124                System.arraycopy(oldArray, 0, array, 0, oldArray.length);
125        }
126
127}
128