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.

BinaryUtils.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.afp.tools;
  18. import java.io.ByteArrayOutputStream;
  19. /**
  20. * Library of utility useful conversion methods.
  21. *
  22. */
  23. public final class BinaryUtils {
  24. /**
  25. * Convert an int into the corresponding byte array by encoding each
  26. * two hexadecimal digits as a char. This will return a byte array
  27. * to the length specified by bufsize.
  28. * @param integer The int representation.
  29. * @param bufsize The required byte array size.
  30. */
  31. public static byte[] convert(int integer, int bufsize) {
  32. StringBuffer buf = new StringBuffer(Integer.toHexString(integer));
  33. if (buf.length() % 2 == 0) {
  34. // Ignore even number of digits
  35. } else {
  36. // Convert to an even number of digits
  37. buf.insert(0, "0");
  38. }
  39. int size = buf.length() / 2;
  40. while (size < bufsize) {
  41. buf.insert(0, "00");
  42. size++;
  43. };
  44. return convert(buf.toString());
  45. }
  46. /**
  47. * Convert an int into the corresponding byte array by encoding each
  48. * two hexadecimal digits as a char.
  49. * @param integer The int representation
  50. */
  51. public static byte[] convert(int integer) {
  52. return convert(Integer.toHexString(integer));
  53. }
  54. /**
  55. * Convert a String of hexadecimal digits into the corresponding
  56. * byte array by encoding each two hexadecimal digits as a byte.
  57. * @param digits The hexadecimal digits representation.
  58. */
  59. public static byte[] convert(String digits) {
  60. if (digits.length() % 2 == 0) {
  61. // Even number of digits, so ignore
  62. } else {
  63. // Convert to an even number of digits
  64. digits = "0" + digits;
  65. }
  66. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  67. for (int i = 0; i < digits.length(); i += 2) {
  68. char c1 = digits.charAt(i);
  69. char c2 = digits.charAt(i + 1);
  70. byte b = 0;
  71. if ((c1 >= '0') && (c1 <= '9'))
  72. b += ((c1 - '0') * 16);
  73. else if ((c1 >= 'a') && (c1 <= 'f'))
  74. b += ((c1 - 'a' + 10) * 16);
  75. else if ((c1 >= 'A') && (c1 <= 'F'))
  76. b += ((c1 - 'A' + 10) * 16);
  77. else
  78. throw new IllegalArgumentException("Bad hexadecimal digit");
  79. if ((c2 >= '0') && (c2 <= '9'))
  80. b += (c2 - '0');
  81. else if ((c2 >= 'a') && (c2 <= 'f'))
  82. b += (c2 - 'a' + 10);
  83. else if ((c2 >= 'A') && (c2 <= 'F'))
  84. b += (c2 - 'A' + 10);
  85. else
  86. throw new IllegalArgumentException("Bad hexadecimal digit");
  87. baos.write(b);
  88. }
  89. return (baos.toByteArray());
  90. }
  91. /**
  92. * Convert the specified short into a byte array.
  93. * @param value The value to be converted.
  94. * @param array The array to receive the data.
  95. * @param offset The offset into the byte array for the start of the value.
  96. */
  97. public static void shortToByteArray(
  98. short value,
  99. byte[] array,
  100. int offset) {
  101. array[offset] = (byte) (value >>> 8);
  102. array[offset + 1] = (byte) value;
  103. }
  104. /**
  105. * Convert the specified short into a byte array.
  106. * @param value The value to be converted.
  107. * @return The byte array
  108. */
  109. public static byte[] shortToByteArray(short value) {
  110. byte[] serverValue = new byte[2];
  111. shortToByteArray(value, serverValue, 0);
  112. return serverValue;
  113. }
  114. }