View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.mail.util;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  
26  public class UUEncode {
27      private static final Encoder encoder = new UUEncoder();
28  
29      /**
30       * encode the input data producing a UUEncoded byte array.
31       *
32       * @return a byte array containing the UUEncoded data.
33       */
34      public static byte[] encode(
35          byte[]    data)
36      {
37          return encode(data, 0, data.length);
38      }
39  
40      /**
41       * encode the input data producing a UUEncoded byte array.
42       *
43       * @return a byte array containing the UUEncoded data.
44       */
45      public static byte[] encode(
46          byte[]    data,
47          int       off,
48          int       length)
49      {
50          ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
51  
52          try
53          {
54              encoder.encode(data, off, length, bOut);
55          }
56          catch (IOException e)
57          {
58              throw new RuntimeException("exception encoding UUEncoded string: " + e);
59          }
60  
61          return bOut.toByteArray();
62      }
63  
64      /**
65       * UUEncode the byte data writing it to the given output stream.
66       *
67       * @return the number of bytes produced.
68       */
69      public static int encode(
70          byte[]         data,
71          OutputStream   out)
72          throws IOException
73      {
74          return encoder.encode(data, 0, data.length, out);
75      }
76  
77      /**
78       * UUEncode the byte data writing it to the given output stream.
79       *
80       * @return the number of bytes produced.
81       */
82      public static int encode(
83          byte[]         data,
84          int            off,
85          int            length,
86          OutputStream   out)
87          throws IOException
88      {
89          return encoder.encode(data, 0, data.length, out);
90      }
91  
92      /**
93       * decode the UUEncoded input data. It is assumed the input data is valid.
94       *
95       * @return a byte array representing the decoded data.
96       */
97      public static byte[] decode(
98          byte[]    data)
99      {
100         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
101 
102         try
103         {
104             encoder.decode(data, 0, data.length, bOut);
105         }
106         catch (IOException e)
107         {
108             throw new RuntimeException("exception decoding UUEncoded string: " + e);
109         }
110 
111         return bOut.toByteArray();
112     }
113 
114     /**
115      * decode the UUEncided String data.
116      *
117      * @return a byte array representing the decoded data.
118      */
119     public static byte[] decode(
120         String    data)
121     {
122         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
123 
124         try
125         {
126             encoder.decode(data, bOut);
127         }
128         catch (IOException e)
129         {
130             throw new RuntimeException("exception decoding UUEncoded string: " + e);
131         }
132 
133         return bOut.toByteArray();
134     }
135 
136     /**
137      * decode the UUEncoded encoded String data writing it to the given output stream.
138      *
139      * @return the number of bytes produced.
140      */
141     public static int decode(
142         String          data,
143         OutputStream    out)
144         throws IOException
145     {
146         return encoder.decode(data, out);
147     }
148 }
149