1 51 package org.apache.fop.layout.hyphenation; 52 53 import java.io.Serializable ; 54 55 61 public class ByteVector implements Serializable { 62 63 66 private static final int DEFAULT_BLOCK_SIZE = 2048; 67 private int BLOCK_SIZE; 68 69 72 private byte[] array; 73 74 77 private int n; 78 79 public ByteVector() { 80 this(DEFAULT_BLOCK_SIZE); 81 } 82 83 public ByteVector(int capacity) { 84 if (capacity > 0) 85 BLOCK_SIZE = capacity; 86 else 87 BLOCK_SIZE = DEFAULT_BLOCK_SIZE; 88 array = new byte[BLOCK_SIZE]; 89 n = 0; 90 } 91 92 public ByteVector(byte[] a) { 93 BLOCK_SIZE = DEFAULT_BLOCK_SIZE; 94 array = a; 95 n = 0; 96 } 97 98 public ByteVector(byte[] a, int capacity) { 99 if (capacity > 0) 100 BLOCK_SIZE = capacity; 101 else 102 BLOCK_SIZE = DEFAULT_BLOCK_SIZE; 103 array = a; 104 n = 0; 105 } 106 107 public byte[] getArray() { 108 return array; 109 } 110 111 114 public int length() { 115 return n; 116 } 117 118 121 public int capacity() { 122 return array.length; 123 } 124 125 public void put(int index, byte val) { 126 array[index] = val; 127 } 128 129 public byte get(int index) { 130 return array[index]; 131 } 132 133 136 public int alloc(int size) { 137 int index = n; 138 int len = array.length; 139 if (n + size >= len) { 140 byte[] aux = new byte[len + BLOCK_SIZE]; 141 System.arraycopy(array, 0, aux, 0, len); 142 array = aux; 143 } 144 n += size; 145 return index; 146 } 147 148 public void trimToSize() { 149 if (n < array.length) { 150 byte[] aux = new byte[n]; 151 System.arraycopy(array, 0, aux, 0, n); 152 array = aux; 153 } 154 } 155 156 } 157 | Popular Tags |