001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package com.lowagie.text.pdf.hyphenation;
018:
019: import java.io.Serializable;
020:
021: /**
022: * This class implements a simple byte vector with access to the
023: * underlying array.
024: *
025: * @author Carlos Villegas <cav@uniscope.co.jp>
026: */
027: public class ByteVector implements Serializable {
028:
029: private static final long serialVersionUID = -1096301185375029343L;
030: /**
031: * Capacity increment size
032: */
033: private static final int DEFAULT_BLOCK_SIZE = 2048;
034: private int blockSize;
035:
036: /**
037: * The encapsulated array
038: */
039: private byte[] array;
040:
041: /**
042: * Points to next free item
043: */
044: private int n;
045:
046: public ByteVector() {
047: this (DEFAULT_BLOCK_SIZE);
048: }
049:
050: public ByteVector(int capacity) {
051: if (capacity > 0) {
052: blockSize = capacity;
053: } else {
054: blockSize = DEFAULT_BLOCK_SIZE;
055: }
056: array = new byte[blockSize];
057: n = 0;
058: }
059:
060: public ByteVector(byte[] a) {
061: blockSize = DEFAULT_BLOCK_SIZE;
062: array = a;
063: n = 0;
064: }
065:
066: public ByteVector(byte[] a, int capacity) {
067: if (capacity > 0) {
068: blockSize = capacity;
069: } else {
070: blockSize = DEFAULT_BLOCK_SIZE;
071: }
072: array = a;
073: n = 0;
074: }
075:
076: public byte[] getArray() {
077: return array;
078: }
079:
080: /**
081: * return number of items in array
082: */
083: public int length() {
084: return n;
085: }
086:
087: /**
088: * returns current capacity of array
089: */
090: public int capacity() {
091: return array.length;
092: }
093:
094: public void put(int index, byte val) {
095: array[index] = val;
096: }
097:
098: public byte get(int index) {
099: return array[index];
100: }
101:
102: /**
103: * This is to implement memory allocation in the array. Like malloc().
104: */
105: public int alloc(int size) {
106: int index = n;
107: int len = array.length;
108: if (n + size >= len) {
109: byte[] aux = new byte[len + blockSize];
110: System.arraycopy(array, 0, aux, 0, len);
111: array = aux;
112: }
113: n += size;
114: return index;
115: }
116:
117: public void trimToSize() {
118: if (n < array.length) {
119: byte[] aux = new byte[n];
120: System.arraycopy(array, 0, aux, 0, n);
121: array = aux;
122: }
123: }
124:
125: }
|