KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > tools > BinaryUtils


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
18 /* $Id: BinaryUtils.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.tools;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23
24 /**
25  * Library of utility useful conversion methods.
26  *
27  */

28 public final class BinaryUtils {
29
30     /**
31      * Convert an int into the corresponding byte array by encoding each
32      * two hexadecimal digits as a char. This will return a byte array
33      * to the length specified by bufsize.
34      * @param integer The int representation.
35      * @param bufsize The required byte array size.
36      */

37     public static byte[] convert(int integer, int bufsize) {
38
39         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(Integer.toHexString(integer));
40         if (buf.length() % 2 == 0) {
41             // Ignore even number of digits
42
} else {
43             // Convert to an even number of digits
44
buf.insert(0, "0");
45         }
46         int size = buf.length() / 2;
47         while (size < bufsize) {
48             buf.insert(0, "00");
49             size++;
50         };
51         return convert(buf.toString());
52
53     }
54
55     /**
56      * Convert an int into the corresponding byte array by encoding each
57      * two hexadecimal digits as a char.
58      * @param integer The int representation
59      */

60     public static byte[] convert(int integer) {
61
62         return convert(Integer.toHexString(integer));
63
64     }
65
66     /**
67      * Convert a String of hexadecimal digits into the corresponding
68      * byte array by encoding each two hexadecimal digits as a byte.
69      * @param digits The hexadecimal digits representation.
70      */

71     public static byte[] convert(String JavaDoc digits) {
72
73         if (digits.length() % 2 == 0) {
74             // Even number of digits, so ignore
75
} else {
76             // Convert to an even number of digits
77
digits = "0" + digits;
78         }
79
80         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
81         for (int i = 0; i < digits.length(); i += 2) {
82             char c1 = digits.charAt(i);
83             char c2 = digits.charAt(i + 1);
84             byte b = 0;
85             if ((c1 >= '0') && (c1 <= '9'))
86                 b += ((c1 - '0') * 16);
87             else if ((c1 >= 'a') && (c1 <= 'f'))
88                 b += ((c1 - 'a' + 10) * 16);
89             else if ((c1 >= 'A') && (c1 <= 'F'))
90                 b += ((c1 - 'A' + 10) * 16);
91             else
92                 throw new IllegalArgumentException JavaDoc("Bad hexadecimal digit");
93             if ((c2 >= '0') && (c2 <= '9'))
94                 b += (c2 - '0');
95             else if ((c2 >= 'a') && (c2 <= 'f'))
96                 b += (c2 - 'a' + 10);
97             else if ((c2 >= 'A') && (c2 <= 'F'))
98                 b += (c2 - 'A' + 10);
99             else
100                 throw new IllegalArgumentException JavaDoc("Bad hexadecimal digit");
101             baos.write(b);
102         }
103         return (baos.toByteArray());
104
105     }
106
107     /**
108      * Convert the specified short into a byte array.
109      * @param value The value to be converted.
110      * @param array The array to receive the data.
111      * @param offset The offset into the byte array for the start of the value.
112      */

113     public static void shortToByteArray(
114         short value,
115         byte[] array,
116         int offset) {
117         array[offset] = (byte) (value >>> 8);
118         array[offset + 1] = (byte) value;
119     }
120
121     /**
122      * Convert the specified short into a byte array.
123      * @param value The value to be converted.
124      * @return The byte array
125      */

126     public static byte[] shortToByteArray(short value) {
127         byte[] serverValue = new byte[2];
128         shortToByteArray(value, serverValue, 0);
129         return serverValue;
130     }
131
132 }
133
Popular Tags