1 34 35 package org.logicalcobwebs.asm; 36 37 41 42 final class ByteVector { 43 44 47 48 byte[] data; 49 50 53 54 int length; 55 56 59 60 public ByteVector () { 61 data = new byte[64]; 62 } 63 64 69 70 public ByteVector (final int initialSize) { 71 data = new byte[initialSize]; 72 } 73 74 81 82 public ByteVector put1 (final int b) { 83 int length = this.length; 84 if (length + 1 > data.length) { 85 enlarge(1); 86 } 87 data[length++] = (byte)b; 88 this.length = length; 89 return this; 90 } 91 92 100 101 public ByteVector put11 (final int b1, final int b2) { 102 int length = this.length; 103 if (length + 2 > data.length) { 104 enlarge(2); 105 } 106 byte[] data = this.data; 107 data[length++] = (byte)b1; 108 data[length++] = (byte)b2; 109 this.length = length; 110 return this; 111 } 112 113 120 121 public ByteVector put2 (final int s) { 122 int length = this.length; 123 if (length + 2 > data.length) { 124 enlarge(2); 125 } 126 byte[] data = this.data; 127 data[length++] = (byte)(s >>> 8); 128 data[length++] = (byte)s; 129 this.length = length; 130 return this; 131 } 132 133 141 142 public ByteVector put12 (final int b, final int s) { 143 int length = this.length; 144 if (length + 3 > data.length) { 145 enlarge(3); 146 } 147 byte[] data = this.data; 148 data[length++] = (byte)b; 149 data[length++] = (byte)(s >>> 8); 150 data[length++] = (byte)s; 151 this.length = length; 152 return this; 153 } 154 155 162 163 public ByteVector put4 (final int i) { 164 int length = this.length; 165 if (length + 4 > data.length) { 166 enlarge(4); 167 } 168 byte[] data = this.data; 169 data[length++] = (byte)(i >>> 24); 170 data[length++] = (byte)(i >>> 16); 171 data[length++] = (byte)(i >>> 8); 172 data[length++] = (byte)i; 173 this.length = length; 174 return this; 175 } 176 177 184 185 public ByteVector put8 (final long l) { 186 int length = this.length; 187 if (length + 8 > data.length) { 188 enlarge(8); 189 } 190 byte[] data = this.data; 191 int i = (int)(l >>> 32); 192 data[length++] = (byte)(i >>> 24); 193 data[length++] = (byte)(i >>> 16); 194 data[length++] = (byte)(i >>> 8); 195 data[length++] = (byte)i; 196 i = (int)l; 197 data[length++] = (byte)(i >>> 24); 198 data[length++] = (byte)(i >>> 16); 199 data[length++] = (byte)(i >>> 8); 200 data[length++] = (byte)i; 201 this.length = length; 202 return this; 203 } 204 205 212 213 public ByteVector putUTF (final String s) { 214 int charLength = s.length(); 215 int byteLength = 0; 216 for (int i = 0; i < charLength; ++i) { 217 char c = s.charAt(i); 218 if (c >= '\001' && c <= '\177') { 219 byteLength++; 220 } else if (c > '\u07FF') { 221 byteLength += 3; 222 } else { 223 byteLength += 2; 224 } 225 } 226 if (byteLength > 65535) { 227 throw new IllegalArgumentException (); 228 } 229 int length = this.length; 230 if (length + 2 + byteLength > data.length) { 231 enlarge(2 + byteLength); 232 } 233 byte[] data = this.data; 234 data[length++] = (byte)(byteLength >>> 8); 235 data[length++] = (byte)(byteLength); 236 for (int i = 0; i < charLength; ++i) { 237 char c = s.charAt(i); 238 if (c >= '\001' && c <= '\177') { 239 data[length++] = (byte)c; 240 } else if (c > '\u07FF') { 241 data[length++] = (byte)(0xE0 | c >> 12 & 0xF); 242 data[length++] = (byte)(0x80 | c >> 6 & 0x3F); 243 data[length++] = (byte)(0x80 | c & 0x3F); 244 } else { 245 data[length++] = (byte)(0xC0 | c >> 6 & 0x1F); 246 data[length++] = (byte)(0x80 | c & 0x3F); 247 } 248 } 249 this.length = length; 250 return this; 251 } 252 253 263 264 public ByteVector putByteArray ( 265 final byte[] b, 266 final int off, 267 final int len) 268 { 269 if (length + len > data.length) { 270 enlarge(len); 271 } 272 if (b != null) { 273 System.arraycopy(b, off, data, length, len); 274 } 275 length += len; 276 return this; 277 } 278 279 285 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 |