001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018:
019: */
020:
021: package org.apache.wsrp4j.producer.util;
022:
023: /*
024: * The Apache Software License, Version 1.1
025: *
026: *
027: * Copyright (c) 2001 The Apache Software Foundation. All rights
028: * reserved.
029: *
030: * Redistribution and use in source and binary forms, with or without
031: * modification, are permitted provided that the following conditions
032: * are met:
033: *
034: * 1. Redistributions of source code must retain the above copyright
035: * notice, this list of conditions and the following disclaimer.
036: *
037: * 2. Redistributions in binary form must reproduce the above copyright
038: * notice, this list of conditions and the following disclaimer in
039: * the documentation and/or other materials provided with the
040: * distribution.
041: *
042: * 3. The end-user documentation included with the redistribution,
043: * if any, must include the following acknowledgment:
044: * "This product includes software developed by the
045: * Apache Software Foundation (http://www.apache.org/)."
046: * Alternately, this acknowledgment may appear in the software itself,
047: * if and wherever such third-party acknowledgments normally appear.
048: *
049: * 4. The names "Axis" and "Apache Software Foundation" must
050: * not be used to endorse or promote products derived from this
051: * software without prior written permission. For written
052: * permission, please contact apache@apache.org.
053: *
054: * 5. Products derived from this software may not be called "Apache",
055: * nor may "Apache" appear in their name, without prior written
056: * permission of the Apache Software Foundation.
057: *
058: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
059: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
060: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
061: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
062: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
063: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
064: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
065: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
066: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
067: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
068: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
069: * SUCH DAMAGE.
070: * ====================================================================
071: *
072: * This software consists of voluntary contributions made by many
073: * individuals on behalf of the Apache Software Foundation. For more
074: * information on the Apache Software Foundation, please see
075: * <http://www.apache.org/>.
076: */
077:
078: import java.io.IOException;
079: import java.io.OutputStream;
080: import java.io.Writer;
081:
082: import org.apache.axis.utils.Messages;
083:
084: /**
085: * This is taken from the axis source tree.
086: * Just adapted the base64 alphaphet for use in url's.
087: *
088: *
089: * @author TAMURA Kent <kent@trl.ibm.co.jp>
090: */
091: public class Base64 {
092: private static final char[] S_BASE64CHAR = { 'A', 'B', 'C', 'D',
093: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
094: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
095: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
096: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
097: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' };
098:
099: private static final char S_BASE64PAD = '*';
100:
101: private static final byte[] S_DECODETABLE = new byte[128];
102: static {
103: for (int i = 0; i < S_DECODETABLE.length; i++)
104: S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
105: for (int i = 0; i < S_BASE64CHAR.length; i++)
106: // 0 to 63
107: S_DECODETABLE[S_BASE64CHAR[i]] = (byte) i;
108: }
109:
110: private static int decode0(char[] ibuf, byte[] obuf, int wp) {
111: int outlen = 3;
112: if (ibuf[3] == S_BASE64PAD)
113: outlen = 2;
114: if (ibuf[2] == S_BASE64PAD)
115: outlen = 1;
116: int b0 = S_DECODETABLE[ibuf[0]];
117: int b1 = S_DECODETABLE[ibuf[1]];
118: int b2 = S_DECODETABLE[ibuf[2]];
119: int b3 = S_DECODETABLE[ibuf[3]];
120: switch (outlen) {
121: case 1:
122: obuf[wp] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
123: return 1;
124: case 2:
125: obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
126: obuf[wp] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
127: return 2;
128: case 3:
129: obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
130: obuf[wp++] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
131: obuf[wp] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
132: return 3;
133: default:
134: throw new RuntimeException(Messages
135: .getMessage("internalError00"));
136: }
137: }
138:
139: /**
140: *
141: */
142: public static byte[] decode(char[] data, int off, int len) {
143: char[] ibuf = new char[4];
144: int ibufcount = 0;
145: byte[] obuf = new byte[len / 4 * 3 + 3];
146: int obufcount = 0;
147: for (int i = off; i < off + len; i++) {
148: char ch = data[i];
149: if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
150: && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
151: ibuf[ibufcount++] = ch;
152: if (ibufcount == ibuf.length) {
153: ibufcount = 0;
154: obufcount += decode0(ibuf, obuf, obufcount);
155: }
156: }
157: }
158: if (obufcount == obuf.length)
159: return obuf;
160: byte[] ret = new byte[obufcount];
161: System.arraycopy(obuf, 0, ret, 0, obufcount);
162: return ret;
163: }
164:
165: /**
166: *
167: */
168: public static byte[] decode(String data) {
169: char[] ibuf = new char[4];
170: int ibufcount = 0;
171: byte[] obuf = new byte[data.length() / 4 * 3 + 3];
172: int obufcount = 0;
173: for (int i = 0; i < data.length(); i++) {
174: char ch = data.charAt(i);
175: if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
176: && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
177: ibuf[ibufcount++] = ch;
178: if (ibufcount == ibuf.length) {
179: ibufcount = 0;
180: obufcount += decode0(ibuf, obuf, obufcount);
181: }
182: }
183: }
184: if (obufcount == obuf.length)
185: return obuf;
186: byte[] ret = new byte[obufcount];
187: System.arraycopy(obuf, 0, ret, 0, obufcount);
188: return ret;
189: }
190:
191: /**
192: *
193: */
194: public static void decode(char[] data, int off, int len,
195: OutputStream ostream) throws IOException {
196: char[] ibuf = new char[4];
197: int ibufcount = 0;
198: byte[] obuf = new byte[3];
199: for (int i = off; i < off + len; i++) {
200: char ch = data[i];
201: if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
202: && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
203: ibuf[ibufcount++] = ch;
204: if (ibufcount == ibuf.length) {
205: ibufcount = 0;
206: int obufcount = decode0(ibuf, obuf, 0);
207: ostream.write(obuf, 0, obufcount);
208: }
209: }
210: }
211: }
212:
213: /**
214: *
215: */
216: public static void decode(String data, OutputStream ostream)
217: throws IOException {
218: char[] ibuf = new char[4];
219: int ibufcount = 0;
220: byte[] obuf = new byte[3];
221: for (int i = 0; i < data.length(); i++) {
222: char ch = data.charAt(i);
223: if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
224: && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
225: ibuf[ibufcount++] = ch;
226: if (ibufcount == ibuf.length) {
227: ibufcount = 0;
228: int obufcount = decode0(ibuf, obuf, 0);
229: ostream.write(obuf, 0, obufcount);
230: }
231: }
232: }
233: }
234:
235: /**
236: * Returns base64 representation of specified byte array.
237: */
238: public static String encode(byte[] data) {
239: return encode(data, 0, data.length);
240: }
241:
242: /**
243: * Returns base64 representation of specified byte array.
244: */
245: public static String encode(byte[] data, int off, int len) {
246: if (len <= 0)
247: return "";
248: char[] out = new char[len / 3 * 4 + 4];
249: int rindex = off;
250: int windex = 0;
251: int rest = len - off;
252: while (rest >= 3) {
253: int i = ((data[rindex] & 0xff) << 16)
254: + ((data[rindex + 1] & 0xff) << 8)
255: + (data[rindex + 2] & 0xff);
256: out[windex++] = S_BASE64CHAR[i >> 18];
257: out[windex++] = S_BASE64CHAR[(i >> 12) & 0x3f];
258: out[windex++] = S_BASE64CHAR[(i >> 6) & 0x3f];
259: out[windex++] = S_BASE64CHAR[i & 0x3f];
260: rindex += 3;
261: rest -= 3;
262: }
263: if (rest == 1) {
264: int i = data[rindex] & 0xff;
265: out[windex++] = S_BASE64CHAR[i >> 2];
266: out[windex++] = S_BASE64CHAR[(i << 4) & 0x3f];
267: out[windex++] = S_BASE64PAD;
268: out[windex++] = S_BASE64PAD;
269: } else if (rest == 2) {
270: int i = ((data[rindex] & 0xff) << 8)
271: + (data[rindex + 1] & 0xff);
272: out[windex++] = S_BASE64CHAR[i >> 10];
273: out[windex++] = S_BASE64CHAR[(i >> 4) & 0x3f];
274: out[windex++] = S_BASE64CHAR[(i << 2) & 0x3f];
275: out[windex++] = S_BASE64PAD;
276: }
277: return new String(out, 0, windex);
278: }
279:
280: /**
281: * Outputs base64 representation of the specified byte array to a byte stream.
282: */
283: public static void encode(byte[] data, int off, int len,
284: OutputStream ostream) throws IOException {
285: if (len <= 0)
286: return;
287: byte[] out = new byte[4];
288: int rindex = off;
289: int rest = len - off;
290: while (rest >= 3) {
291: int i = ((data[rindex] & 0xff) << 16)
292: + ((data[rindex + 1] & 0xff) << 8)
293: + (data[rindex + 2] & 0xff);
294: out[0] = (byte) S_BASE64CHAR[i >> 18];
295: out[1] = (byte) S_BASE64CHAR[(i >> 12) & 0x3f];
296: out[2] = (byte) S_BASE64CHAR[(i >> 6) & 0x3f];
297: out[3] = (byte) S_BASE64CHAR[i & 0x3f];
298: ostream.write(out, 0, 4);
299: rindex += 3;
300: rest -= 3;
301: }
302: if (rest == 1) {
303: int i = data[rindex] & 0xff;
304: out[0] = (byte) S_BASE64CHAR[i >> 2];
305: out[1] = (byte) S_BASE64CHAR[(i << 4) & 0x3f];
306: out[2] = (byte) S_BASE64PAD;
307: out[3] = (byte) S_BASE64PAD;
308: ostream.write(out, 0, 4);
309: } else if (rest == 2) {
310: int i = ((data[rindex] & 0xff) << 8)
311: + (data[rindex + 1] & 0xff);
312: out[0] = (byte) S_BASE64CHAR[i >> 10];
313: out[1] = (byte) S_BASE64CHAR[(i >> 4) & 0x3f];
314: out[2] = (byte) S_BASE64CHAR[(i << 2) & 0x3f];
315: out[3] = (byte) S_BASE64PAD;
316: ostream.write(out, 0, 4);
317: }
318: }
319:
320: /**
321: * Outputs base64 representation of the specified byte array to a character stream.
322: */
323: public static void encode(byte[] data, int off, int len,
324: Writer writer) throws IOException {
325: if (len <= 0)
326: return;
327: char[] out = new char[4];
328: int rindex = off;
329: int rest = len - off;
330: int output = 0;
331: while (rest >= 3) {
332: int i = ((data[rindex] & 0xff) << 16)
333: + ((data[rindex + 1] & 0xff) << 8)
334: + (data[rindex + 2] & 0xff);
335: out[0] = S_BASE64CHAR[i >> 18];
336: out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
337: out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
338: out[3] = S_BASE64CHAR[i & 0x3f];
339: writer.write(out, 0, 4);
340: rindex += 3;
341: rest -= 3;
342: output += 4;
343: if (output % 76 == 0)
344: writer.write("\n");
345: }
346: if (rest == 1) {
347: int i = data[rindex] & 0xff;
348: out[0] = S_BASE64CHAR[i >> 2];
349: out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
350: out[2] = S_BASE64PAD;
351: out[3] = S_BASE64PAD;
352: writer.write(out, 0, 4);
353: } else if (rest == 2) {
354: int i = ((data[rindex] & 0xff) << 8)
355: + (data[rindex + 1] & 0xff);
356: out[0] = S_BASE64CHAR[i >> 10];
357: out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
358: out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
359: out[3] = S_BASE64PAD;
360: writer.write(out, 0, 4);
361: }
362: }
363: }
|