Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/xr/GrowableByteArray.java
32288 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.xr;2627import java.util.*;2829/**30* Growable int array, designed to allow subclasses to emulate31* the behaviour of value types.32*33* @author Clemens Eisserer34*/3536public class GrowableByteArray37{3839byte[] array;40int size;41int cellSize;4243public GrowableByteArray(int cellSize, int initialSize)44{45array = new byte[initialSize];46size = 0;47this.cellSize = cellSize;48}4950private int getNextCellIndex()51{52int oldSize = size;53size += cellSize;5455if (size >= array.length)56{57growArray();58}5960return oldSize;61}6263/**64* @return a direct reference to the backing array.65*/66public byte[] getArray()67{68return array;69}7071/**72* @return a copy of the backing array.73*/74public byte[] getSizedArray()75{76return Arrays.copyOf(array, getSize());77}7879public final int getByte(int index)80{81return array[getCellIndex(index)];82}8384/**85* Returns the index of the next free cell,86* and grows the backing arrays if required.87*/88public final int getNextIndex()89{90return getNextCellIndex() / cellSize;91}9293protected final int getCellIndex(int cellIndex)94{95return cellSize * cellIndex;96}9798public final void addByte(byte i)99{100int nextIndex = getNextIndex();101array[nextIndex] = i;102}103104/**105* @return The number of stored cells.106*/107public final int getSize()108{109return size / cellSize;110}111112public void clear()113{114size = 0;115}116117protected void growArray()118{119int newSize = Math.max(array.length * 2, 10);120byte[] oldArray = array;121array = new byte[newSize];122123System.arraycopy(oldArray, 0, array, 0, oldArray.length);124}125126}127128129