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.6KB

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