選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ByteUtil.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. Copyright (c) 2005 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess.impl;
  23. import java.io.Closeable;
  24. import java.io.FileWriter;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;
  28. import java.io.PrintWriter;
  29. import java.nio.ByteBuffer;
  30. import java.nio.ByteOrder;
  31. import java.util.Arrays;
  32. /**
  33. * Byte manipulation and display utilities
  34. * @author Tim McCune
  35. */
  36. public final class ByteUtil {
  37. private static final String[] HEX_CHARS = new String[] {
  38. "0", "1", "2", "3", "4", "5", "6", "7",
  39. "8", "9", "A", "B", "C", "D", "E", "F"};
  40. private static final int NUM_BYTES_PER_BLOCK = 4;
  41. private static final int NUM_BYTES_PER_LINE = 24;
  42. private ByteUtil() {}
  43. /**
  44. * Put an integer into the given buffer at the given offset as a 3-byte
  45. * integer.
  46. * @param buffer buffer into which to insert the int
  47. * @param val Int to convert
  48. */
  49. public static void put3ByteInt(ByteBuffer buffer, int val)
  50. {
  51. put3ByteInt(buffer, val, buffer.order());
  52. }
  53. /**
  54. * Put an integer into the given buffer at the given offset as a 3-byte
  55. * integer.
  56. * @param buffer buffer into which to insert the int
  57. * @param val Int to convert
  58. * @param order the order to insert the bytes of the int
  59. */
  60. public static void put3ByteInt(ByteBuffer buffer, int val, ByteOrder order)
  61. {
  62. int pos = buffer.position();
  63. put3ByteInt(buffer, val, pos, order);
  64. buffer.position(pos + 3);
  65. }
  66. /**
  67. * Put an integer into the given buffer at the given offset as a 3-byte
  68. * integer.
  69. * @param buffer buffer into which to insert the int
  70. * @param val Int to convert
  71. * @param offset offset at which to insert the int
  72. * @param order the order to insert the bytes of the int
  73. */
  74. public static void put3ByteInt(ByteBuffer buffer, int val, int offset,
  75. ByteOrder order) {
  76. int offInc = 1;
  77. if(order == ByteOrder.BIG_ENDIAN) {
  78. offInc = -1;
  79. offset += 2;
  80. }
  81. buffer.put(offset, (byte) (val & 0xFF));
  82. buffer.put(offset + (1 * offInc), (byte) ((val >>> 8) & 0xFF));
  83. buffer.put(offset + (2 * offInc), (byte) ((val >>> 16) & 0xFF));
  84. }
  85. /**
  86. * Read a 3 byte int from a buffer
  87. * @param buffer Buffer containing the bytes
  88. * @return The int
  89. */
  90. public static int get3ByteInt(ByteBuffer buffer) {
  91. return get3ByteInt(buffer, buffer.order());
  92. }
  93. /**
  94. * Read a 3 byte int from a buffer
  95. * @param buffer Buffer containing the bytes
  96. * @param order the order of the bytes of the int
  97. * @return The int
  98. */
  99. public static int get3ByteInt(ByteBuffer buffer, ByteOrder order) {
  100. int pos = buffer.position();
  101. int rtn = get3ByteInt(buffer, pos, order);
  102. buffer.position(pos + 3);
  103. return rtn;
  104. }
  105. /**
  106. * Read a 3 byte int from a buffer
  107. * @param buffer Buffer containing the bytes
  108. * @param offset Offset at which to start reading the int
  109. * @return The int
  110. */
  111. public static int get3ByteInt(ByteBuffer buffer, int offset) {
  112. return get3ByteInt(buffer, offset, buffer.order());
  113. }
  114. /**
  115. * Read a 3 byte int from a buffer
  116. * @param buffer Buffer containing the bytes
  117. * @param offset Offset at which to start reading the int
  118. * @param order the order of the bytes of the int
  119. * @return The int
  120. */
  121. public static int get3ByteInt(ByteBuffer buffer, int offset,
  122. ByteOrder order) {
  123. int offInc = 1;
  124. if(order == ByteOrder.BIG_ENDIAN) {
  125. offInc = -1;
  126. offset += 2;
  127. }
  128. int rtn = getUnsignedByte(buffer, offset);
  129. rtn += (getUnsignedByte(buffer, offset + (1 * offInc)) << 8);
  130. rtn += (getUnsignedByte(buffer, offset + (2 * offInc)) << 16);
  131. return rtn;
  132. }
  133. /**
  134. * Read an unsigned byte from a buffer
  135. * @param buffer Buffer containing the bytes
  136. * @return The unsigned byte as an int
  137. */
  138. public static int getUnsignedByte(ByteBuffer buffer) {
  139. int pos = buffer.position();
  140. int rtn = getUnsignedByte(buffer, pos);
  141. buffer.position(pos + 1);
  142. return rtn;
  143. }
  144. /**
  145. * Read an unsigned byte from a buffer
  146. * @param buffer Buffer containing the bytes
  147. * @param offset Offset at which to read the byte
  148. * @return The unsigned byte as an int
  149. */
  150. public static int getUnsignedByte(ByteBuffer buffer, int offset) {
  151. return asUnsignedByte(buffer.get(offset));
  152. }
  153. /**
  154. * Read an unsigned short from a buffer
  155. * @param buffer Buffer containing the short
  156. * @return The unsigned short as an int
  157. */
  158. public static int getUnsignedShort(ByteBuffer buffer) {
  159. int pos = buffer.position();
  160. int rtn = getUnsignedShort(buffer, pos);
  161. buffer.position(pos + 2);
  162. return rtn;
  163. }
  164. /**
  165. * Read an unsigned short from a buffer
  166. * @param buffer Buffer containing the short
  167. * @param offset Offset at which to read the short
  168. * @return The unsigned short as an int
  169. */
  170. public static int getUnsignedShort(ByteBuffer buffer, int offset) {
  171. return asUnsignedShort(buffer.getShort(offset));
  172. }
  173. /**
  174. * @param buffer Buffer containing the bytes
  175. * @param order the order of the bytes of the int
  176. * @return an int from the current position in the given buffer, read using
  177. * the given ByteOrder
  178. */
  179. public static int getInt(ByteBuffer buffer, ByteOrder order) {
  180. int offset = buffer.position();
  181. int rtn = getInt(buffer, offset, order);
  182. buffer.position(offset + 4);
  183. return rtn;
  184. }
  185. /**
  186. * @param buffer Buffer containing the bytes
  187. * @param offset Offset at which to start reading the int
  188. * @param order the order of the bytes of the int
  189. * @return an int from the given position in the given buffer, read using
  190. * the given ByteOrder
  191. */
  192. public static int getInt(ByteBuffer buffer, int offset, ByteOrder order) {
  193. ByteOrder origOrder = buffer.order();
  194. try {
  195. return buffer.order(order).getInt(offset);
  196. } finally {
  197. buffer.order(origOrder);
  198. }
  199. }
  200. /**
  201. * Writes an int at the current position in the given buffer, using the
  202. * given ByteOrder
  203. * @param buffer buffer into which to insert the int
  204. * @param val Int to insert
  205. * @param order the order to insert the bytes of the int
  206. */
  207. public static void putInt(ByteBuffer buffer, int val, ByteOrder order) {
  208. int offset = buffer.position();
  209. putInt(buffer, val, offset, order);
  210. buffer.position(offset + 4);
  211. }
  212. /**
  213. * Writes an int at the given position in the given buffer, using the
  214. * given ByteOrder
  215. * @param buffer buffer into which to insert the int
  216. * @param val Int to insert
  217. * @param offset offset at which to insert the int
  218. * @param order the order to insert the bytes of the int
  219. */
  220. public static void putInt(ByteBuffer buffer, int val, int offset,
  221. ByteOrder order)
  222. {
  223. ByteOrder origOrder = buffer.order();
  224. try {
  225. buffer.order(order).putInt(offset, val);
  226. } finally {
  227. buffer.order(origOrder);
  228. }
  229. }
  230. /**
  231. * Read an unsigned variable length int from a buffer
  232. * @param buffer Buffer containing the variable length int
  233. * @return The unsigned int
  234. */
  235. public static int getUnsignedVarInt(ByteBuffer buffer, int numBytes) {
  236. int pos = buffer.position();
  237. int rtn = getUnsignedVarInt(buffer, pos, numBytes);
  238. buffer.position(pos + numBytes);
  239. return rtn;
  240. }
  241. /**
  242. * Read an unsigned variable length int from a buffer
  243. * @param buffer Buffer containing the variable length int
  244. * @param offset Offset at which to read the value
  245. * @return The unsigned int
  246. */
  247. public static int getUnsignedVarInt(ByteBuffer buffer, int offset,
  248. int numBytes) {
  249. switch(numBytes) {
  250. case 1:
  251. return getUnsignedByte(buffer, offset);
  252. case 2:
  253. return getUnsignedShort(buffer, offset);
  254. case 3:
  255. return get3ByteInt(buffer, offset);
  256. case 4:
  257. return buffer.getInt(offset);
  258. default:
  259. throw new IllegalArgumentException("Invalid num bytes " + numBytes);
  260. }
  261. }
  262. /**
  263. * Reads an array of bytes from the given buffer
  264. * @param buffer Buffer containing the desired bytes
  265. * @param len length of the desired bytes
  266. * @return a new buffer with the given number of bytes from the current
  267. * position in the given buffer
  268. */
  269. public static byte[] getBytes(ByteBuffer buffer, int len)
  270. {
  271. byte[] bytes = new byte[len];
  272. buffer.get(bytes);
  273. return bytes;
  274. }
  275. /**
  276. * Reads an array of bytes from the given buffer at the given offset
  277. * @param buffer Buffer containing the desired bytes
  278. * @param offset Offset at which to read the bytes
  279. * @param len length of the desired bytes
  280. * @return a new buffer with the given number of bytes from the given
  281. * position in the given buffer
  282. */
  283. public static byte[] getBytes(ByteBuffer buffer, int offset, int len)
  284. {
  285. int origPos = buffer.position();
  286. try {
  287. buffer.position(offset);
  288. return getBytes(buffer, len);
  289. } finally {
  290. buffer.position(origPos);
  291. }
  292. }
  293. /**
  294. * Concatenates and returns the given byte arrays.
  295. */
  296. public static byte[] concat(byte[] b1, byte[] b2) {
  297. byte[] out = new byte[b1.length + b2.length];
  298. System.arraycopy(b1, 0, out, 0, b1.length);
  299. System.arraycopy(b2, 0, out, b1.length, b2.length);
  300. return out;
  301. }
  302. /**
  303. * Sets all bits in the given remaining byte range to 0.
  304. */
  305. public static void clearRemaining(ByteBuffer buffer)
  306. {
  307. if(!buffer.hasRemaining()) {
  308. return;
  309. }
  310. int pos = buffer.position();
  311. clearRange(buffer, pos, pos + buffer.remaining());
  312. }
  313. /**
  314. * Sets all bits in the given byte range to 0.
  315. */
  316. public static void clearRange(ByteBuffer buffer, int start,
  317. int end)
  318. {
  319. putRange(buffer, start, end, (byte)0x00);
  320. }
  321. /**
  322. * Sets all bits in the given byte range to 1.
  323. */
  324. public static void fillRange(ByteBuffer buffer, int start,
  325. int end)
  326. {
  327. putRange(buffer, start, end, (byte)0xff);
  328. }
  329. /**
  330. * Sets all bytes in the given byte range to the given byte value.
  331. */
  332. public static void putRange(ByteBuffer buffer, int start,
  333. int end, byte b)
  334. {
  335. for(int i = start; i < end; ++i) {
  336. buffer.put(i, b);
  337. }
  338. }
  339. /**
  340. * Matches a pattern of bytes against the given buffer starting at the given
  341. * offset.
  342. */
  343. public static boolean matchesRange(ByteBuffer buffer, int start,
  344. byte[] pattern)
  345. {
  346. for(int i = 0; i < pattern.length; ++i) {
  347. if(pattern[i] != buffer.get(start + i)) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. /**
  354. * Searches for a pattern of bytes in the given buffer starting at the
  355. * given offset.
  356. * @return the offset of the pattern if a match is found, -1 otherwise
  357. */
  358. public static int findRange(ByteBuffer buffer, int start, byte[] pattern)
  359. {
  360. byte firstByte = pattern[0];
  361. int limit = buffer.limit() - pattern.length;
  362. for(int i = start; i < limit; ++i) {
  363. if((firstByte == buffer.get(i)) && matchesRange(buffer, i, pattern)) {
  364. return i;
  365. }
  366. }
  367. return -1;
  368. }
  369. /**
  370. * Convert a byte buffer to a hexadecimal string for display
  371. * @param buffer Buffer to display, starting at offset 0
  372. * @param size Number of bytes to read from the buffer
  373. * @return The display String
  374. */
  375. public static String toHexString(ByteBuffer buffer, int size) {
  376. return toHexString(buffer, 0, size);
  377. }
  378. /**
  379. * Convert a byte array to a hexadecimal string for display
  380. * @param array byte array to display, starting at offset 0
  381. * @return The display String
  382. */
  383. public static String toHexString(byte[] array) {
  384. return toHexString(ByteBuffer.wrap(array), 0, array.length);
  385. }
  386. /**
  387. * Convert a byte buffer to a hexadecimal string for display
  388. * @param buffer Buffer to display, starting at offset 0
  389. * @param offset Offset at which to start reading the buffer
  390. * @param size Number of bytes to read from the buffer
  391. * @return The display String
  392. */
  393. public static String toHexString(ByteBuffer buffer, int offset, int size) {
  394. return toHexString(buffer, offset, size, true);
  395. }
  396. /**
  397. * Convert a byte buffer to a hexadecimal string for display
  398. * @param buffer Buffer to display, starting at offset 0
  399. * @param offset Offset at which to start reading the buffer
  400. * @param size Number of bytes to read from the buffer
  401. * @param formatted flag indicating if formatting is required
  402. * @return The display String
  403. */
  404. public static String toHexString(ByteBuffer buffer,
  405. int offset, int size, boolean formatted) {
  406. StringBuilder rtn = new StringBuilder();
  407. int position = buffer.position();
  408. buffer.position(offset);
  409. size = Math.min(size, buffer.remaining());
  410. for (int i = 0; i < size; i++) {
  411. byte b = buffer.get();
  412. byte h = (byte) (b & 0xF0);
  413. h = (byte) (h >>> 4);
  414. h = (byte) (h & 0x0F);
  415. rtn.append(HEX_CHARS[h]);
  416. h = (byte) (b & 0x0F);
  417. rtn.append(HEX_CHARS[h]);
  418. int next = (i + 1);
  419. if(formatted && (next < size))
  420. {
  421. if((next % NUM_BYTES_PER_LINE) == 0) {
  422. rtn.append("\n");
  423. } else {
  424. rtn.append(" ");
  425. if ((next % NUM_BYTES_PER_BLOCK) == 0) {
  426. rtn.append(" ");
  427. }
  428. }
  429. }
  430. }
  431. buffer.position(position);
  432. return rtn.toString();
  433. }
  434. /**
  435. * Convert the given number of bytes from the given database page to a
  436. * hexidecimal string for display.
  437. */
  438. public static String toHexString(DatabaseImpl db, int pageNumber, int size)
  439. throws IOException
  440. {
  441. ByteBuffer buffer = db.getPageChannel().createPageBuffer();
  442. db.getPageChannel().readPage(buffer, pageNumber);
  443. return toHexString(buffer, size);
  444. }
  445. /**
  446. * Writes a sequence of hexidecimal values into the given buffer, where
  447. * every two characters represent one byte value.
  448. */
  449. public static void writeHexString(ByteBuffer buffer,
  450. String hexStr)
  451. throws IOException
  452. {
  453. char[] hexChars = hexStr.toCharArray();
  454. if((hexChars.length % 2) != 0) {
  455. throw new IOException("Hex string length must be even");
  456. }
  457. for(int i = 0; i < hexChars.length; i += 2) {
  458. String tmpStr = new String(hexChars, i, 2);
  459. buffer.put((byte)Long.parseLong(tmpStr, 16));
  460. }
  461. }
  462. /**
  463. * Writes a chunk of data to a file in pretty printed hexidecimal.
  464. */
  465. public static void toHexFile(
  466. String fileName,
  467. ByteBuffer buffer,
  468. int offset, int size)
  469. throws IOException
  470. {
  471. PrintWriter writer = new PrintWriter(
  472. new FileWriter(fileName));
  473. try {
  474. writer.println(toHexString(buffer, offset, size));
  475. } finally {
  476. writer.close();
  477. }
  478. }
  479. /**
  480. * @return the byte value converted to an unsigned int value
  481. */
  482. public static int asUnsignedByte(byte b) {
  483. return b & 0xFF;
  484. }
  485. /**
  486. * @return the short value converted to an unsigned int value
  487. */
  488. public static int asUnsignedShort(short s) {
  489. return s & 0xFFFF;
  490. }
  491. /**
  492. * Swaps the 4 bytes (changes endianness) of the bytes at the given offset.
  493. *
  494. * @param bytes buffer containing bytes to swap
  495. * @param offset offset of the first byte of the bytes to swap
  496. */
  497. public static void swap4Bytes(byte[] bytes, int offset)
  498. {
  499. byte b = bytes[offset + 0];
  500. bytes[offset + 0] = bytes[offset + 3];
  501. bytes[offset + 3] = b;
  502. b = bytes[offset + 1];
  503. bytes[offset + 1] = bytes[offset + 2];
  504. bytes[offset + 2] = b;
  505. }
  506. /**
  507. * Swaps the 2 bytes (changes endianness) of the bytes at the given offset.
  508. *
  509. * @param bytes buffer containing bytes to swap
  510. * @param offset offset of the first byte of the bytes to swap
  511. */
  512. public static void swap2Bytes(byte[] bytes, int offset)
  513. {
  514. byte b = bytes[offset + 0];
  515. bytes[offset + 0] = bytes[offset + 1];
  516. bytes[offset + 1] = b;
  517. }
  518. /**
  519. * Moves the position of the given buffer the given count from the current
  520. * position.
  521. * @return the new buffer position
  522. */
  523. public static int forward(ByteBuffer buffer, int count)
  524. {
  525. int newPos = buffer.position() + count;
  526. buffer.position(newPos);
  527. return newPos;
  528. }
  529. /**
  530. * Returns a copy of the given array of the given length.
  531. */
  532. public static byte[] copyOf(byte[] arr, int newLength)
  533. {
  534. return copyOf(arr, 0, newLength);
  535. }
  536. /**
  537. * Returns a copy of the given array of the given length starting at the
  538. * given position.
  539. */
  540. public static byte[] copyOf(byte[] arr, int offset, int newLength)
  541. {
  542. byte[] newArr = new byte[newLength];
  543. int srcLen = arr.length - offset;
  544. System.arraycopy(arr, offset, newArr, 0, Math.min(srcLen, newLength));
  545. return newArr;
  546. }
  547. /**
  548. * Copies the given InputStream to the given OutputStream.
  549. */
  550. public static void copy(InputStream in, OutputStream out) throws IOException {
  551. byte[] buf = new byte[8 * 1024];
  552. int read = 0;
  553. while((read = in.read(buf)) > -1) {
  554. out.write(buf, 0, read);
  555. }
  556. }
  557. /**
  558. * Closes the given Closeable if non-null, swallows any IOExceptions.
  559. */
  560. public static void closeQuietly(Closeable c) {
  561. if(c != null) {
  562. try {
  563. c.close();
  564. } catch(IOException ignored) {}
  565. }
  566. }
  567. /**
  568. * Utility byte stream similar to ByteArrayOutputStream but with extended
  569. * accessibility to the bytes.
  570. */
  571. public static class ByteStream extends OutputStream
  572. {
  573. private byte[] _bytes;
  574. private int _length;
  575. private int _lastLength;
  576. public ByteStream() {
  577. this(32);
  578. }
  579. public ByteStream(int capacity) {
  580. _bytes = new byte[capacity];
  581. }
  582. public int getLength() {
  583. return _length;
  584. }
  585. public byte[] getBytes() {
  586. return _bytes;
  587. }
  588. protected void ensureNewCapacity(int numBytes) {
  589. int newLength = _length + numBytes;
  590. if(newLength > _bytes.length) {
  591. byte[] temp = new byte[newLength * 2];
  592. System.arraycopy(_bytes, 0, temp, 0, _length);
  593. _bytes = temp;
  594. }
  595. }
  596. @Override
  597. public void write(int b) {
  598. ensureNewCapacity(1);
  599. _bytes[_length++] = (byte)b;
  600. }
  601. @Override
  602. public void write(byte[] b) {
  603. write(b, 0, b.length);
  604. }
  605. @Override
  606. public void write(byte[] b, int offset, int length) {
  607. ensureNewCapacity(length);
  608. System.arraycopy(b, offset, _bytes, _length, length);
  609. _length += length;
  610. }
  611. public byte get(int offset) {
  612. return _bytes[offset];
  613. }
  614. public void set(int offset, byte b) {
  615. _bytes[offset] = b;
  616. }
  617. public void writeFill(int length, byte b) {
  618. ensureNewCapacity(length);
  619. int oldLength = _length;
  620. _length += length;
  621. Arrays.fill(_bytes, oldLength, _length, b);
  622. }
  623. public void skip(int n) {
  624. ensureNewCapacity(n);
  625. _length += n;
  626. }
  627. public void writeTo(ByteStream out) {
  628. out.write(_bytes, 0, _length);
  629. }
  630. public byte[] toByteArray() {
  631. byte[] result = null;
  632. if(_length == _bytes.length) {
  633. result = _bytes;
  634. _bytes = null;
  635. } else {
  636. result = copyOf(_bytes, _length);
  637. if(_lastLength == _length) {
  638. // if we get the same result length bytes twice in a row, clear the
  639. // _bytes so that the next _bytes will be _lastLength
  640. _bytes = null;
  641. }
  642. }
  643. // save result length so we can potentially get the right length of the
  644. // next byte[] in reset()
  645. _lastLength = _length;
  646. return result;
  647. }
  648. public void reset() {
  649. _length = 0;
  650. if(_bytes == null) {
  651. _bytes = new byte[_lastLength];
  652. }
  653. }
  654. public void trimTrailing(byte minTrimCode, byte maxTrimCode)
  655. {
  656. int minTrim = ByteUtil.asUnsignedByte(minTrimCode);
  657. int maxTrim = ByteUtil.asUnsignedByte(maxTrimCode);
  658. int idx = _length - 1;
  659. while(idx >= 0) {
  660. int val = asUnsignedByte(get(idx));
  661. if((val >= minTrim) && (val <= maxTrim)) {
  662. --idx;
  663. } else {
  664. break;
  665. }
  666. }
  667. _length = idx + 1;
  668. }
  669. }
  670. }