Debian Codegraph @

uk/ac/babraham/FastQC/Utilities/MultiMemberGZIPInputStream.java

raw symbols SCIP
1 package uk.ac.babraham.FastQC.Utilities;
2
3 /**
4 * This class is a work round for a bug in the core java gzip decompression
5 * code which can't handle multiple compressed blocks being concatenated within
6 * the same file. It incorrectly reports that it has hit EOF when it gets to
7 * the end of the first compressed block.
8 *
9 * This work round was posted in the bug report at:
10 *
11 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4691425
12 *
13 * Unfortunately the Sun bug database doesn't show who posted it so I can't
14 * provide an attribution. They didn't specify under what licence this code
15 * was being provided, but I'm going to assume that since it was posted as
16 * a work round for a reported bug that it's open enough to use here.
17 */
18
19
20 import java.io.InputStream;
21 import java.io.PushbackInputStream;
22 import java.io.IOException;
23 import java.util.zip.GZIPInputStream;
24
25 public class MultiMemberGZIPInputStream extends GZIPInputStream {
26
27 public MultiMemberGZIPInputStream(InputStream in, int size) throws IOException
28 {
29 // Wrap the stream in a PushbackInputStream...
30 super(new PushbackInputStream(in, size), size);
31 this.size=size;
32 }
33
34 public MultiMemberGZIPInputStream(InputStream in) throws IOException
35 {
36 // Wrap the stream in a PushbackInputStream...
37 super(new PushbackInputStream(in, 1024));
38 this.size=-1;
39 }
40
41 private MultiMemberGZIPInputStream(MultiMemberGZIPInputStream parent) throws IOException
42 {
43 super(parent.in);
44 this.size=-1;
45 this.parent=parent.parent==null ? parent : parent.parent;
46 this.parent.child=this;
47 }
48
49 private MultiMemberGZIPInputStream(MultiMemberGZIPInputStream parent, int size) throws IOException
50 {
51 super(parent.in, size);
52 this.size=size;
53 this.parent=parent.parent==null ? parent : parent.parent;
54 this.parent.child=this;
55 }
56
57 private MultiMemberGZIPInputStream parent;
58 private MultiMemberGZIPInputStream child;
59 private int size;
60 private boolean eos;
61
62 public int read(byte[] inputBuffer, int inputBufferOffset, int inputBufferLen) throws IOException {
63
64 if (eos) { return -1;}
65 if (this.child!=null)
66 return this.child.read(inputBuffer, inputBufferOffset, inputBufferLen);
67
68 int charsRead=super.read(inputBuffer, inputBufferOffset, inputBufferLen);
69 if (charsRead==-1)
70 {
71 // Push any remaining buffered data back onto the stream
72 // If the stream is then not empty, use it to construct
73 // a new instance of this class and delegate this and any
74 // future calls to it...
75 int n = inf.getRemaining() - 8;
76 if (n > 0)
77 {
78 // More than 8 bytes remaining in deflater
79 // First 8 are gzip trailer. Add the rest to
80 // any un-read data...
81 ((PushbackInputStream)this.in).unread(buf, len-n, n);
82 }
83 else
84 {
85 // Nothing in the buffer. We need to know whether or not
86 // there is unread data available in the underlying stream
87 // since the base class will not handle an empty file.
88 // Read a byte to see if there is data and if so,
89 // push it back onto the stream...
90 byte[] b=new byte[1];
91 int ret=in.read(b,0,1);
92 if (ret==-1)
93 {
94 eos=true;
95 return -1;
96 }
97 else
98 ((PushbackInputStream)this.in).unread(b, 0, 1);
99 }
100
101 MultiMemberGZIPInputStream child;
102 if (this.size==-1)
103 child=new MultiMemberGZIPInputStream(this);
104 else
105 child=new MultiMemberGZIPInputStream(this, this.size);
106 return child.read(inputBuffer, inputBufferOffset, inputBufferLen);
107 }
108 else
109 return charsRead;
110 }
111
112 }