com/quadcap/io/Base64OutputStream.java
Go to the documentation of this file.00001
package com.quadcap.io;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.ByteArrayOutputStream;
00042
import java.io.IOException;
00043
import java.io.OutputStream;
00044
00045
00046
00047
00048
00049
00050
00051
00052 public class Base64OutputStream extends OutputStream {
00053 OutputStream
out;
00054 int accum;
00055 int pos = 0;
00056 public boolean doLineBreaks =
true;
00057
00058 public static byte[]
base64 = {
00059 (byte)
'A', (byte)
'B', (byte)
'C', (byte)
'D',
00060 (byte)
'E', (byte)
'F', (byte)
'G', (byte)
'H',
00061 (byte)
'I', (byte)
'J', (byte)
'K', (byte)
'L',
00062 (byte)
'M', (byte)
'N', (byte)
'O', (byte)
'P',
00063 (byte)
'Q', (byte)
'R', (byte)
'S', (byte)
'T',
00064 (byte)
'U', (byte)
'V', (byte)
'W', (byte)
'X',
00065 (byte)
'Y', (byte)
'Z', (byte)
'a', (byte)
'b',
00066 (byte)
'c', (byte)
'd', (byte)
'e', (byte)
'f',
00067 (byte)
'g', (byte)
'h', (byte)
'i', (byte)
'j',
00068 (byte)
'k', (byte)
'l', (byte)
'm', (byte)
'n',
00069 (byte)
'o', (byte)
'p', (byte)
'q', (byte)
'r',
00070 (byte)
's', (byte)
't', (byte)
'u', (byte)
'v',
00071 (byte)
'w', (byte)
'x', (byte)
'y', (byte)
'z',
00072 (byte)
'0', (byte)
'1', (byte)
'2', (byte)
'3',
00073 (byte)
'4', (byte)
'5', (byte)
'6', (byte)
'7',
00074 (byte)
'8', (byte)
'9', (byte)
'+', (byte)
'/'
00075 };
00076
00077
00078
00079
00080 public Base64OutputStream() {
00081
this.out =
new ByteArrayOutputStream();
00082 }
00083
00084
00085
00086
00087 public Base64OutputStream(OutputStream out) {
00088
this.out = out;
00089 }
00090
00091
00092
00093
00094 public void write(
int c)
throws IOException {
00095
accum <<= 8;
00096
accum |= (c & 0xff);
00097
if ((++
pos % 3) == 0) {
00098
out.write(
base64[(
accum >> 18) & 0x3f]);
00099
out.write(
base64[(accum >> 12) & 0x3f]);
00100
out.write(
base64[(accum >> 6) & 0x3f]);
00101
out.write(
base64[(accum >> 0) & 0x3f]);
00102 accum = 0;
00103 }
00104
if (
doLineBreaks && (
pos % 54) == 0) {
00105
out.write(
'\r');
00106
out.write(
'\n');
00107 }
00108 }
00109
00110
00111
00112
00113 public void finish() throws IOException {
00114
int p =
pos % 3;
00115
if (p == 1) {
00116
accum <<= 16;
00117
out.write(
base64[(
accum >> 18) & 0x3f]);
00118
out.write(
base64[(accum >> 12) & 0x3f]);
00119
out.write(
'=');
00120
out.write(
'=');
00121 }
else if (p == 2) {
00122
accum <<= 8;
00123
out.write(
base64[(
accum >> 18) & 0x3f]);
00124
out.write(
base64[(accum >> 12) & 0x3f]);
00125
out.write(
base64[(accum >> 6) & 0x3f]);
00126
out.write(
'=');
00127 }
00128 }
00129
00130
00131
00132
00133 public String
toString() {
00134
return out.toString();
00135 }
00136 }