2370N/A/*
2685N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2370N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2370N/A *
2370N/A * This code is free software; you can redistribute it and/or modify it
2370N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Oracle designates this
2370N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Oracle in the LICENSE file that accompanied this code.
2370N/A *
2370N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2370N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2370N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2370N/A * version 2 for more details (a copy is included in the LICENSE file that
2370N/A * accompanied this code).
2370N/A *
2370N/A * You should have received a copy of the GNU General Public License version
2370N/A * 2 along with this work; if not, write to the Free Software Foundation,
2370N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2370N/A *
2685N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2685N/A * or visit www.oracle.com if you need additional information or have any
2685N/A * questions.
2370N/A */
2370N/A
2370N/Apackage sun.java2d.xr;
2370N/A
2370N/Aimport java.util.*;
2370N/A
2370N/A/**
2370N/A * Growable int array, designed to allow subclasses to emulate
2370N/A * the behaviour of value types.
2370N/A *
2370N/A * @author Clemens Eisserer
2370N/A */
2370N/A
2370N/Apublic class GrowableByteArray
2370N/A{
2370N/A
2370N/A byte[] array;
2370N/A int size;
2370N/A int cellSize;
2370N/A
2370N/A public GrowableByteArray(int cellSize, int initialSize)
2370N/A {
2370N/A array = new byte[initialSize];
2370N/A size = 0;
2370N/A this.cellSize = cellSize;
2370N/A }
2370N/A
2370N/A private int getNextCellIndex()
2370N/A {
2370N/A int oldSize = size;
2370N/A size += cellSize;
2370N/A
2370N/A if (size >= array.length)
2370N/A {
2370N/A growArray();
2370N/A }
2370N/A
2370N/A return oldSize;
2370N/A }
2370N/A
2370N/A /**
2370N/A * @return a direct reference to the backing array.
2370N/A */
2370N/A public byte[] getArray()
2370N/A {
2370N/A return array;
2370N/A }
2370N/A
2370N/A /**
2370N/A * @return a copy of the backing array.
2370N/A */
2370N/A public byte[] getSizedArray()
2370N/A {
2370N/A return Arrays.copyOf(array, getSize());
2370N/A }
2370N/A
2370N/A public final int getByte(int index)
2370N/A {
2370N/A return array[getCellIndex(index)];
2370N/A }
2370N/A
2370N/A /**
2370N/A * Returns the index of the next free cell,
2370N/A * and grows the backing arrays if required.
2370N/A */
2370N/A public final int getNextIndex()
2370N/A {
2370N/A return getNextCellIndex() / cellSize;
2370N/A }
2370N/A
2370N/A protected final int getCellIndex(int cellIndex)
2370N/A {
2370N/A return cellSize * cellIndex;
2370N/A }
2370N/A
2370N/A public final void addByte(byte i)
2370N/A {
2370N/A int nextIndex = getNextIndex();
2370N/A array[nextIndex] = i;
2370N/A }
2370N/A
2370N/A /**
2370N/A * @return The number of stored cells.
2370N/A */
2370N/A public final int getSize()
2370N/A {
2370N/A return size / cellSize;
2370N/A }
2370N/A
2370N/A public void clear()
2370N/A {
2370N/A size = 0;
2370N/A }
2370N/A
2370N/A protected void growArray()
2370N/A {
2370N/A int newSize = Math.max(array.length * 2, 10);
2370N/A byte[] oldArray = array;
2370N/A array = new byte[newSize];
2370N/A
2370N/A System.arraycopy(oldArray, 0, array, 0, oldArray.length);
2370N/A }
2370N/A
2370N/A}