001: package com.sun.portal.util;
002:
003: import java.io.*;
004: import java.util.*;
005: import sun.misc.*;
006:
007: /**
008: * This class encodes characters using a variant of Base64 encoding that results
009: * in an encoded string that doesn't need to be escaped for use in HTTP URLs.
010: * Specifically, the following characters are remapped in this implementation:
011: *
012: * <pre>
013: * Location Base64 Http64
014: * -------- ------ ------
015: * 62 + -
016: * 63 / _
017: * (pad) = $
018: * </pre>
019: *
020: * The Base64 implementation relies on the <code>sun.misc.*</code> package
021: * included with Sun's implementation of the Java Platform; there is no
022: * guarantee that this implementation will be used in future versions of this
023: * class.
024: *
025: * @author Todd Fast, todd.fast@sun.com
026: * @author Mike Frisino, michael.frisino@sun.com
027: * @version JATO/1.2.2 $Id: Http64Encoder.java,v 1.1 2005/06/15 22:23:16 rt94277 Exp $
028: *
029: * This class is copied from JATO 1.2.2 util package
030: *
031: */
032: public class Http64Encoder extends CharacterEncoder {
033: /**
034: * Default constructor
035: *
036: */
037: public Http64Encoder() {
038: super ();
039: }
040:
041: /**
042: *
043: *
044: */
045: protected int bytesPerAtom() {
046: return 3;
047: }
048:
049: /**
050: *
051: *
052: */
053: protected int bytesPerLine() {
054: return 57;
055: }
056:
057: /**
058: *
059: *
060: */
061: protected void encodeLineSuffix(OutputStream aStream)
062: throws IOException {
063: // Do nothing
064: }
065:
066: /**
067: * enocodeAtom - Take three bytes of input and encode it as 4
068: * printable characters. Note that if the length in len is less
069: * than three is encodes either one or two '=' signs to indicate
070: * padding characters.
071: */
072: protected void encodeAtom(OutputStream outStream, byte data[],
073: int offset, int len) throws IOException {
074: byte a, b, c;
075:
076: if (len == 1) {
077: a = data[offset];
078: b = 0;
079: c = 0;
080: outStream.write(pem_array[(a >>> 2) & 0x3F]);
081: outStream.write(pem_array[((a << 4) & 0x30)
082: + ((b >>> 4) & 0xf)]);
083: outStream.write('$');
084: outStream.write('$');
085: } else if (len == 2) {
086: a = data[offset];
087: b = data[offset + 1];
088: c = 0;
089: outStream.write(pem_array[(a >>> 2) & 0x3F]);
090: outStream.write(pem_array[((a << 4) & 0x30)
091: + ((b >>> 4) & 0xf)]);
092: outStream.write(pem_array[((b << 2) & 0x3c)
093: + ((c >>> 6) & 0x3)]);
094: outStream.write('$');
095: } else {
096: a = data[offset];
097: b = data[offset + 1];
098: c = data[offset + 2];
099: outStream.write(pem_array[(a >>> 2) & 0x3F]);
100: outStream.write(pem_array[((a << 4) & 0x30)
101: + ((b >>> 4) & 0xf)]);
102: outStream.write(pem_array[((b << 2) & 0x3c)
103: + ((c >>> 6) & 0x3)]);
104: outStream.write(pem_array[c & 0x3F]);
105: }
106: }
107:
108: ////////////////////////////////////////////////////////////////////////////
109: // Class variables
110: ////////////////////////////////////////////////////////////////////////////
111:
112: private static final char pem_array[] = {
113: // 0 1 2 3 4 5 6 7
114: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
115: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
116: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
117: 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3
118: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4
119: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5
120: 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6
121: '4', '5', '6', '7', '8', '9', '-', '_' // 7
122: };
123:
124: ////////////////////////////////////////////////////////////////////////////
125: // Instance variables
126: ////////////////////////////////////////////////////////////////////////////
127: }
|