001: package com.sun.portal.util;
002:
003: import java.io.*;
004: import java.util.*;
005: import sun.misc.*;
006:
007: /**
008: * This class decodes characters that were perviously encoded using a variant of
009: * Base64 encoding. This encoding results in an encoded string that doesn't
010: * need to be escaped for use in HTTP URLs. Specifically, the following
011: * characters are remapped in this implementation:
012: *
013: * <pre>
014: * Location Base64 Http64
015: * -------- ------ ------
016: * 62 + -
017: * 63 / _
018: * (pad) = $
019: * </pre>
020: *
021: * The Base64 implementation relies on the <code>sun.misc.*</code> package
022: * included with Sun's implementation of the Java Platform; there is no
023: * guarantee that this implementation will be used in future versions of this
024: * class.
025: *
026: * @author Todd Fast, todd.fast@sun.com
027: * @author Mike Frisino, michael.frisino@sun.com
028: * @version JATO/1.2.2 $Id: Http64Decoder.java,v 1.1 2005/06/15 22:23:16 rt94277 Exp $
029: * This class is copied from JATO 1.2.2 util package.
030: *
031: */
032: public class Http64Decoder extends CharacterDecoder {
033: /**
034: * Default constructor
035: *
036: */
037: public Http64Decoder() {
038: super ();
039: }
040:
041: /**
042: *
043: *
044: */
045: protected int bytesPerAtom() {
046: return 4;
047: }
048:
049: /**
050: *
051: *
052: */
053: protected int bytesPerLine() {
054: // Any multiple of 4 will do, 72 might be common
055: return 72;
056: }
057:
058: /**
059: * The signature of the decodeAtom() method change in JDK 1.4. This method
060: * reflects that change.
061: *
062: * @since jato/1.2.1
063: */
064: protected void decodeAtom(PushbackInputStream inStream,
065: OutputStream outStream, int rem) throws java.io.IOException {
066: decodeAtom((InputStream) inStream, outStream, rem);
067: }
068:
069: /**
070: * The signature of the decodeAtom() method change in JDK 1.4. This method
071: * reflects the original 1.3 version of the method.
072: *
073: */
074: protected void decodeAtom(InputStream inStream,
075: OutputStream outStream, int rem) throws java.io.IOException {
076: int i;
077: byte a = -1;
078: byte b = -1;
079: byte c = -1;
080: byte d = -1;
081:
082: if (rem < 2) {
083: throw new CEFormatException(
084: "BASE64Decoder: Not enough bytes for an atom.");
085: }
086:
087: do {
088: i = inStream.read();
089: if (i == -1)
090: throw new CEStreamExhausted();
091: } while (i == '\n' || i == '\r');
092:
093: decode_buffer[0] = (byte) i;
094:
095: i = readFully(inStream, decode_buffer, 1, rem - 1);
096:
097: if (i == -1)
098: throw new CEStreamExhausted();
099:
100: if (rem > 3 && decode_buffer[3] == '$')
101: rem = 3;
102:
103: if (rem > 2 && decode_buffer[2] == '$')
104: rem = 2;
105:
106: switch (rem) {
107: case 4:
108: d = pem_convert_array[decode_buffer[3] & 0xff];
109: // NOBREAK
110: case 3:
111: c = pem_convert_array[decode_buffer[2] & 0xff];
112: // NOBREAK
113: case 2:
114: b = pem_convert_array[decode_buffer[1] & 0xff];
115: a = pem_convert_array[decode_buffer[0] & 0xff];
116: break;
117: }
118:
119: switch (rem) {
120: case 2:
121: outStream
122: .write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
123: break;
124: case 3:
125: outStream
126: .write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
127: outStream
128: .write((byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)));
129: break;
130: case 4:
131: outStream
132: .write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
133: outStream
134: .write((byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)));
135: outStream.write((byte) (((c << 6) & 0xc0) | (d & 0x3f)));
136: break;
137: }
138: }
139:
140: ////////////////////////////////////////////////////////////////////////////
141: // Class variables
142: ////////////////////////////////////////////////////////////////////////////
143:
144: private final static char pem_array[] = {
145: // 0 1 2 3 4 5 6 7
146: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
147: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
148: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
149: 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3
150: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4
151: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5
152: 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6
153: '4', '5', '6', '7', '8', '9', '-', '_' // 7
154: };
155:
156: private final static byte pem_convert_array[] = new byte[256];
157:
158: ////////////////////////////////////////////////////////////////////////////
159: // Instance variables
160: ////////////////////////////////////////////////////////////////////////////
161:
162: byte decode_buffer[] = new byte[4];
163:
164: ////////////////////////////////////////////////////////////////////////////
165: // Initializers
166: ////////////////////////////////////////////////////////////////////////////
167:
168: static {
169: for (int i = 0; i < 255; i++)
170: pem_convert_array[i] = -1;
171:
172: for (int i = 0; i < pem_array.length; i++)
173: pem_convert_array[pem_array[i]] = (byte) i;
174: }
175: }
|