KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > hyphenation > ByteVector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.lowagie.text.pdf.hyphenation;
18
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * This class implements a simple byte vector with access to the
23  * underlying array.
24  *
25  * @author Carlos Villegas <cav@uniscope.co.jp>
26  */

27 public class ByteVector implements Serializable JavaDoc {
28
29     private static final long serialVersionUID = -1096301185375029343L;
30     /**
31      * Capacity increment size
32      */

33     private static final int DEFAULT_BLOCK_SIZE = 2048;
34     private int blockSize;
35
36     /**
37      * The encapsulated array
38      */

39     private byte[] array;
40
41     /**
42      * Points to next free item
43      */

44     private int n;
45
46     public ByteVector() {
47         this(DEFAULT_BLOCK_SIZE);
48     }
49
50     public ByteVector(int capacity) {
51         if (capacity > 0) {
52             blockSize = capacity;
53         } else {
54             blockSize = DEFAULT_BLOCK_SIZE;
55         }
56         array = new byte[blockSize];
57         n = 0;
58     }
59
60     public ByteVector(byte[] a) {
61         blockSize = DEFAULT_BLOCK_SIZE;
62         array = a;
63         n = 0;
64     }
65
66     public ByteVector(byte[] a, int capacity) {
67         if (capacity > 0) {
68             blockSize = capacity;
69         } else {
70             blockSize = DEFAULT_BLOCK_SIZE;
71         }
72         array = a;
73         n = 0;
74     }
75
76     public byte[] getArray() {
77         return array;
78     }
79
80     /**
81      * return number of items in array
82      */

83     public int length() {
84         return n;
85     }
86
87     /**
88      * returns current capacity of array
89      */

90     public int capacity() {
91         return array.length;
92     }
93
94     public void put(int index, byte val) {
95         array[index] = val;
96     }
97
98     public byte get(int index) {
99         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 }
126
Popular Tags