source: josm/trunk/src/com/kitfox/svg/xml/Base64OutputStream.java@ 4739

Last change on this file since 4739 was 4256, checked in by bastiK, 14 years ago

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.kitfox.svg.xml;
7
8import java.io.FilterOutputStream;
9import java.io.IOException;
10import java.io.OutputStream;
11
12/**
13 *
14 * @author kitfox
15 */
16public 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}
Note: See TracBrowser for help on using the repository browser.