1 | /*
|
---|
2 | * To change this template, choose Tools | Templates
|
---|
3 | * and open the template in the editor.
|
---|
4 | */
|
---|
5 |
|
---|
6 | package com.kitfox.svg.xml;
|
---|
7 |
|
---|
8 | import java.io.FilterOutputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.OutputStream;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | *
|
---|
14 | * @author kitfox
|
---|
15 | */
|
---|
16 | public class Base64OutputStream extends FilterOutputStream
|
---|
17 | {
|
---|
18 | int buf;
|
---|
19 | int numBytes;
|
---|
20 | int numChunks;
|
---|
21 |
|
---|
22 | public Base64OutputStream(OutputStream out)
|
---|
23 | {
|
---|
24 | super(out);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void flush() throws IOException
|
---|
28 | {
|
---|
29 | out.flush();
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void close() throws IOException
|
---|
33 | {
|
---|
34 | switch (numBytes)
|
---|
35 | {
|
---|
36 | case 1:
|
---|
37 | buf <<= 4;
|
---|
38 | out.write(getBase64Byte(1));
|
---|
39 | out.write(getBase64Byte(0));
|
---|
40 | out.write('=');
|
---|
41 | out.write('=');
|
---|
42 | break;
|
---|
43 | case 2:
|
---|
44 | buf <<= 2;
|
---|
45 | out.write(getBase64Byte(2));
|
---|
46 | out.write(getBase64Byte(1));
|
---|
47 | out.write(getBase64Byte(0));
|
---|
48 | out.write('=');
|
---|
49 | break;
|
---|
50 | case 3:
|
---|
51 | out.write(getBase64Byte(3));
|
---|
52 | out.write(getBase64Byte(2));
|
---|
53 | out.write(getBase64Byte(1));
|
---|
54 | out.write(getBase64Byte(0));
|
---|
55 | break;
|
---|
56 | default:
|
---|
57 | assert false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | out.close();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void write(int b) throws IOException
|
---|
64 | {
|
---|
65 | buf = (buf << 8) | (0xff & b);
|
---|
66 | numBytes++;
|
---|
67 |
|
---|
68 | if (numBytes == 3)
|
---|
69 | {
|
---|
70 | out.write(getBase64Byte(3));
|
---|
71 | out.write(getBase64Byte(2));
|
---|
72 | out.write(getBase64Byte(1));
|
---|
73 | out.write(getBase64Byte(0));
|
---|
74 |
|
---|
75 | numBytes = 0;
|
---|
76 | numChunks++;
|
---|
77 | if (numChunks == 16)
|
---|
78 | {
|
---|
79 | // out.write('\r');
|
---|
80 | // out.write('\n');
|
---|
81 | numChunks = 0;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public byte getBase64Byte(int index)
|
---|
87 | {
|
---|
88 | return Base64Util.encodeByte((buf >> (index * 6)) & 0x3f);
|
---|
89 | }
|
---|
90 | }
|
---|