1 17 18 19 20 package org.apache.fop.hyphenation; 21 22 import java.io.Serializable ; 23 24 30 public class ByteVector implements Serializable { 31 32 35 private static final int DEFAULT_BLOCK_SIZE = 2048; 36 private int blockSize; 37 38 41 private byte[] array; 42 43 46 private int n; 47 48 public ByteVector() { 49 this(DEFAULT_BLOCK_SIZE); 50 } 51 52 public ByteVector(int capacity) { 53 if (capacity > 0) { 54 blockSize = capacity; 55 } else { 56 blockSize = DEFAULT_BLOCK_SIZE; 57 } 58 array = new byte[blockSize]; 59 n = 0; 60 } 61 62 public ByteVector(byte[] a) { 63 blockSize = DEFAULT_BLOCK_SIZE; 64 array = a; 65 n = 0; 66 } 67 68 public ByteVector(byte[] a, int capacity) { 69 if (capacity > 0) { 70 blockSize = capacity; 71 } else { 72 blockSize = DEFAULT_BLOCK_SIZE; 73 } 74 array = a; 75 n = 0; 76 } 77 78 public byte[] getArray() { 79 return array; 80 } 81 82 85 public int length() { 86 return n; 87 } 88 89 92 public int capacity() { 93 return array.length; 94 } 95 96 public void put(int index, byte val) { 97 array[index] = val; 98 } 99 100 public byte get(int index) { 101 return array[index]; 102 } 103 104 107 public int alloc(int size) { 108 int index = n; 109 int len = array.length; 110 if (n + size >= len) { 111 byte[] aux = new byte[len + blockSize]; 112 System.arraycopy(array, 0, aux, 0, len); 113 array = aux; 114 } 115 n += size; 116 return index; 117 } 118 119 public void trimToSize() { 120 if (n < array.length) { 121 byte[] aux = new byte[n]; 122 System.arraycopy(array, 0, aux, 0, n); 123 array = aux; 124 } 125 } 126 127 } 128 | Popular Tags |