Rev 20 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
30 | daniel-mar | 1 | package de.viathinksoft.immortal.genX; |
20 | daniel-mar | 2 | |
3 | /** |
||
4 | * |
||
5 | * @author Daniel Marschall |
||
6 | * |
||
7 | */ |
||
8 | public class ByteArray { |
||
9 | |||
10 | private int expansionSize; |
||
11 | private byte[] a; |
||
12 | private int top = -1; |
||
13 | |||
14 | public ByteArray() { |
||
15 | this(100, 1000); |
||
16 | } |
||
17 | |||
18 | public ByteArray(int initialSize, int expansionSize) { |
||
19 | this.a = new byte[initialSize]; |
||
20 | this.expansionSize = expansionSize; |
||
21 | } |
||
22 | |||
23 | private void expand() { |
||
24 | byte[] b = a; |
||
25 | |||
26 | a = new byte[b.length + expansionSize]; |
||
27 | |||
28 | for (int i = 0; i < b.length; i++) { |
||
29 | a[i] = b[i]; |
||
30 | } |
||
31 | } |
||
32 | |||
33 | public void add(byte x) { |
||
34 | top++; |
||
35 | if (top >= a.length) { |
||
36 | expand(); |
||
37 | } |
||
38 | a[top] = x; |
||
39 | } |
||
40 | |||
41 | public byte remove() { |
||
42 | return a[top--]; |
||
43 | } |
||
44 | |||
45 | public byte get(int i) { |
||
46 | return a[i]; |
||
47 | } |
||
48 | |||
49 | public void set(int i, byte x) { |
||
50 | a[i] = x; |
||
51 | } |
||
52 | |||
53 | public int count() { |
||
54 | return top + 1; |
||
55 | } |
||
56 | |||
57 | public void clear() { |
||
58 | top = -1; |
||
59 | } |
||
60 | |||
61 | public boolean isEmpty() { |
||
62 | return top == -1; |
||
63 | } |
||
64 | |||
65 | @Override |
||
66 | public String toString() { |
||
67 | StringBuilder x = new StringBuilder(); |
||
68 | for (int i = 0; i <= top; i++) { |
||
69 | x.append("" + a[i]); |
||
70 | } |
||
71 | return x.toString(); |
||
72 | } |
||
73 | |||
74 | } |