1 30 package org.objectweb.asm; 31 32 38 public class ByteVector { 39 40 43 byte[] data; 44 45 48 int length; 49 50 54 public ByteVector() { 55 data = new byte[64]; 56 } 57 58 64 public ByteVector(final int initialSize) { 65 data = new byte[initialSize]; 66 } 67 68 75 public ByteVector putByte(final int b) { 76 int length = this.length; 77 if (length + 1 > data.length) { 78 enlarge(1); 79 } 80 data[length++] = (byte) b; 81 this.length = length; 82 return this; 83 } 84 85 93 ByteVector put11(final int b1, final int b2) { 94 int length = this.length; 95 if (length + 2 > data.length) { 96 enlarge(2); 97 } 98 byte[] data = this.data; 99 data[length++] = (byte) b1; 100 data[length++] = (byte) b2; 101 this.length = length; 102 return this; 103 } 104 105 112 public ByteVector putShort(final int s) { 113 int length = this.length; 114 if (length + 2 > data.length) { 115 enlarge(2); 116 } 117 byte[] data = this.data; 118 data[length++] = (byte) (s >>> 8); 119 data[length++] = (byte) s; 120 this.length = length; 121 return this; 122 } 123 124 132 ByteVector put12(final int b, final int s) { 133 int length = this.length; 134 if (length + 3 > data.length) { 135 enlarge(3); 136 } 137 byte[] data = this.data; 138 data[length++] = (byte) b; 139 data[length++] = (byte) (s >>> 8); 140 data[length++] = (byte) s; 141 this.length = length; 142 return this; 143 } 144 145 152 public ByteVector putInt(final int i) { 153 int length = this.length; 154 if (length + 4 > data.length) { 155 enlarge(4); 156 } 157 byte[] data = this.data; 158 data[length++] = (byte) (i >>> 24); 159 data[length++] = (byte) (i >>> 16); 160 data[length++] = (byte) (i >>> 8); 161 data[length++] = (byte) i; 162 this.length = length; 163 return this; 164 } 165 166 173 public ByteVector putLong(final long l) { 174 int length = this.length; 175 if (length + 8 > data.length) { 176 enlarge(8); 177 } 178 byte[] data = this.data; 179 int i = (int) (l >>> 32); 180 data[length++] = (byte) (i >>> 24); 181 data[length++] = (byte) (i >>> 16); 182 data[length++] = (byte) (i >>> 8); 183 data[length++] = (byte) i; 184 i = (int) l; 185 data[length++] = (byte) (i >>> 24); 186 data[length++] = (byte) (i >>> 16); 187 data[length++] = (byte) (i >>> 8); 188 data[length++] = (byte) i; 189 this.length = length; 190 return this; 191 } 192 193 200 public ByteVector putUTF8(final String s) { 201 int charLength = s.length(); 202 if (length + 2 + charLength > data.length) { 203 enlarge(2 + charLength); 204 } 205 int len = length; 206 byte[] data = this.data; 207 data[len++] = (byte) (charLength >>> 8); 214 data[len++] = (byte) (charLength); 215 for (int i = 0; i < charLength; ++i) { 216 char c = s.charAt(i); 217 if (c >= '\001' && c <= '\177') { 218 data[len++] = (byte) c; 219 } else { 220 int byteLength = i; 221 for (int j = i; j < charLength; ++j) { 222 c = s.charAt(j); 223 if (c >= '\001' && c <= '\177') { 224 byteLength++; 225 } else if (c > '\u07FF') { 226 byteLength += 3; 227 } else { 228 byteLength += 2; 229 } 230 } 231 data[length] = (byte) (byteLength >>> 8); 232 data[length + 1] = (byte) (byteLength); 233 if (length + 2 + byteLength > data.length) { 234 length = len; 235 enlarge(2 + byteLength); 236 data = this.data; 237 } 238 for (int j = i; j < charLength; ++j) { 239 c = s.charAt(j); 240 if (c >= '\001' && c <= '\177') { 241 data[len++] = (byte) c; 242 } else if (c > '\u07FF') { 243 data[len++] = (byte) (0xE0 | c >> 12 & 0xF); 244 data[len++] = (byte) (0x80 | c >> 6 & 0x3F); 245 data[len++] = (byte) (0x80 | c & 0x3F); 246 } else { 247 data[len++] = (byte) (0xC0 | c >> 6 & 0x1F); 248 data[len++] = (byte) (0x80 | c & 0x3F); 249 } 250 } 251 break; 252 } 253 } 254 length = len; 255 return this; 256 } 257 258 268 public ByteVector putByteArray(final byte[] b, final int off, final int len) 269 { 270 if (length + len > data.length) { 271 enlarge(len); 272 } 273 if (b != null) { 274 System.arraycopy(b, off, data, length, len); 275 } 276 length += len; 277 return this; 278 } 279 280 286 private void enlarge(final int size) { 287 int length1 = 2 * data.length; 288 int length2 = length + size; 289 byte[] newData = new byte[length1 > length2 ? length1 : length2]; 290 System.arraycopy(data, 0, newData, 0, length); 291 data = newData; 292 } 293 } 294 | Popular Tags |