1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* $Id: ByteVector.java 1297284 2012-03-05 23:29:29Z gadams $ */ 19 20 package org.apache.fop.hyphenation; 21 22 import java.io.Serializable; 23 24 /** 25 * <p>This class implements a simple byte vector with access to the 26 * underlying array.</p> 27 * 28 * <p>This work was authored by Carlos Villegas (cav@uniscope.co.jp).</p> 29 */ 30 public class ByteVector implements Serializable { 31 32 private static final long serialVersionUID = 1554572867863466772L; 33 34 /** 35 * Capacity increment size 36 */ 37 private static final int DEFAULT_BLOCK_SIZE = 2048; 38 private int blockSize; 39 40 /** 41 * The encapsulated array 42 */ 43 private byte[] array; 44 45 /** 46 * Points to next free item 47 */ 48 private int n; 49 50 /** 51 * Construct byte vector instance with default block size. 52 */ ByteVector()53 public ByteVector() { 54 this(DEFAULT_BLOCK_SIZE); 55 } 56 57 /** 58 * Construct byte vector instance. 59 * @param capacity initial block size 60 */ ByteVector(int capacity)61 public ByteVector(int capacity) { 62 if (capacity > 0) { 63 blockSize = capacity; 64 } else { 65 blockSize = DEFAULT_BLOCK_SIZE; 66 } 67 array = new byte[blockSize]; 68 n = 0; 69 } 70 71 /** 72 * Construct byte vector instance. 73 * @param a byte array to use 74 * TODO should n should be initialized to a.length to be consistent with 75 * CharVector behavior? [GA] 76 */ ByteVector(byte[] a)77 public ByteVector(byte[] a) { 78 blockSize = DEFAULT_BLOCK_SIZE; 79 array = a; 80 n = 0; 81 } 82 83 /** 84 * Construct byte vector instance. 85 * @param a byte array to use 86 * @param capacity initial block size 87 * TODO should n should be initialized to a.length to be consistent with 88 * CharVector behavior? [GA] 89 */ ByteVector(byte[] a, int capacity)90 public ByteVector(byte[] a, int capacity) { 91 if (capacity > 0) { 92 blockSize = capacity; 93 } else { 94 blockSize = DEFAULT_BLOCK_SIZE; 95 } 96 array = a; 97 n = 0; 98 } 99 100 /** 101 * Obtain byte vector array. 102 * @return byte array 103 */ getArray()104 public byte[] getArray() { 105 return array; 106 } 107 108 /** 109 * Obtain number of items in array. 110 * @return number of items 111 */ length()112 public int length() { 113 return n; 114 } 115 116 /** 117 * Obtain capacity of array. 118 * @return current capacity of array 119 */ capacity()120 public int capacity() { 121 return array.length; 122 } 123 124 /** 125 * Pet byte at index. 126 * @param index the index 127 * @param val a byte 128 */ put(int index, byte val)129 public void put(int index, byte val) { 130 array[index] = val; 131 } 132 133 /** 134 * Get byte at index. 135 * @param index the index 136 * @return a byte 137 */ get(int index)138 public byte get(int index) { 139 return array[index]; 140 } 141 142 /** 143 * This is to implement memory allocation in the array. Like malloc(). 144 * @param size to allocate 145 * @return previous length 146 */ alloc(int size)147 public int alloc(int size) { 148 int index = n; 149 int len = array.length; 150 if (n + size >= len) { 151 byte[] aux = new byte[len + blockSize]; 152 System.arraycopy(array, 0, aux, 0, len); 153 array = aux; 154 } 155 n += size; 156 return index; 157 } 158 159 /** 160 * Trim byte vector to current length. 161 */ trimToSize()162 public void trimToSize() { 163 if (n < array.length) { 164 byte[] aux = new byte[n]; 165 System.arraycopy(array, 0, aux, 0, n); 166 array = aux; 167 } 168 } 169 170 } 171