1 30 31 package oracle.toplink.libraries.asm; 32 33 39 40 public class ByteVector { 41 42 45 46 byte[] data; 47 48 51 52 int length; 53 54 57 58 public ByteVector () { 59 data = new byte[64]; 60 } 61 62 67 68 public ByteVector (final int initialSize) { 69 data = new byte[initialSize]; 70 } 71 72 79 80 public ByteVector putByte (final int b) { 81 int length = this.length; 82 if (length + 1 > data.length) { 83 enlarge(1); 84 } 85 data[length++] = (byte)b; 86 this.length = length; 87 return this; 88 } 89 90 98 99 ByteVector put11 (final int b1, final int b2) { 100 int length = this.length; 101 if (length + 2 > data.length) { 102 enlarge(2); 103 } 104 byte[] data = this.data; 105 data[length++] = (byte)b1; 106 data[length++] = (byte)b2; 107 this.length = length; 108 return this; 109 } 110 111 118 119 public ByteVector putShort (final int s) { 120 int length = this.length; 121 if (length + 2 > data.length) { 122 enlarge(2); 123 } 124 byte[] data = this.data; 125 data[length++] = (byte)(s >>> 8); 126 data[length++] = (byte)s; 127 this.length = length; 128 return this; 129 } 130 131 139 140 ByteVector put12 (final int b, final int s) { 141 int length = this.length; 142 if (length + 3 > data.length) { 143 enlarge(3); 144 } 145 byte[] data = this.data; 146 data[length++] = (byte)b; 147 data[length++] = (byte)(s >>> 8); 148 data[length++] = (byte)s; 149 this.length = length; 150 return this; 151 } 152 153 160 161 public ByteVector putInt (final int i) { 162 int length = this.length; 163 if (length + 4 > data.length) { 164 enlarge(4); 165 } 166 byte[] data = this.data; 167 data[length++] = (byte)(i >>> 24); 168 data[length++] = (byte)(i >>> 16); 169 data[length++] = (byte)(i >>> 8); 170 data[length++] = (byte)i; 171 this.length = length; 172 return this; 173 } 174 175 182 183 public ByteVector putLong (final long l) { 184 int length = this.length; 185 if (length + 8 > data.length) { 186 enlarge(8); 187 } 188 byte[] data = this.data; 189 int i = (int)(l >>> 32); 190 data[length++] = (byte)(i >>> 24); 191 data[length++] = (byte)(i >>> 16); 192 data[length++] = (byte)(i >>> 8); 193 data[length++] = (byte)i; 194 i = (int)l; 195 data[length++] = (byte)(i >>> 24); 196 data[length++] = (byte)(i >>> 16); 197 data[length++] = (byte)(i >>> 8); 198 data[length++] = (byte)i; 199 this.length = length; 200 return this; 201 } 202 203 210 211 public ByteVector putUTF8 (final String s) { 212 int charLength = s.length(); 213 int byteLength = 0; 214 for (int i = 0; i < charLength; ++i) { 215 char c = s.charAt(i); 216 if (c >= '\001' && c <= '\177') { 217 byteLength++; 218 } else if (c > '\u07FF') { 219 byteLength += 3; 220 } else { 221 byteLength += 2; 222 } 223 } 224 if (byteLength > 65535) { 225 throw new IllegalArgumentException (); 226 } 227 int length = this.length; 228 if (length + 2 + byteLength > data.length) { 229 enlarge(2 + byteLength); 230 } 231 byte[] data = this.data; 232 data[length++] = (byte)(byteLength >>> 8); 233 data[length++] = (byte)(byteLength); 234 for (int i = 0; i < charLength; ++i) { 235 char c = s.charAt(i); 236 if (c >= '\001' && c <= '\177') { 237 data[length++] = (byte)c; 238 } else if (c > '\u07FF') { 239 data[length++] = (byte)(0xE0 | c >> 12 & 0xF); 240 data[length++] = (byte)(0x80 | c >> 6 & 0x3F); 241 data[length++] = (byte)(0x80 | c & 0x3F); 242 } else { 243 data[length++] = (byte)(0xC0 | c >> 6 & 0x1F); 244 data[length++] = (byte)(0x80 | c & 0x3F); 245 } 246 } 247 this.length = length; 248 return this; 249 } 250 251 261 262 public ByteVector putByteArray ( 263 final byte[] b, 264 final int off, 265 final int len) 266 { 267 if (length + len > data.length) { 268 enlarge(len); 269 } 270 if (b != null) { 271 System.arraycopy(b, off, data, length, len); 272 } 273 length += len; 274 return this; 275 } 276 277 283 284 private void enlarge (final int size) { 285 int length1 = 2 * data.length; 286 int length2 = length + size; 287 byte[] newData = new byte[length1 > length2 ? length1 : length2]; 288 System.arraycopy(data, 0, newData, 0, length); 289 data = newData; 290 } 291 } 292 | Popular Tags |