001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: BinaryUtils.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.afp.tools;
021:
022: import java.io.ByteArrayOutputStream;
023:
024: /**
025: * Library of utility useful conversion methods.
026: *
027: */
028: public final class BinaryUtils {
029:
030: /**
031: * Convert an int into the corresponding byte array by encoding each
032: * two hexadecimal digits as a char. This will return a byte array
033: * to the length specified by bufsize.
034: * @param integer The int representation.
035: * @param bufsize The required byte array size.
036: */
037: public static byte[] convert(int integer, int bufsize) {
038:
039: StringBuffer buf = new StringBuffer(Integer
040: .toHexString(integer));
041: if (buf.length() % 2 == 0) {
042: // Ignore even number of digits
043: } else {
044: // Convert to an even number of digits
045: buf.insert(0, "0");
046: }
047: int size = buf.length() / 2;
048: while (size < bufsize) {
049: buf.insert(0, "00");
050: size++;
051: }
052: ;
053: return convert(buf.toString());
054:
055: }
056:
057: /**
058: * Convert an int into the corresponding byte array by encoding each
059: * two hexadecimal digits as a char.
060: * @param integer The int representation
061: */
062: public static byte[] convert(int integer) {
063:
064: return convert(Integer.toHexString(integer));
065:
066: }
067:
068: /**
069: * Convert a String of hexadecimal digits into the corresponding
070: * byte array by encoding each two hexadecimal digits as a byte.
071: * @param digits The hexadecimal digits representation.
072: */
073: public static byte[] convert(String digits) {
074:
075: if (digits.length() % 2 == 0) {
076: // Even number of digits, so ignore
077: } else {
078: // Convert to an even number of digits
079: digits = "0" + digits;
080: }
081:
082: ByteArrayOutputStream baos = new ByteArrayOutputStream();
083: for (int i = 0; i < digits.length(); i += 2) {
084: char c1 = digits.charAt(i);
085: char c2 = digits.charAt(i + 1);
086: byte b = 0;
087: if ((c1 >= '0') && (c1 <= '9'))
088: b += ((c1 - '0') * 16);
089: else if ((c1 >= 'a') && (c1 <= 'f'))
090: b += ((c1 - 'a' + 10) * 16);
091: else if ((c1 >= 'A') && (c1 <= 'F'))
092: b += ((c1 - 'A' + 10) * 16);
093: else
094: throw new IllegalArgumentException(
095: "Bad hexadecimal digit");
096: if ((c2 >= '0') && (c2 <= '9'))
097: b += (c2 - '0');
098: else if ((c2 >= 'a') && (c2 <= 'f'))
099: b += (c2 - 'a' + 10);
100: else if ((c2 >= 'A') && (c2 <= 'F'))
101: b += (c2 - 'A' + 10);
102: else
103: throw new IllegalArgumentException(
104: "Bad hexadecimal digit");
105: baos.write(b);
106: }
107: return (baos.toByteArray());
108:
109: }
110:
111: /**
112: * Convert the specified short into a byte array.
113: * @param value The value to be converted.
114: * @param array The array to receive the data.
115: * @param offset The offset into the byte array for the start of the value.
116: */
117: public static void shortToByteArray(short value, byte[] array,
118: int offset) {
119: array[offset] = (byte) (value >>> 8);
120: array[offset + 1] = (byte) value;
121: }
122:
123: /**
124: * Convert the specified short into a byte array.
125: * @param value The value to be converted.
126: * @return The byte array
127: */
128: public static byte[] shortToByteArray(short value) {
129: byte[] serverValue = new byte[2];
130: shortToByteArray(value, serverValue, 0);
131: return serverValue;
132: }
133:
134: }
|