File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\UnbufferedBase64InputStream.java.html

1 package hudson.util;
2
3 import org.apache.commons.codec.binary.Base64;
4
5 import java.io.DataInputStream;
6 import java.io.FilterInputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9
10 /**
11 * Filter InputStream that decodes base64 without doing any buffering.
12 *
13 * <p>
14 * This is slower implementation, but it won't consume unnecessary bytes from the underlying {@link InputStream},
15 * allowing the reader to switch between the unencoded bytes and base64 bytes.
16 *
17 * @author Kohsuke Kawaguchi
18 * @since 1.349
19 */
20 public class UnbufferedBase64InputStream extends FilterInputStream {
21 private byte[] encoded = new byte[4];
22 private byte[] decoded;
23 private int pos;
24 private final DataInputStream din;
25
26 public UnbufferedBase64InputStream(InputStream in) {
27 super(in);
28 din = new DataInputStream(in);
29 // initial placement to force the decoding in the next read()
30 pos = 4;
31 decoded = encoded;
32 }
33
34 @Override
35 public int read() throws IOException {
36 if (decoded.length==0)
37 return -1; // EOF
38
39 if (pos==decoded.length) {
40 din.readFully(encoded);
41 decoded = Base64.decodeBase64(encoded);
42 if (decoded.length==0) return -1; // EOF
43 pos = 0;
44 }
45
46 return (decoded[pos++])&0xFF;
47 }
48
49 @Override
50 public int read(byte[] b, int off, int len) throws IOException {
51 int i;
52 for (i=0; i<len; i++) {
53 int ch = read();
54 if (ch<0) break;
55 b[off+i] = (byte)ch;
56 }
57 return i==0 ? -1 : i;
58 }
59
60 @Override
61 public long skip(long n) throws IOException {
62 long r=0;
63 while (n>0) {
64 int ch = read();
65 if (ch<0) break;
66 n--; r++;
67 }
68 return r;
69 }
70 }
71