1. /*
  2. * @(#)Bits.java 1.3 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * Utility methods for packing/unpacking primitive values in/out of byte arrays
  10. * using big-endian byte ordering.
  11. */
  12. class Bits {
  13. /*
  14. * Methods for unpacking primitive values from byte arrays starting at
  15. * given offsets.
  16. */
  17. static boolean getBoolean(byte[] b, int off) {
  18. return b[off] != 0;
  19. }
  20. static char getChar(byte[] b, int off) {
  21. return (char) (((b[off + 1] & 0xFF) << 0) +
  22. ((b[off + 0] & 0xFF) << 8));
  23. }
  24. static short getShort(byte[] b, int off) {
  25. return (short) (((b[off + 1] & 0xFF) << 0) +
  26. ((b[off + 0] & 0xFF) << 8));
  27. }
  28. static int getInt(byte[] b, int off) {
  29. return ((b[off + 3] & 0xFF) << 0) +
  30. ((b[off + 2] & 0xFF) << 8) +
  31. ((b[off + 1] & 0xFF) << 16) +
  32. ((b[off + 0] & 0xFF) << 24);
  33. }
  34. static float getFloat(byte[] b, int off) {
  35. int i = ((b[off + 3] & 0xFF) << 0) +
  36. ((b[off + 2] & 0xFF) << 8) +
  37. ((b[off + 1] & 0xFF) << 16) +
  38. ((b[off + 0] & 0xFF) << 24);
  39. return Float.intBitsToFloat(i);
  40. }
  41. static long getLong(byte[] b, int off) {
  42. return ((b[off + 7] & 0xFFL) << 0) +
  43. ((b[off + 6] & 0xFFL) << 8) +
  44. ((b[off + 5] & 0xFFL) << 16) +
  45. ((b[off + 4] & 0xFFL) << 24) +
  46. ((b[off + 3] & 0xFFL) << 32) +
  47. ((b[off + 2] & 0xFFL) << 40) +
  48. ((b[off + 1] & 0xFFL) << 48) +
  49. ((b[off + 0] & 0xFFL) << 56);
  50. }
  51. static double getDouble(byte[] b, int off) {
  52. long j = ((b[off + 7] & 0xFFL) << 0) +
  53. ((b[off + 6] & 0xFFL) << 8) +
  54. ((b[off + 5] & 0xFFL) << 16) +
  55. ((b[off + 4] & 0xFFL) << 24) +
  56. ((b[off + 3] & 0xFFL) << 32) +
  57. ((b[off + 2] & 0xFFL) << 40) +
  58. ((b[off + 1] & 0xFFL) << 48) +
  59. ((b[off + 0] & 0xFFL) << 56);
  60. return Double.longBitsToDouble(j);
  61. }
  62. /*
  63. * Methods for packing primitive values into byte arrays starting at given
  64. * offsets.
  65. */
  66. static void putBoolean(byte[] b, int off, boolean val) {
  67. b[off] = (byte) (val ? 1 : 0);
  68. }
  69. static void putChar(byte[] b, int off, char val) {
  70. b[off + 1] = (byte) (val >>> 0);
  71. b[off + 0] = (byte) (val >>> 8);
  72. }
  73. static void putShort(byte[] b, int off, short val) {
  74. b[off + 1] = (byte) (val >>> 0);
  75. b[off + 0] = (byte) (val >>> 8);
  76. }
  77. static void putInt(byte[] b, int off, int val) {
  78. b[off + 3] = (byte) (val >>> 0);
  79. b[off + 2] = (byte) (val >>> 8);
  80. b[off + 1] = (byte) (val >>> 16);
  81. b[off + 0] = (byte) (val >>> 24);
  82. }
  83. static void putFloat(byte[] b, int off, float val) {
  84. int i = Float.floatToIntBits(val);
  85. b[off + 3] = (byte) (i >>> 0);
  86. b[off + 2] = (byte) (i >>> 8);
  87. b[off + 1] = (byte) (i >>> 16);
  88. b[off + 0] = (byte) (i >>> 24);
  89. }
  90. static void putLong(byte[] b, int off, long val) {
  91. b[off + 7] = (byte) (val >>> 0);
  92. b[off + 6] = (byte) (val >>> 8);
  93. b[off + 5] = (byte) (val >>> 16);
  94. b[off + 4] = (byte) (val >>> 24);
  95. b[off + 3] = (byte) (val >>> 32);
  96. b[off + 2] = (byte) (val >>> 40);
  97. b[off + 1] = (byte) (val >>> 48);
  98. b[off + 0] = (byte) (val >>> 56);
  99. }
  100. static void putDouble(byte[] b, int off, double val) {
  101. long j = Double.doubleToLongBits(val);
  102. b[off + 7] = (byte) (j >>> 0);
  103. b[off + 6] = (byte) (j >>> 8);
  104. b[off + 5] = (byte) (j >>> 16);
  105. b[off + 4] = (byte) (j >>> 24);
  106. b[off + 3] = (byte) (j >>> 32);
  107. b[off + 2] = (byte) (j >>> 40);
  108. b[off + 1] = (byte) (j >>> 48);
  109. b[off + 0] = (byte) (j >>> 56);
  110. }
  111. }