1 package fr.ove.utils;
2
3 public class ByteVector implements java.io.Serializable {
4 protected byte elementData[];
5 protected int elementCount;
6 protected int capacityIncrement;
7
8 public ByteVector(int initialCapacity, int capacityIncrement) {
9 super();
10 this.elementData = new byte[initialCapacity];
11 this.capacityIncrement = capacityIncrement;
12 }
13
14 public ByteVector(int initialCapacity) {
15 this(initialCapacity, 5);
16 }
17
18 public ByteVector() {
19 this(10);
20 }
21
22 public final synchronized void trimToSize() {
23 int oldCapacity = elementData.length;
24 if (elementCount < oldCapacity) {
25 byte oldData[] = elementData;
26 elementData = new byte[elementCount];
27 System.arraycopy(oldData, 0, elementData, 0, elementCount);
28 }
29 }
30
31 public final int size() {
32 return elementCount;
33 }
34
35 public final boolean isEmpty() {
36 return elementCount == 0;
37 }
38
39 public final boolean contains(byte elem) {
40 return indexOf(elem, 0) >= 0;
41 }
42
43 public final int indexOf(byte elem) {
44 return indexOf(elem, 0);
45 }
46
47 public final synchronized int indexOf(byte elem, int index) {
48 for (int i = index; i < elementCount; i++) {
49 if (elem == elementData[i])
50 return i;
51 }
52 return -1;
53 }
54
55 public final synchronized byte elementAt(int index) {
56 if (index >= elementCount)
57 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
58
59 try {
60 return elementData[index];
61 } catch (ArrayIndexOutOfBoundsException e) {
62 throw new ArrayIndexOutOfBoundsException(index + " < 0");
63 }
64 }
65
66 public final synchronized void addElement(byte b) {
67 ensureCapacity(elementCount + 1);
68 elementData[elementCount++] = b;
69 }
70
71 public final synchronized void addElements(byte[] b) {
72 if (b != null) {
73 ensureCapacity(elementCount + b.length);
74 System.arraycopy(b, 0, elementData, elementCount, b.length);
75 elementCount += b.length;
76 }
77 else
78 throw new NullPointerException("Try to add null elements");
79 }
80
81 public final synchronized void addElements(byte[] b, int length) {
82 if (b != null) {
83 if ((length > 0) && (length <= b.length)) {
84 ensureCapacity(elementCount + b.length);
85 System.arraycopy(b, 0, elementData, elementCount, length);
86 elementCount += length;
87 }
88 else
89 throw new IllegalArgumentException("Invalid length argument : " + length);
90 }
91 else
92 throw new NullPointerException("Try to add null elements");
93 }
94
95 public final synchronized boolean removeElement(byte b) {
96 int i = indexOf(b);
97 if (i >= 0) {
98 removeElementAt(i);
99 return true;
100 }
101 return false;
102 }
103
104 public final synchronized void removeElementAt(int index) {
105 if (index >= elementCount) {
106 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
107 }
108 else if (index < 0) {
109 throw new ArrayIndexOutOfBoundsException(index);
110 }
111 int j = elementCount - index - 1;
112 if (j > 0) {
113 System.arraycopy(elementData, index + 1, elementData, index, j);
114 }
115 elementCount--;
116 }
117
118 public final synchronized void removeAllElements() {
119 elementCount = 0;
120 elementData = new byte[capacityIncrement];
121 }
122
123 public final synchronized byte[] getBytes() {
124 trimToSize();
125 return elementData;
126 }
127
128 public final synchronized void ensureCapacity(int minCapacity) {
129 int oldCapacity = elementData.length;
130 if (minCapacity > oldCapacity) {
131 byte oldData[] = elementData;
132 int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 2);
133 if (newCapacity < minCapacity)
134 newCapacity = minCapacity;
135 elementData = new byte[newCapacity];
136 System.arraycopy(oldData, 0, elementData, 0, elementCount);
137 }
138 }
139
140 public final synchronized String toString() {
141 String s = " [ ";
142 for (int i = 0 ; i <elementCount ; i++)
143 s += elementData[i] + " ";
144 s += "]";
145 return s;
146 }
147 }