1 17 18 19 20 package org.apache.fop.render.afp.tools; 21 22 import java.io.ByteArrayOutputStream ; 23 24 28 public final class BinaryUtils { 29 30 37 public static byte[] convert(int integer, int bufsize) { 38 39 StringBuffer buf = new StringBuffer (Integer.toHexString(integer)); 40 if (buf.length() % 2 == 0) { 41 } else { 43 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 60 public static byte[] convert(int integer) { 61 62 return convert(Integer.toHexString(integer)); 63 64 } 65 66 71 public static byte[] convert(String digits) { 72 73 if (digits.length() % 2 == 0) { 74 } else { 76 digits = "0" + digits; 78 } 79 80 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 ("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 ("Bad hexadecimal digit"); 101 baos.write(b); 102 } 103 return (baos.toByteArray()); 104 105 } 106 107 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 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 |