001: /*
002: * @(#)Base64Encoder.java 1.1 05/01/20
003: *
004: * Copyright (c) 2004,2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.io;
010:
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.OutputStream;
016:
017: public class Base64Encoder {
018: private static final int BUFFER_SIZE = 4096;
019: private static final char[] table = { 'A', 'B', 'C', 'D', 'E', 'F',
020: 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
021: 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
022: 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
023: 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
024: '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' };
025:
026: public Base64Encoder() {
027: }
028:
029: private final int get1(byte buf[], int offset) {
030: return (buf[offset] & 0xfc) >> 2;
031: }
032:
033: private final int get2(byte buf[], int offset) {
034: return ((buf[offset] & 0x3) << 4)
035: | ((buf[offset + 1] & 0xf0) >>> 4);
036: }
037:
038: private final int get3(byte buf[], int offset) {
039: return ((buf[offset + 1] & 0x0f) << 2)
040: | ((buf[offset + 2] & 0xc0) >>> 6);
041: }
042:
043: private static final int get4(byte buf[], int offset) {
044: return buf[offset + 2] & 0x3f;
045: }
046:
047: public void encode(InputStream in, OutputStream out)
048: throws IOException {
049: byte buffer[] = new byte[BUFFER_SIZE];
050: int nread = -1;
051: int offset = 0;
052: int count = 0;
053: while ((nread = in.read(buffer, offset, BUFFER_SIZE - offset)) > 0) {
054: if ((nread + offset) >= 3) {
055: nread += offset;
056: offset = 0;
057: while (offset + 3 <= nread) {
058: int c1 = get1(buffer, offset);
059: int c2 = get2(buffer, offset);
060: int c3 = get3(buffer, offset);
061: int c4 = get4(buffer, offset);
062: switch (count) {
063: case 73:
064: out.write(table[c1]);
065: out.write(table[c2]);
066: out.write(table[c3]);
067: out.write('\n');
068: out.write(table[c4]);
069: count = 1;
070: break;
071: case 74:
072: out.write(table[c1]);
073: out.write(table[c2]);
074: out.write('\n');
075: out.write(table[c3]);
076: out.write(table[c4]);
077: count = 2;
078: break;
079: case 75:
080: out.write(table[c1]);
081: out.write('\n');
082: out.write(table[c2]);
083: out.write(table[c3]);
084: out.write(table[c4]);
085: count = 3;
086: break;
087: case 76:
088: out.write('\n');
089: out.write(table[c1]);
090: out.write(table[c2]);
091: out.write(table[c3]);
092: out.write(table[c4]);
093: count = 4;
094: break;
095: default:
096: out.write(table[c1]);
097: out.write(table[c2]);
098: out.write(table[c3]);
099: out.write(table[c4]);
100: count += 4;
101: break;
102: }
103: offset += 3;
104: }
105: for (int i = 0; i < 3; i++) {
106: buffer[i] = (i < nread - offset) ? buffer[offset
107: + i] : ((byte) 0);
108: }
109: offset = nread - offset;
110: } else {
111: offset += nread;
112: }
113: }
114: switch (offset) {
115: case 1:
116: out.write(table[get1(buffer, 0)]);
117: out.write(table[get2(buffer, 0)]);
118: out.write('=');
119: out.write('=');
120: break;
121: case 2:
122: out.write(table[get1(buffer, 0)]);
123: out.write(table[get2(buffer, 0)]);
124: out.write(table[get3(buffer, 0)]);
125: out.write('=');
126: }
127: return;
128: }
129:
130: public byte[] encode(byte[] bytes) throws IOException {
131: InputStream in = new ByteArrayInputStream(bytes);
132: ByteArrayOutputStream out = new ByteArrayOutputStream();
133: encode(in, out);
134: return out.toByteArray();
135: }
136:
137: public String encode(String input) throws IOException {
138: int len = input.length();
139: byte bytes[] = new byte[len];
140: for (int i = 0; i < len; i++) {
141: bytes[i] = (byte) input.charAt(i);
142: }
143: InputStream in = new ByteArrayInputStream(bytes);
144: ByteArrayOutputStream out = new ByteArrayOutputStream();
145: encode(in, out);
146: return out.toString();
147: }
148: }
|