KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > hyphenation > ByteVector


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 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.hyphenation;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * This class implements a simple byte vector with access to the
26  * underlying array.
27  *
28  * @author Carlos Villegas <cav@uniscope.co.jp>
29  */

30 public class ByteVector implements Serializable JavaDoc {
31
32     /**
33      * Capacity increment size
34      */

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

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

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     /**
83      * return number of items in array
84      */

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

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     /**
105      * This is to implement memory allocation in the array. Like malloc().
106      */

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