1 19 package com.mysql.jdbc; 20 21 import java.io.UnsupportedEncodingException ; 22 23 import java.sql.SQLException ; 24 25 26 32 class Buffer { 33 static final int NO_LENGTH_LIMIT = -1; 34 static final long NULL_LENGTH = -1; 35 private byte[] byteBuffer; 36 private boolean wasMultiPacket = false; 37 private int bufLength = 0; 38 private int position = 0; 39 private int sendLength = 0; 40 41 Buffer(byte[] buf) { 42 this.byteBuffer = buf; 43 setBufLength(buf.length); 44 } 45 46 Buffer(int size) { 47 this.byteBuffer = new byte[size]; 48 setBufLength(this.byteBuffer.length); 49 this.position = MysqlIO.HEADER_LENGTH; 50 } 51 52 57 public void setByteBuffer(byte[] byteBuffer) { 58 this.byteBuffer = byteBuffer; 59 } 60 61 66 public byte[] getByteBuffer() { 67 return this.byteBuffer; 68 } 69 70 75 public void setPosition(int position) { 76 this.position = position; 77 } 78 79 84 public int getPosition() { 85 return this.position; 86 } 87 88 93 public void setWasMultiPacket(boolean flag) { 94 wasMultiPacket = flag; 95 } 96 97 102 public int fastSkipLenString() { 103 long len = this.readFieldLength(); 104 105 position += len; 106 107 return (int) len; } 109 110 115 public boolean wasMultiPacket() { 116 return wasMultiPacket; 117 } 118 119 protected final byte[] getBufferSource() { 120 return byteBuffer; 121 } 122 123 final byte[] getBytes(int len) { 124 byte[] b = new byte[len]; 125 System.arraycopy(this.byteBuffer, this.position, b, 0, len); 126 this.position += len; 128 return b; 129 } 130 131 final boolean isLastDataPacket() { 133 return ((getBufLength() < 9) && ((this.byteBuffer[0] & 0xff) == 254)); 134 } 135 136 final void clear() { 137 this.position = MysqlIO.HEADER_LENGTH; 138 } 139 140 final void dump() { 141 StringUtils.dumpAsHex(this.byteBuffer, getBufLength()); 142 } 143 144 final void dumpHeader() { 145 for (int i = 0; i < MysqlIO.HEADER_LENGTH; i++) { 146 String hexVal = Integer.toHexString((int) this.byteBuffer[i] & 0xff); 147 148 if (hexVal.length() == 1) { 149 hexVal = "0" + hexVal; 150 } 151 152 System.out.print(hexVal + " "); 153 } 154 } 155 156 final void dumpNBytes(int start, int nBytes) { 157 StringBuffer asciiBuf = new StringBuffer (); 158 159 for (int i = start; 160 (i < (start + nBytes)) && (i < this.byteBuffer.length); i++) { 161 String hexVal = Integer.toHexString((int) this.byteBuffer[i] & 0xff); 162 163 if (hexVal.length() == 1) { 164 hexVal = "0" + hexVal; 165 } 166 167 System.out.print(hexVal + " "); 168 169 if ((this.byteBuffer[i] > 32) && (this.byteBuffer[i] < 127)) { 170 asciiBuf.append((char) this.byteBuffer[i]); 171 } else { 172 asciiBuf.append("."); 173 } 174 175 asciiBuf.append(" "); 176 } 177 178 System.out.println(" " + asciiBuf.toString()); 179 } 180 181 final void ensureCapacity(int additionalData) throws SQLException { 182 if ((this.position + additionalData) > getBufLength()) { 183 if ((this.position + additionalData) < this.byteBuffer.length) { 184 setBufLength(this.byteBuffer.length); 190 } else { 191 int newLength = (int) (this.byteBuffer.length * 1.25); 196 197 if (newLength < (this.byteBuffer.length + additionalData)) { 198 newLength = this.byteBuffer.length 199 + (int) (additionalData * 1.25); 200 } 201 202 if (newLength < this.byteBuffer.length) { 203 newLength = this.byteBuffer.length + additionalData; 204 } 205 206 byte[] newBytes = new byte[newLength]; 207 208 System.arraycopy(this.byteBuffer, 0, newBytes, 0, 209 this.byteBuffer.length); 210 this.byteBuffer = newBytes; 211 setBufLength(this.byteBuffer.length); 212 } 213 } 214 } 215 216 final long newReadLength() { 217 int sw = this.byteBuffer[this.position++] & 0xff; 218 219 switch (sw) { 220 case 251: 221 return (long) 0; 222 223 case 252: 224 return (long) readInt(); 225 226 case 253: 227 return (long) readLongInt(); 228 229 case 254: return (long) readLongLong(); 231 232 default: 233 return (long) sw; 234 } 235 } 236 237 final byte readByte() { 238 return this.byteBuffer[this.position++]; 239 } 240 241 final long readFieldLength() { 242 int sw = this.byteBuffer[this.position++] & 0xff; 243 244 switch (sw) { 245 case 251: 246 return NULL_LENGTH; 247 248 case 252: 249 return (long) readInt(); 250 251 case 253: 252 return (long) readLongInt(); 253 254 case 254: 255 return readLongLong(); 256 257 default: 258 return (long) sw; 259 } 260 } 261 262 final int readInt() { 264 byte[] b = this.byteBuffer; 266 return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8); 267 } 268 269 final byte[] readLenByteArray(int offset) { 270 long len = this.readFieldLength(); 271 272 if (len == NULL_LENGTH) { 273 return null; 274 } 275 276 if (len == 0) { 277 return new byte[0]; 278 } 279 280 this.position += offset; 281 282 return getBytes((int) len); 283 } 284 285 final long readLength() { 286 int sw = this.byteBuffer[this.position++] & 0xff; 287 288 switch (sw) { 289 case 251: 290 return (long) 0; 291 292 case 252: 293 return (long) readInt(); 294 295 case 253: 296 return (long) readLongInt(); 297 298 case 254: 299 return (long) readLong(); 300 301 default: 302 return (long) sw; 303 } 304 } 305 306 final long readLong() { 308 byte[] b = this.byteBuffer; 309 310 return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8) 311 | ((b[this.position++] & 0xff) << 16) 312 | ((b[this.position++] & 0xff) << 24); 313 } 314 315 final int readLongInt() { 317 byte[] b = this.byteBuffer; 318 319 return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8) 320 | ((b[this.position++] & 0xff) << 16); 321 } 322 323 final long readLongLong() { 325 byte[] b = this.byteBuffer; 326 327 return (long) (b[this.position++] & 0xff) 328 | ((long) (b[this.position++] & 0xff) << 8) 329 | ((long) (b[this.position++] & 0xff) << 16) 330 | ((long) (b[this.position++] & 0xff) << 24) 331 | ((long) (b[this.position++] & 0xff) << 32) 332 | ((long) (b[this.position++] & 0xff) << 40) 333 | ((long) (b[this.position++] & 0xff) << 48) 334 | ((long) (b[this.position++] & 0xff) << 56); 335 } 336 337 final String readString() { 344 int i = this.position; 345 int len = 0; 346 int maxLen = getBufLength(); 347 348 while ((i < maxLen) && (this.byteBuffer[i] != 0)) { 349 len++; 350 i++; 351 } 352 353 String s = new String (this.byteBuffer, this.position, len); 354 this.position += (len + 1); 356 return s; 357 } 358 359 final String readString(String encoding) throws SQLException { 360 int i = this.position; 361 int len = 0; 362 int maxLen = getBufLength(); 363 364 while ((i < maxLen) && (this.byteBuffer[i] != 0)) { 365 len++; 366 i++; 367 } 368 369 this.position += (len + 1); 371 try { 372 return new String (this.byteBuffer, this.position, len, encoding); 373 374 } catch (UnsupportedEncodingException uEE) { 375 throw new SQLException ("Unsupported character encoding '" + encoding + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 376 } 377 } 378 379 final int readnBytes() { 380 int sw = this.byteBuffer[this.position++] & 0xff; 381 382 switch (sw) { 383 case 1: 384 return this.byteBuffer[this.position++] & 0xff; 385 386 case 2: 387 return this.readInt(); 388 389 case 3: 390 return this.readLongInt(); 391 392 case 4: 393 return (int) this.readLong(); 394 395 default: 396 return 255; 397 } 398 } 399 400 final void writeByte(byte b) throws SQLException { 401 ensureCapacity(1); 402 403 this.byteBuffer[this.position++] = b; 404 } 405 406 final void writeBytesNoNull(byte[] bytes) throws SQLException { 408 int len = bytes.length; 409 ensureCapacity(len); 410 System.arraycopy(bytes, 0, this.byteBuffer, this.position, len); 411 this.position += len; 412 } 413 414 final void writeBytesNoNull(byte[] bytes, int offset, int length) 416 throws SQLException { 417 ensureCapacity(length); 418 System.arraycopy(bytes, offset, this.byteBuffer, this.position, length); 419 this.position += length; 420 } 421 422 final void writeDouble(double d) { 423 long l = Double.doubleToLongBits(d); 424 writeLongLong(l); 425 } 426 427 final void writeFloat(float f) { 428 int i = Float.floatToIntBits(f); 429 byte[] b = this.byteBuffer; 430 b[this.position++] = (byte) (i & 0xff); 431 b[this.position++] = (byte) (i >>> 8); 432 b[this.position++] = (byte) (i >>> 16); 433 b[this.position++] = (byte) (i >>> 24); 434 } 435 436 final void writeInt(int i) { 438 byte[] b = this.byteBuffer; 439 b[this.position++] = (byte) (i & 0xff); 440 b[this.position++] = (byte) (i >>> 8); 441 } 442 443 final void writeLong(long i) { 445 byte[] b = this.byteBuffer; 446 b[this.position++] = (byte) (i & 0xff); 447 b[this.position++] = (byte) (i >>> 8); 448 b[this.position++] = (byte) (i >>> 16); 449 b[this.position++] = (byte) (i >>> 24); 450 } 451 452 final void writeLongInt(int i) { 454 byte[] b = this.byteBuffer; 455 b[this.position++] = (byte) (i & 0xff); 456 b[this.position++] = (byte) (i >>> 8); 457 b[this.position++] = (byte) (i >>> 16); 458 } 459 460 final void writeLongLong(long i) { 461 byte[] b = this.byteBuffer; 462 b[this.position++] = (byte) (i & 0xff); 463 b[this.position++] = (byte) (i >>> 8); 464 b[this.position++] = (byte) (i >>> 16); 465 b[this.position++] = (byte) (i >>> 24); 466 b[this.position++] = (byte) (i >>> 32); 467 b[this.position++] = (byte) (i >>> 40); 468 b[this.position++] = (byte) (i >>> 48); 469 b[this.position++] = (byte) (i >>> 56); 470 } 471 472 final void writeString(String s) throws SQLException { 474 ensureCapacity(s.length() + 1); 475 476 writeStringNoNull(s); 477 this.byteBuffer[this.position++] = 0; 478 } 479 480 final void writeStringNoNull(String s) throws SQLException { 482 int len = s.length(); 483 ensureCapacity(len); 484 System.arraycopy(s.getBytes(), 0, byteBuffer, position, len); 485 position += len; 486 487 } 492 493 final void writeStringNoNull(String s, String encoding, SingleByteCharsetConverter converter) 496 throws UnsupportedEncodingException , SQLException { 497 byte[] b = null; 498 499 if (converter != null) { 500 b = converter.toBytes(s); 501 } else { 502 b = StringUtils.getBytes(s, encoding); 503 } 504 505 int len = b.length; 506 ensureCapacity(len); 507 System.arraycopy(b, 0, this.byteBuffer, this.position, len); 508 this.position += len; 509 } 510 511 void setBufLength(int bufLength) { 512 this.bufLength = bufLength; 513 } 514 515 int getBufLength() { 516 return bufLength; 517 } 518 519 void setSendLength(int sendLength) { 520 this.sendLength = sendLength; 521 } 522 } 523 | Popular Tags |