KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > util > Util


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.util;
37
38 import java.io.*;
39
40 public class Util
41 {
42     private static final byte encTab[] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
43         0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
44         0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
45         0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
46         0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
47         0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f };
48
49     private static final byte decTab[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
50         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1,
52         -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1,
53         -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
54         18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
55         30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
56         48, 49, 50, 51, -1, -1, -1, -1, -1 };
57
58     public static String JavaDoc base64Encode(String JavaDoc s) throws IOException
59     {
60         return encode(s.getBytes(), 0);
61     }
62
63     /**
64      * Encode a raw byte array to a Base64 String.
65      *
66      * @param in Byte array to encode.
67      * @param len Length of Base64 lines. 0 means no line breaks.
68     **/

69     public static String JavaDoc encode(byte[] in, int len) throws IOException
70     {
71         ByteArrayOutputStream baos = null;
72         ByteArrayInputStream bais = null;
73         try
74         {
75             baos = new ByteArrayOutputStream();
76             bais = new ByteArrayInputStream(in);
77             encode(bais, baos, len);
78             // ASCII byte array to String
79
return (new String JavaDoc(baos.toByteArray()));
80         }
81         finally
82         {
83             if (baos != null)
84             {
85                 baos.close();
86             }
87             if (bais != null)
88             {
89                 bais.close();
90             }
91         }
92     }
93
94     public static void encode(InputStream in, OutputStream out, int len)
95         throws IOException
96     {
97
98         // Check that length is a multiple of 4 bytes
99
if (len % 4 != 0)
100         {
101             throw new IllegalArgumentException JavaDoc("Length must be a multiple of 4");
102         }
103
104         // Read input stream until end of file
105
int bits = 0;
106         int nbits = 0;
107         int nbytes = 0;
108         int b;
109
110         while ((b = in.read()) != -1)
111         {
112             bits = (bits << 8) | b;
113             nbits += 8;
114             while (nbits >= 6)
115             {
116                 nbits -= 6;
117                 out.write(encTab[0x3f & (bits >> nbits)]);
118                 nbytes++;
119                 // New line
120
if (len != 0 && nbytes >= len)
121                 {
122                     out.write(0x0d);
123                     out.write(0x0a);
124                     nbytes -= len;
125                 }
126             }
127         }
128
129         switch (nbits)
130         {
131             case 2:
132                 out.write(encTab[0x3f & (bits << 4)]);
133                 out.write(0x3d); // 0x3d = '='
134
out.write(0x3d);
135                 break;
136             case 4:
137                 out.write(encTab[0x3f & (bits << 2)]);
138                 out.write(0x3d);
139                 break;
140         }
141
142         if (len != 0)
143         {
144             if (nbytes != 0)
145             {
146                 out.write(0x0d);
147                 out.write(0x0a);
148             }
149             out.write(0x0d);
150             out.write(0x0a);
151         }
152     }
153 }
Popular Tags