1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | */
|
---|
34 |
|
---|
35 | package com.kitfox.svg.xml;
|
---|
36 |
|
---|
37 | import java.io.FilterInputStream;
|
---|
38 | import java.io.IOException;
|
---|
39 | import java.io.InputStream;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | *
|
---|
43 | * @author kitfox
|
---|
44 | */
|
---|
45 | public class Base64InputStream extends FilterInputStream
|
---|
46 | {
|
---|
47 | int buf; //Cached bytes to read
|
---|
48 | int bufSize; //Number of bytes waiting to be read from buffer
|
---|
49 | boolean drain = false; //After set, read no more chunks
|
---|
50 |
|
---|
51 | public Base64InputStream(InputStream in)
|
---|
52 | {
|
---|
53 | super(in);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public int read() throws IOException
|
---|
57 | {
|
---|
58 | if (drain && bufSize == 0)
|
---|
59 | {
|
---|
60 | return -1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (bufSize == 0)
|
---|
64 | {
|
---|
65 | //Read next chunk into 4 byte buffer
|
---|
66 | int chunk = in.read();
|
---|
67 | if (chunk == -1)
|
---|
68 | {
|
---|
69 | drain = true;
|
---|
70 | return -1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | //get remaining 3 bytes
|
---|
74 | for (int i = 0; i < 3; ++i)
|
---|
75 | {
|
---|
76 | int value = in.read();
|
---|
77 | if (value == -1)
|
---|
78 | {
|
---|
79 | throw new IOException("Early termination of base64 stream");
|
---|
80 | }
|
---|
81 | chunk = (chunk << 8) | (value & 0xff);
|
---|
82 | }
|
---|
83 |
|
---|
84 | //Check for special termination characters
|
---|
85 | if ((chunk & 0xffff) == (((byte)'=' << 8) | (byte)'='))
|
---|
86 | {
|
---|
87 | bufSize = 1;
|
---|
88 | drain = true;
|
---|
89 | }
|
---|
90 | else if ((chunk & 0xff) == (byte)'=')
|
---|
91 | {
|
---|
92 | bufSize = 2;
|
---|
93 | drain = true;
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | bufSize = 3;
|
---|
98 | }
|
---|
99 |
|
---|
100 | //Fill buffer with decoded characters
|
---|
101 | for (int i = 0; i < bufSize + 1; ++i)
|
---|
102 | {
|
---|
103 | buf = (buf << 6) | Base64Util.decodeByte((chunk >> 24) & 0xff);
|
---|
104 | chunk <<= 8;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | //Return nth remaing bte & decrement counter
|
---|
109 | return (buf >> (--bufSize * 8)) & 0xff;
|
---|
110 | }
|
---|
111 | }
|
---|