You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteUtil.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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;
  23. import java.nio.ByteBuffer;
  24. /**
  25. * Byte manipulation and display utilities
  26. * @author Tim McCune
  27. */
  28. public final class ByteUtil {
  29. private static final String[] HEX_CHARS = new String[] {
  30. "0", "1", "2", "3", "4", "5", "6", "7",
  31. "8", "9", "A", "B", "C", "D", "E", "F"};
  32. private ByteUtil() {}
  33. /**
  34. * Convert an int from 4 bytes to 3
  35. * @param i Int to convert
  36. * @return Array of 3 bytes in little-endian order
  37. */
  38. public static byte[] to3ByteInt(int i) {
  39. byte[] rtn = new byte[3];
  40. rtn[0] = (byte) (i & 0xFF);
  41. rtn[1] = (byte) ((i >>> 8) & 0xFF);
  42. rtn[2] = (byte) ((i >>> 16) & 0xFF);
  43. return rtn;
  44. }
  45. /**
  46. * Read a 3 byte int from a buffer in little-endian order
  47. * @param buffer Buffer containing the bytes
  48. * @param offset Offset at which to start reading the int
  49. * @return The int
  50. */
  51. public static int get3ByteInt(ByteBuffer buffer, int offset) {
  52. int rtn = buffer.get(offset) & 0xff;
  53. rtn += ((((int) buffer.get(offset + 1)) & 0xFF) << 8);
  54. rtn += ((((int) buffer.get(offset + 2)) & 0xFF) << 16);
  55. rtn &= 16777215; //2 ^ (8 * 3) - 1
  56. return rtn;
  57. }
  58. /**
  59. * Convert a byte buffer to a hexadecimal string for display
  60. * @param buffer Buffer to display, starting at offset 0
  61. * @param size Number of bytes to read from the buffer
  62. * @return The display String
  63. */
  64. public static String toHexString(ByteBuffer buffer, int size) {
  65. return toHexString(buffer, 0, size);
  66. }
  67. /**
  68. * Convert a byte buffer to a hexadecimal string for display
  69. * @param buffer Buffer to display, starting at offset 0
  70. * @param offset Offset at which to start reading the buffer
  71. * @param size Number of bytes to read from the buffer
  72. * @return The display String
  73. */
  74. public static String toHexString(ByteBuffer buffer, int offset, int size) {
  75. return toHexString(buffer, offset, size, true);
  76. }
  77. /**
  78. * Convert a byte buffer to a hexadecimal string for display
  79. * @param buffer Buffer to display, starting at offset 0
  80. * @param offset Offset at which to start reading the buffer
  81. * @param size Number of bytes to read from the buffer
  82. * @param formatted flag indicating if formatting is required
  83. * @return The display String
  84. */
  85. public static String toHexString(ByteBuffer buffer,
  86. int offset, int size, boolean formatted) {
  87. StringBuilder rtn = new StringBuilder();
  88. int position = buffer.position();
  89. buffer.position(offset);
  90. for (int i = 0; i < size; i++) {
  91. byte b = buffer.get();
  92. byte h = (byte) (b & 0xF0);
  93. h = (byte) (h >>> 4);
  94. h = (byte) (h & 0x0F);
  95. rtn.append(HEX_CHARS[(int) h]);
  96. h = (byte) (b & 0x0F);
  97. rtn.append(HEX_CHARS[(int) h]);
  98. if (formatted == true)
  99. {
  100. rtn.append(" ");
  101. if ((i + 1) % 4 == 0) {
  102. rtn.append(" ");
  103. }
  104. if ((i + 1) % 24 == 0) {
  105. rtn.append("\n");
  106. }
  107. }
  108. }
  109. buffer.position(position);
  110. return rtn.toString();
  111. }
  112. }