PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/com/mysql/jdbc/Buffer.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 674 lines | 431 code | 129 blank | 114 comment | 45 complexity | 3581aec49387bc692fc9a544a7ac2725 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. /*
  2. Copyright 2002-2005 MySQL AB, 2008-2010 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package com.mysql.jdbc;
  22. import java.io.UnsupportedEncodingException;
  23. import java.nio.ByteBuffer;
  24. import java.sql.SQLException;
  25. /**
  26. * Buffer contains code to read and write packets from/to the MySQL server.
  27. *
  28. * @version $Id$
  29. * @author Mark Matthews
  30. */
  31. public class Buffer {
  32. static final int MAX_BYTES_TO_DUMP = 512;
  33. static final int NO_LENGTH_LIMIT = -1;
  34. static final long NULL_LENGTH = -1;
  35. private int bufLength = 0;
  36. private byte[] byteBuffer;
  37. private int position = 0;
  38. protected boolean wasMultiPacket = false;
  39. Buffer(byte[] buf) {
  40. this.byteBuffer = buf;
  41. setBufLength(buf.length);
  42. }
  43. Buffer(int size) {
  44. this.byteBuffer = new byte[size];
  45. setBufLength(this.byteBuffer.length);
  46. this.position = MysqlIO.HEADER_LENGTH;
  47. }
  48. final void clear() {
  49. this.position = MysqlIO.HEADER_LENGTH;
  50. }
  51. final void dump() {
  52. dump(getBufLength());
  53. }
  54. final String dump(int numBytes) {
  55. return StringUtils.dumpAsHex(getBytes(0,
  56. numBytes > getBufLength() ? getBufLength() : numBytes),
  57. numBytes > getBufLength() ? getBufLength() : numBytes);
  58. }
  59. final String dumpClampedBytes(int numBytes) {
  60. int numBytesToDump = numBytes < MAX_BYTES_TO_DUMP ? numBytes
  61. : MAX_BYTES_TO_DUMP;
  62. String dumped = StringUtils.dumpAsHex(getBytes(0,
  63. numBytesToDump > getBufLength() ? getBufLength()
  64. : numBytesToDump),
  65. numBytesToDump > getBufLength() ? getBufLength()
  66. : numBytesToDump);
  67. if (numBytesToDump < numBytes) {
  68. return dumped + " ....(packet exceeds max. dump length)";
  69. }
  70. return dumped;
  71. }
  72. final void dumpHeader() {
  73. for (int i = 0; i < MysqlIO.HEADER_LENGTH; i++) {
  74. String hexVal = Integer.toHexString(readByte(i) & 0xff);
  75. if (hexVal.length() == 1) {
  76. hexVal = "0" + hexVal; //$NON-NLS-1$
  77. }
  78. System.out.print(hexVal + " "); //$NON-NLS-1$
  79. }
  80. }
  81. final void dumpNBytes(int start, int nBytes) {
  82. StringBuffer asciiBuf = new StringBuffer();
  83. for (int i = start; (i < (start + nBytes)) && (i < getBufLength()); i++) {
  84. String hexVal = Integer.toHexString(readByte(i) & 0xff);
  85. if (hexVal.length() == 1) {
  86. hexVal = "0" + hexVal; //$NON-NLS-1$
  87. }
  88. System.out.print(hexVal + " "); //$NON-NLS-1$
  89. if ((readByte(i) > 32) && (readByte(i) < 127)) {
  90. asciiBuf.append((char) readByte(i));
  91. } else {
  92. asciiBuf.append("."); //$NON-NLS-1$
  93. }
  94. asciiBuf.append(" "); //$NON-NLS-1$
  95. }
  96. System.out.println(" " + asciiBuf.toString()); //$NON-NLS-1$
  97. }
  98. final void ensureCapacity(int additionalData) throws SQLException {
  99. if ((this.position + additionalData) > getBufLength()) {
  100. if ((this.position + additionalData) < this.byteBuffer.length) {
  101. // byteBuffer.length is != getBufLength() all of the time
  102. // due to re-using of packets (we don't shrink them)
  103. //
  104. // If we can, don't re-alloc, just set buffer length
  105. // to size of current buffer
  106. setBufLength(this.byteBuffer.length);
  107. } else {
  108. //
  109. // Otherwise, re-size, and pad so we can avoid
  110. // allocing again in the near future
  111. //
  112. int newLength = (int) (this.byteBuffer.length * 1.25);
  113. if (newLength < (this.byteBuffer.length + additionalData)) {
  114. newLength = this.byteBuffer.length
  115. + (int) (additionalData * 1.25);
  116. }
  117. if (newLength < this.byteBuffer.length) {
  118. newLength = this.byteBuffer.length + additionalData;
  119. }
  120. byte[] newBytes = new byte[newLength];
  121. System.arraycopy(this.byteBuffer, 0, newBytes, 0,
  122. this.byteBuffer.length);
  123. this.byteBuffer = newBytes;
  124. setBufLength(this.byteBuffer.length);
  125. }
  126. }
  127. }
  128. /**
  129. * Skip over a length-encoded string
  130. *
  131. * @return The position past the end of the string
  132. */
  133. public int fastSkipLenString() {
  134. long len = this.readFieldLength();
  135. this.position += len;
  136. return (int) len;
  137. }
  138. public void fastSkipLenByteArray() {
  139. long len = this.readFieldLength();
  140. if (len == NULL_LENGTH || len == 0) {
  141. return;
  142. }
  143. this.position += len;
  144. }
  145. protected final byte[] getBufferSource() {
  146. return this.byteBuffer;
  147. }
  148. int getBufLength() {
  149. return this.bufLength;
  150. }
  151. /**
  152. * Returns the array of bytes this Buffer is using to read from.
  153. *
  154. * @return byte array being read from
  155. */
  156. public byte[] getByteBuffer() {
  157. return this.byteBuffer;
  158. }
  159. final byte[] getBytes(int len) {
  160. byte[] b = new byte[len];
  161. System.arraycopy(this.byteBuffer, this.position, b, 0, len);
  162. this.position += len; // update cursor
  163. return b;
  164. }
  165. /*
  166. * (non-Javadoc)
  167. *
  168. * @see com.mysql.jdbc.Buffer#getBytes(int, int)
  169. */
  170. byte[] getBytes(int offset, int len) {
  171. byte[] dest = new byte[len];
  172. System.arraycopy(this.byteBuffer, offset, dest, 0, len);
  173. return dest;
  174. }
  175. int getCapacity() {
  176. return this.byteBuffer.length;
  177. }
  178. public ByteBuffer getNioBuffer() {
  179. throw new IllegalArgumentException(Messages
  180. .getString("ByteArrayBuffer.0")); //$NON-NLS-1$
  181. }
  182. /**
  183. * Returns the current position to write to/ read from
  184. *
  185. * @return the current position to write to/ read from
  186. */
  187. public int getPosition() {
  188. return this.position;
  189. }
  190. // 2000-06-05 Changed
  191. final boolean isLastDataPacket() {
  192. return ((getBufLength() < 9) && ((this.byteBuffer[0] & 0xff) == 254));
  193. }
  194. final long newReadLength() {
  195. int sw = this.byteBuffer[this.position++] & 0xff;
  196. switch (sw) {
  197. case 251:
  198. return 0;
  199. case 252:
  200. return readInt();
  201. case 253:
  202. return readLongInt();
  203. case 254: // changed for 64 bit lengths
  204. return readLongLong();
  205. default:
  206. return sw;
  207. }
  208. }
  209. final byte readByte() {
  210. return this.byteBuffer[this.position++];
  211. }
  212. final byte readByte(int readAt) {
  213. return this.byteBuffer[readAt];
  214. }
  215. final long readFieldLength() {
  216. int sw = this.byteBuffer[this.position++] & 0xff;
  217. switch (sw) {
  218. case 251:
  219. return NULL_LENGTH;
  220. case 252:
  221. return readInt();
  222. case 253:
  223. return readLongInt();
  224. case 254:
  225. return readLongLong();
  226. default:
  227. return sw;
  228. }
  229. }
  230. // 2000-06-05 Changed
  231. final int readInt() {
  232. byte[] b = this.byteBuffer; // a little bit optimization
  233. return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8);
  234. }
  235. final int readIntAsLong() {
  236. byte[] b = this.byteBuffer;
  237. return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8)
  238. | ((b[this.position++] & 0xff) << 16)
  239. | ((b[this.position++] & 0xff) << 24);
  240. }
  241. final byte[] readLenByteArray(int offset) {
  242. long len = this.readFieldLength();
  243. if (len == NULL_LENGTH) {
  244. return null;
  245. }
  246. if (len == 0) {
  247. return Constants.EMPTY_BYTE_ARRAY;
  248. }
  249. this.position += offset;
  250. return getBytes((int) len);
  251. }
  252. final long readLength() {
  253. int sw = this.byteBuffer[this.position++] & 0xff;
  254. switch (sw) {
  255. case 251:
  256. return 0;
  257. case 252:
  258. return readInt();
  259. case 253:
  260. return readLongInt();
  261. case 254:
  262. return readLong();
  263. default:
  264. return sw;
  265. }
  266. }
  267. // 2000-06-05 Fixed
  268. final long readLong() {
  269. byte[] b = this.byteBuffer;
  270. return ((long) b[this.position++] & 0xff)
  271. | (((long) b[this.position++] & 0xff) << 8)
  272. | ((long) (b[this.position++] & 0xff) << 16)
  273. | ((long) (b[this.position++] & 0xff) << 24);
  274. }
  275. // 2000-06-05 Changed
  276. final int readLongInt() {
  277. byte[] b = this.byteBuffer;
  278. return (b[this.position++] & 0xff) | ((b[this.position++] & 0xff) << 8)
  279. | ((b[this.position++] & 0xff) << 16);
  280. }
  281. // 2000-06-05 Fixed
  282. final long readLongLong() {
  283. byte[] b = this.byteBuffer;
  284. return (b[this.position++] & 0xff)
  285. | ((long) (b[this.position++] & 0xff) << 8)
  286. | ((long) (b[this.position++] & 0xff) << 16)
  287. | ((long) (b[this.position++] & 0xff) << 24)
  288. | ((long) (b[this.position++] & 0xff) << 32)
  289. | ((long) (b[this.position++] & 0xff) << 40)
  290. | ((long) (b[this.position++] & 0xff) << 48)
  291. | ((long) (b[this.position++] & 0xff) << 56);
  292. }
  293. final int readnBytes() {
  294. int sw = this.byteBuffer[this.position++] & 0xff;
  295. switch (sw) {
  296. case 1:
  297. return this.byteBuffer[this.position++] & 0xff;
  298. case 2:
  299. return this.readInt();
  300. case 3:
  301. return this.readLongInt();
  302. case 4:
  303. return (int) this.readLong();
  304. default:
  305. return 255;
  306. }
  307. }
  308. //
  309. // Read a null-terminated string
  310. //
  311. // To avoid alloc'ing a new byte array, we
  312. // do this by hand, rather than calling getNullTerminatedBytes()
  313. //
  314. final String readString() {
  315. int i = this.position;
  316. int len = 0;
  317. int maxLen = getBufLength();
  318. while ((i < maxLen) && (this.byteBuffer[i] != 0)) {
  319. len++;
  320. i++;
  321. }
  322. String s = new String(this.byteBuffer, this.position, len);
  323. this.position += (len + 1); // update cursor
  324. return s;
  325. }
  326. final String readString(String encoding, ExceptionInterceptor exceptionInterceptor) throws SQLException {
  327. int i = this.position;
  328. int len = 0;
  329. int maxLen = getBufLength();
  330. while ((i < maxLen) && (this.byteBuffer[i] != 0)) {
  331. len++;
  332. i++;
  333. }
  334. try {
  335. return new String(this.byteBuffer, this.position, len, encoding);
  336. } catch (UnsupportedEncodingException uEE) {
  337. throw SQLError.createSQLException(Messages.getString("ByteArrayBuffer.1") //$NON-NLS-1$
  338. + encoding + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor); //$NON-NLS-1$
  339. } finally {
  340. this.position += (len + 1); // update cursor
  341. }
  342. }
  343. void setBufLength(int bufLengthToSet) {
  344. this.bufLength = bufLengthToSet;
  345. }
  346. /**
  347. * Sets the array of bytes to use as a buffer to read from.
  348. *
  349. * @param byteBuffer
  350. * the array of bytes to use as a buffer
  351. */
  352. public void setByteBuffer(byte[] byteBufferToSet) {
  353. this.byteBuffer = byteBufferToSet;
  354. }
  355. /**
  356. * Set the current position to write to/ read from
  357. *
  358. * @param position
  359. * the position (0-based index)
  360. */
  361. public void setPosition(int positionToSet) {
  362. this.position = positionToSet;
  363. }
  364. /**
  365. * Sets whether this packet was part of a multipacket
  366. *
  367. * @param flag
  368. * was this packet part of a multipacket?
  369. */
  370. public void setWasMultiPacket(boolean flag) {
  371. this.wasMultiPacket = flag;
  372. }
  373. public String toString() {
  374. return dumpClampedBytes(getPosition());
  375. }
  376. public String toSuperString() {
  377. return super.toString();
  378. }
  379. /**
  380. * Was this packet part of a multipacket?
  381. *
  382. * @return was this packet part of a multipacket?
  383. */
  384. public boolean wasMultiPacket() {
  385. return this.wasMultiPacket;
  386. }
  387. final void writeByte(byte b) throws SQLException {
  388. ensureCapacity(1);
  389. this.byteBuffer[this.position++] = b;
  390. }
  391. // Write a byte array
  392. final void writeBytesNoNull(byte[] bytes) throws SQLException {
  393. int len = bytes.length;
  394. ensureCapacity(len);
  395. System.arraycopy(bytes, 0, this.byteBuffer, this.position, len);
  396. this.position += len;
  397. }
  398. // Write a byte array with the given offset and length
  399. final void writeBytesNoNull(byte[] bytes, int offset, int length)
  400. throws SQLException {
  401. ensureCapacity(length);
  402. System.arraycopy(bytes, offset, this.byteBuffer, this.position, length);
  403. this.position += length;
  404. }
  405. final void writeDouble(double d) throws SQLException {
  406. long l = Double.doubleToLongBits(d);
  407. writeLongLong(l);
  408. }
  409. final void writeFieldLength(long length) throws SQLException {
  410. if (length < 251) {
  411. writeByte((byte) length);
  412. } else if (length < 65536L) {
  413. ensureCapacity(3);
  414. writeByte((byte) 252);
  415. writeInt((int) length);
  416. } else if (length < 16777216L) {
  417. ensureCapacity(4);
  418. writeByte((byte) 253);
  419. writeLongInt((int) length);
  420. } else {
  421. ensureCapacity(9);
  422. writeByte((byte) 254);
  423. writeLongLong(length);
  424. }
  425. }
  426. final void writeFloat(float f) throws SQLException {
  427. ensureCapacity(4);
  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. // 2000-06-05 Changed
  436. final void writeInt(int i) throws SQLException {
  437. ensureCapacity(2);
  438. byte[] b = this.byteBuffer;
  439. b[this.position++] = (byte) (i & 0xff);
  440. b[this.position++] = (byte) (i >>> 8);
  441. }
  442. // Write a String using the specified character
  443. // encoding
  444. final void writeLenBytes(byte[] b) throws SQLException {
  445. int len = b.length;
  446. ensureCapacity(len + 9);
  447. writeFieldLength(len);
  448. System.arraycopy(b, 0, this.byteBuffer, this.position, len);
  449. this.position += len;
  450. }
  451. // Write a String using the specified character
  452. // encoding
  453. final void writeLenString(String s, String encoding, String serverEncoding,
  454. SingleByteCharsetConverter converter, boolean parserKnowsUnicode,
  455. MySQLConnection conn)
  456. throws UnsupportedEncodingException, SQLException {
  457. byte[] b = null;
  458. if (converter != null) {
  459. b = converter.toBytes(s);
  460. } else {
  461. b = StringUtils.getBytes(s, encoding, serverEncoding,
  462. parserKnowsUnicode, conn, conn.getExceptionInterceptor());
  463. }
  464. int len = b.length;
  465. ensureCapacity(len + 9);
  466. writeFieldLength(len);
  467. System.arraycopy(b, 0, this.byteBuffer, this.position, len);
  468. this.position += len;
  469. }
  470. // 2000-06-05 Changed
  471. final void writeLong(long i) throws SQLException {
  472. ensureCapacity(4);
  473. byte[] b = this.byteBuffer;
  474. b[this.position++] = (byte) (i & 0xff);
  475. b[this.position++] = (byte) (i >>> 8);
  476. b[this.position++] = (byte) (i >>> 16);
  477. b[this.position++] = (byte) (i >>> 24);
  478. }
  479. // 2000-06-05 Changed
  480. final void writeLongInt(int i) throws SQLException {
  481. ensureCapacity(3);
  482. byte[] b = this.byteBuffer;
  483. b[this.position++] = (byte) (i & 0xff);
  484. b[this.position++] = (byte) (i >>> 8);
  485. b[this.position++] = (byte) (i >>> 16);
  486. }
  487. final void writeLongLong(long i) throws SQLException {
  488. ensureCapacity(8);
  489. byte[] b = this.byteBuffer;
  490. b[this.position++] = (byte) (i & 0xff);
  491. b[this.position++] = (byte) (i >>> 8);
  492. b[this.position++] = (byte) (i >>> 16);
  493. b[this.position++] = (byte) (i >>> 24);
  494. b[this.position++] = (byte) (i >>> 32);
  495. b[this.position++] = (byte) (i >>> 40);
  496. b[this.position++] = (byte) (i >>> 48);
  497. b[this.position++] = (byte) (i >>> 56);
  498. }
  499. // Write null-terminated string
  500. final void writeString(String s) throws SQLException {
  501. ensureCapacity((s.length() * 2) + 1);
  502. writeStringNoNull(s);
  503. this.byteBuffer[this.position++] = 0;
  504. }
  505. // Write null-terminated string in the given encoding
  506. final void writeString(String s, String encoding, MySQLConnection conn) throws SQLException {
  507. ensureCapacity((s.length() * 2) + 1);
  508. try {
  509. writeStringNoNull(s, encoding, encoding, false, conn);
  510. } catch (UnsupportedEncodingException ue) {
  511. throw new SQLException(ue.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
  512. }
  513. this.byteBuffer[this.position++] = 0;
  514. }
  515. // Write string, with no termination
  516. final void writeStringNoNull(String s) throws SQLException {
  517. int len = s.length();
  518. ensureCapacity(len * 2);
  519. System.arraycopy(s.getBytes(), 0, this.byteBuffer, this.position, len);
  520. this.position += len;
  521. // for (int i = 0; i < len; i++)
  522. // {
  523. // this.byteBuffer[this.position++] = (byte)s.charAt(i);
  524. // }
  525. }
  526. // Write a String using the specified character
  527. // encoding
  528. final void writeStringNoNull(String s, String encoding,
  529. String serverEncoding, boolean parserKnowsUnicode, MySQLConnection conn)
  530. throws UnsupportedEncodingException, SQLException {
  531. byte[] b = StringUtils.getBytes(s, encoding, serverEncoding,
  532. parserKnowsUnicode, conn, conn.getExceptionInterceptor());
  533. int len = b.length;
  534. ensureCapacity(len);
  535. System.arraycopy(b, 0, this.byteBuffer, this.position, len);
  536. this.position += len;
  537. }
  538. }