| 
 1 | 
  
 |  | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 |  | 
| 
 4 | 
  
 |  | 
| 
 5 | 
  
 |  | 
| 
 6 | 
  
 |  | 
| 
 7 | 
  
 |  | 
| 
 8 | 
  
 |  | 
| 
 9 | 
  
 |  | 
| 
 10 | 
  
 |  | 
| 
 11 | 
  
 |  | 
| 
 12 | 
  
 |  | 
| 
 13 | 
  
 |  | 
| 
 14 | 
  
 |  | 
| 
 15 | 
  
 |  | 
| 
 16 | 
  
 |  | 
| 
 17 | 
  
 |  | 
| 
 18 | 
  
 | package org.apache.geronimo.mail.util; | 
| 
 19 | 
  
 |  | 
| 
 20 | 
  
 | import java.io.ByteArrayOutputStream; | 
| 
 21 | 
  
 | import java.io.IOException; | 
| 
 22 | 
  
 | import java.io.OutputStream; | 
| 
 23 | 
  
 |  | 
| 
 24 | 
  
 | public class Hex | 
| 
 25 | 
  
 | { | 
| 
 26 | 
  
 |     private static final Encoder encoder = new HexEncoder(); | 
| 
 27 | 
  
 |  | 
| 
 28 | 
  
 |      | 
| 
 29 | 
  
 |  | 
| 
 30 | 
  
 |  | 
| 
 31 | 
  
 |  | 
| 
 32 | 
  
 |  | 
| 
 33 | 
 0
 |     public static byte[] encode(
 | 
| 
 34 | 
  
 |         byte[]    data) | 
| 
 35 | 
  
 |     { | 
| 
 36 | 
 0
 |         return encode(data, 0, data.length);
 | 
| 
 37 | 
  
 |     } | 
| 
 38 | 
  
 |  | 
| 
 39 | 
  
 |      | 
| 
 40 | 
  
 |  | 
| 
 41 | 
  
 |  | 
| 
 42 | 
  
 |  | 
| 
 43 | 
  
 |  | 
| 
 44 | 
 0
 |     public static byte[] encode(
 | 
| 
 45 | 
  
 |         byte[]    data, | 
| 
 46 | 
  
 |         int       off, | 
| 
 47 | 
  
 |         int       length) | 
| 
 48 | 
  
 |     { | 
| 
 49 | 
 0
 |         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
 | 
| 
 50 | 
  
 |  | 
| 
 51 | 
 0
 |         try
 | 
| 
 52 | 
  
 |         { | 
| 
 53 | 
 0
 |             encoder.encode(data, off, length, bOut);
 | 
| 
 54 | 
  
 |         } | 
| 
 55 | 
  
 |         catch (IOException e) | 
| 
 56 | 
  
 |         { | 
| 
 57 | 
 0
 |             throw new RuntimeException("exception encoding Hex string: " + e);
 | 
| 
 58 | 
  
 |         } | 
| 
 59 | 
  
 |  | 
| 
 60 | 
 0
 |         return bOut.toByteArray();
 | 
| 
 61 | 
  
 |     } | 
| 
 62 | 
  
 |  | 
| 
 63 | 
  
 |      | 
| 
 64 | 
  
 |  | 
| 
 65 | 
  
 |  | 
| 
 66 | 
  
 |  | 
| 
 67 | 
  
 |  | 
| 
 68 | 
 0
 |     public static int encode(
 | 
| 
 69 | 
  
 |         byte[]         data, | 
| 
 70 | 
  
 |         OutputStream   out) | 
| 
 71 | 
  
 |         throws IOException | 
| 
 72 | 
  
 |     { | 
| 
 73 | 
 0
 |         return encoder.encode(data, 0, data.length, out);
 | 
| 
 74 | 
  
 |     } | 
| 
 75 | 
  
 |  | 
| 
 76 | 
  
 |      | 
| 
 77 | 
  
 |  | 
| 
 78 | 
  
 |  | 
| 
 79 | 
  
 |  | 
| 
 80 | 
  
 |  | 
| 
 81 | 
 0
 |     public static int encode(
 | 
| 
 82 | 
  
 |         byte[]         data, | 
| 
 83 | 
  
 |         int            off, | 
| 
 84 | 
  
 |         int            length, | 
| 
 85 | 
  
 |         OutputStream   out) | 
| 
 86 | 
  
 |         throws IOException | 
| 
 87 | 
  
 |     { | 
| 
 88 | 
 0
 |         return encoder.encode(data, 0, data.length, out);
 | 
| 
 89 | 
  
 |     } | 
| 
 90 | 
  
 |  | 
| 
 91 | 
  
 |      | 
| 
 92 | 
  
 |  | 
| 
 93 | 
  
 |  | 
| 
 94 | 
  
 |  | 
| 
 95 | 
  
 |  | 
| 
 96 | 
 0
 |     public static byte[] decode(
 | 
| 
 97 | 
  
 |         byte[]    data) | 
| 
 98 | 
  
 |     { | 
| 
 99 | 
 0
 |         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
 | 
| 
 100 | 
  
 |  | 
| 
 101 | 
 0
 |         try
 | 
| 
 102 | 
  
 |         { | 
| 
 103 | 
 0
 |             encoder.decode(data, 0, data.length, bOut);
 | 
| 
 104 | 
  
 |         } | 
| 
 105 | 
  
 |         catch (IOException e) | 
| 
 106 | 
  
 |         { | 
| 
 107 | 
 0
 |             throw new RuntimeException("exception decoding Hex string: " + e);
 | 
| 
 108 | 
  
 |         } | 
| 
 109 | 
  
 |  | 
| 
 110 | 
 0
 |         return bOut.toByteArray();
 | 
| 
 111 | 
  
 |     } | 
| 
 112 | 
  
 |  | 
| 
 113 | 
  
 |      | 
| 
 114 | 
  
 |  | 
| 
 115 | 
  
 |  | 
| 
 116 | 
  
 |  | 
| 
 117 | 
  
 |  | 
| 
 118 | 
 0
 |     public static byte[] decode(
 | 
| 
 119 | 
  
 |         String    data) | 
| 
 120 | 
  
 |     { | 
| 
 121 | 
 0
 |         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
 | 
| 
 122 | 
  
 |  | 
| 
 123 | 
 0
 |         try
 | 
| 
 124 | 
  
 |         { | 
| 
 125 | 
 0
 |             encoder.decode(data, bOut);
 | 
| 
 126 | 
  
 |         } | 
| 
 127 | 
  
 |         catch (IOException e) | 
| 
 128 | 
  
 |         { | 
| 
 129 | 
 0
 |             throw new RuntimeException("exception decoding Hex string: " + e);
 | 
| 
 130 | 
  
 |         } | 
| 
 131 | 
  
 |  | 
| 
 132 | 
 0
 |         return bOut.toByteArray();
 | 
| 
 133 | 
  
 |     } | 
| 
 134 | 
  
 |  | 
| 
 135 | 
  
 |      | 
| 
 136 | 
  
 |  | 
| 
 137 | 
  
 |  | 
| 
 138 | 
  
 |  | 
| 
 139 | 
  
 |  | 
| 
 140 | 
  
 |  | 
| 
 141 | 
 0
 |     public static int decode(
 | 
| 
 142 | 
  
 |         String          data, | 
| 
 143 | 
  
 |         OutputStream    out) | 
| 
 144 | 
  
 |         throws IOException | 
| 
 145 | 
  
 |     { | 
| 
 146 | 
 0
 |         return encoder.decode(data, out);
 | 
| 
 147 | 
  
 |     } | 
| 
 148 | 
  
 | } |