| 1 |
package uk.ac.babraham.FastQC.Utilities; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 30 |
super(new PushbackInputStream(in, size), size); |
| 31 |
this.size=size; |
| 32 |
} |
| 33 |
|
| 34 |
public MultiMemberGZIPInputStream(InputStream in) throws IOException |
| 35 |
{ |
| 36 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
int n = inf.getRemaining() - 8; |
| 76 |
if (n > 0) |
| 77 |
{ |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
((PushbackInputStream)this.in).unread(buf, len-n, n); |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
} |