| 1 |
// Copyright 2003, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.FilterOutputStream; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.OutputStream; |
| 7 |
|
| 8 |
/** |
| 9 |
* The Base64OutputStream encodes binary data according to RFC 2045. |
| 10 |
* |
| 11 |
* @author Mark Donszelmann |
| 12 |
* @version $Id: Base64OutputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 13 |
*/ |
| 14 |
public class Base64OutputStream extends FilterOutputStream implements |
| 15 |
FinishableOutputStream { |
| 16 |
|
| 17 |
private int MAX_LINE_LENGTH = 74; |
| 18 |
|
| 19 |
private int position; |
| 20 |
|
| 21 |
private byte[] buffer; |
| 22 |
|
| 23 |
private int lineLength; |
| 24 |
|
| 25 |
private static final char intToBase64[] = { 'A', 'B', 'C', 'D', 'E', 'F', |
| 26 |
'G', 'H', // 0 - 7 |
| 27 |
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 - 15 |
| 28 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 - 23 |
| 29 |
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 - 31 |
| 30 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 - 39 |
| 31 |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 - 47 |
| 32 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 - 55 |
| 33 |
'4', '5', '6', '7', '8', '9', '+', '/' // 56 - 63 |
| 34 |
}; |
| 35 |
|
| 36 |
private String newline = "\n"; |
| 37 |
|
| 38 |
/** |
| 39 |
* Creates a Base64 output stream for given output |
| 40 |
* |
| 41 |
* @param out stream to write to |
| 42 |
*/ |
| 43 |
public Base64OutputStream(OutputStream out) { |
| 44 |
super(out); |
| 45 |
buffer = new byte[3]; |
| 46 |
position = 0; |
| 47 |
lineLength = 0; |
| 48 |
try { |
| 49 |
newline = System.getProperty("line.separator"); |
| 50 |
} catch (SecurityException e) { |
| 51 |
// ignored; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public void write(int a) throws IOException { |
| 56 |
buffer[position++] = (byte) a; |
| 57 |
if (position < buffer.length) |
| 58 |
return; |
| 59 |
|
| 60 |
// System.out.print(Integer.toHexString(buffer[0])+", |
| 61 |
// "+Integer.toHexString(buffer[1])+", |
| 62 |
// "+Integer.toHexString(buffer[2])+" "); |
| 63 |
writeTuple(); |
| 64 |
// writeNewLine(); |
| 65 |
|
| 66 |
lineLength += 4; |
| 67 |
if (lineLength >= MAX_LINE_LENGTH) { |
| 68 |
writeNewLine(); |
| 69 |
lineLength = 0; |
| 70 |
} |
| 71 |
|
| 72 |
position = 0; |
| 73 |
} |
| 74 |
|
| 75 |
public void finish() throws IOException { |
| 76 |
writeTuple(); |
| 77 |
flush(); |
| 78 |
if (out instanceof FinishableOutputStream) { |
| 79 |
((FinishableOutputStream) out).finish(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public void close() throws IOException { |
| 84 |
finish(); |
| 85 |
super.close(); |
| 86 |
} |
| 87 |
|
| 88 |
private void writeTuple() throws IOException { |
| 89 |
int data = (position > 0 ? (buffer[0] << 16) & 0x00FF0000 : 0) |
| 90 |
| (position > 1 ? (buffer[1] << 8) & 0x0000FF00 : 0) |
| 91 |
| (position > 2 ? (buffer[2]) & 0x000000FF : 0); |
| 92 |
|
| 93 |
// System.out.println(Integer.toHexString(data)); |
| 94 |
switch (position) { |
| 95 |
case 3: |
| 96 |
// System.out.println("\n*** "+((data >> 18) & 0x3f) +", "+((data >> |
| 97 |
// 12) & 0x3f)+", "+ |
| 98 |
// ((data >> 6) & 0x3f) +", "+((data ) & 0x3f)); |
| 99 |
out.write(intToBase64[(data >> 18) & 0x3f]); |
| 100 |
out.write(intToBase64[(data >> 12) & 0x3f]); |
| 101 |
out.write(intToBase64[(data >> 6) & 0x3f]); |
| 102 |
out.write(intToBase64[(data) & 0x3f]); |
| 103 |
return; |
| 104 |
|
| 105 |
case 2: |
| 106 |
out.write(intToBase64[(data >> 18) & 0x3f]); |
| 107 |
out.write(intToBase64[(data >> 12) & 0x3f]); |
| 108 |
out.write(intToBase64[(data >> 6) & 0x3f]); |
| 109 |
out.write('='); |
| 110 |
return; |
| 111 |
|
| 112 |
case 1: |
| 113 |
out.write(intToBase64[(data >> 18) & 0x3f]); |
| 114 |
out.write(intToBase64[(data >> 12) & 0x3f]); |
| 115 |
out.write('='); |
| 116 |
out.write('='); |
| 117 |
return; |
| 118 |
|
| 119 |
default: |
| 120 |
return; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
private void writeNewLine() throws IOException { |
| 125 |
// write a newline |
| 126 |
for (int i=0; i < newline.length(); i++) { |
| 127 |
out.write(newline.charAt(i)); |
| 128 |
} |
| 129 |
} |
| 130 |
} |