| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
import java.io.FilterOutputStream; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.OutputStream; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class Base64OutputStream extends FilterOutputStream { |
| 30 |
private final Base64.Coder coder; |
| 31 |
private final int flags; |
| 32 |
|
| 33 |
private byte[] buffer = null; |
| 34 |
private int bpos = 0; |
| 35 |
|
| 36 |
private static byte[] EMPTY = new byte[0]; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public Base64OutputStream(OutputStream out, int flags) { |
| 47 |
this(out, flags, true); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@UnsupportedAppUsage |
| 63 |
public Base64OutputStream(OutputStream out, int flags, boolean encode) { |
| 64 |
super(out); |
| 65 |
this.flags = flags; |
| 66 |
if (encode) { |
| 67 |
coder = new Base64.Encoder(flags, null); |
| 68 |
} else { |
| 69 |
coder = new Base64.Decoder(flags, null); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public void write(int b) throws IOException { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if (buffer == null) { |
| 80 |
buffer = new byte[1024]; |
| 81 |
} |
| 82 |
if (bpos >= buffer.length) { |
| 83 |
|
| 84 |
internalWrite(buffer, 0, bpos, false); |
| 85 |
bpos = 0; |
| 86 |
} |
| 87 |
buffer[bpos++] = (byte) b; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private void flushBuffer() throws IOException { |
| 95 |
if (bpos > 0) { |
| 96 |
internalWrite(buffer, 0, bpos, false); |
| 97 |
bpos = 0; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public void write(byte[] b, int off, int len) throws IOException { |
| 102 |
if (len <= 0) return; |
| 103 |
flushBuffer(); |
| 104 |
internalWrite(b, off, len, false); |
| 105 |
} |
| 106 |
|
| 107 |
public void close() throws IOException { |
| 108 |
IOException thrown = null; |
| 109 |
try { |
| 110 |
flushBuffer(); |
| 111 |
internalWrite(EMPTY, 0, 0, true); |
| 112 |
} catch (IOException e) { |
| 113 |
thrown = e; |
| 114 |
} |
| 115 |
|
| 116 |
try { |
| 117 |
if ((flags & Base64.NO_CLOSE) == 0) { |
| 118 |
out.close(); |
| 119 |
} else { |
| 120 |
out.flush(); |
| 121 |
} |
| 122 |
} catch (IOException e) { |
| 123 |
if (thrown == null) { |
| 124 |
thrown = e; |
| 125 |
} else { |
| 126 |
thrown.addSuppressed(e); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (thrown != null) { |
| 131 |
throw thrown; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private void internalWrite(byte[] b, int off, int len, boolean finish) throws IOException { |
| 142 |
coder.output = embiggen(coder.output, coder.maxOutputSize(len)); |
| 143 |
if (!coder.process(b, off, len, finish)) { |
| 144 |
throw new Base64DataException("bad base-64"); |
| 145 |
} |
| 146 |
out.write(coder.output, 0, coder.op); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private byte[] embiggen(byte[] b, int len) { |
| 154 |
if (b == null || b.length < len) { |
| 155 |
return new byte[len]; |
| 156 |
} else { |
| 157 |
return b; |
| 158 |
} |
| 159 |
} |
| 160 |
} |