| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
package org.doit.io; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class ByteArray |
| 31 |
{ |
| 32 |
public byte bytes[]; |
| 33 |
public int offset = 0; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public ByteArray () |
| 39 |
{ |
| 40 |
this (512); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public ByteArray (int size) |
| 47 |
{ |
| 48 |
bytes = new byte[size]; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public ByteArray (String s) |
| 55 |
{ |
| 56 |
this (s.length ()); |
| 57 |
append (s); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public ByteArray (byte b[]) |
| 64 |
{ |
| 65 |
this (b.length); |
| 66 |
append (b); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void append (byte ch) |
| 73 |
{ |
| 74 |
if (offset == bytes.length) |
| 75 |
{ |
| 76 |
byte tmpbytes[] = bytes; |
| 77 |
bytes = new byte[tmpbytes.length * 2]; |
| 78 |
System.arraycopy (tmpbytes, 0, bytes, 0, offset); |
| 79 |
} |
| 80 |
bytes[offset++] = ch; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public void append (ByteArray b) |
| 87 |
{ |
| 88 |
if (bytes.length - offset < b.length ()) |
| 89 |
{ |
| 90 |
byte tmpbytes[] = bytes; |
| 91 |
bytes = new byte[tmpbytes.length + b.length ()]; |
| 92 |
System.arraycopy (tmpbytes, 0, bytes, 0, offset); |
| 93 |
} |
| 94 |
System.arraycopy (b.bytes, 0, bytes, offset, b.length ()); |
| 95 |
offset += b.length (); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public void append (byte b[]) |
| 102 |
{ |
| 103 |
if (bytes.length - offset < b.length) |
| 104 |
{ |
| 105 |
byte tmpbytes[] = bytes; |
| 106 |
bytes = new byte[tmpbytes.length + b.length]; |
| 107 |
System.arraycopy (tmpbytes, 0, bytes, 0, offset); |
| 108 |
} |
| 109 |
System.arraycopy (b, 0, bytes, offset, b.length); |
| 110 |
offset += b.length; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void append (String s) |
| 117 |
{ |
| 118 |
append (s.getBytes ()); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public String toString () |
| 125 |
{ |
| 126 |
return new String (bytes, 0, offset); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public byte getBytes () [] |
| 133 |
{ |
| 134 |
return bytes; |
| 135 |
} |
| 136 |
|
| 137 |
public byte get (int i) |
| 138 |
{ |
| 139 |
return bytes[i]; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public int length () |
| 146 |
{ |
| 147 |
return offset; |
| 148 |
} |
| 149 |
|
| 150 |
public void erase () |
| 151 |
{ |
| 152 |
offset = 0; |
| 153 |
} |
| 154 |
|
| 155 |
public void chop () |
| 156 |
{ |
| 157 |
chop (1); |
| 158 |
} |
| 159 |
|
| 160 |
public void chop (int i) |
| 161 |
{ |
| 162 |
offset -= i; |
| 163 |
if (offset < 0) |
| 164 |
{ |
| 165 |
offset = 0; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public static void main (String args[]) |
| 170 |
{ |
| 171 |
ByteArray b = new ByteArray (3); |
| 172 |
b.append ("foo"); |
| 173 |
b.append ("bar"); |
| 174 |
b.append ("joe"); |
| 175 |
System.out.println (b.toString ()); |
| 176 |
|
| 177 |
ByteArray tmp = new ByteArray (1); |
| 178 |
tmp.append ("test"); |
| 179 |
b.append (tmp); |
| 180 |
System.out.println (b.toString ()); |
| 181 |
} |
| 182 |
} |