OSDN Git Service

Add new source files
[armadillo/armadillo1.git] / src / jp / sfjp / armadillo / archive / ArchiveInputStream.java
1 package jp.sfjp.armadillo.archive;
2
3 import java.io.*;
4
5 /**
6  * The InputStream for Archive.
7  */
8 public abstract class ArchiveInputStream extends FilterInputStream {
9
10     protected InputStream frontStream;
11     protected long remaining;
12
13     private boolean closed;
14
15     public ArchiveInputStream(InputStream is) {
16         super(is);
17         this.closed = false;
18         this.remaining = 0;
19     }
20
21     protected final void ensureOpen() throws IOException {
22         if (closed)
23             throw new IOException("stream closed");
24     }
25
26     @Override
27     public int read() throws IOException {
28         ensureOpen();
29         if (frontStream == null)
30             return super.read();
31         else if (remaining <= 0) {
32             assert remaining == 0;
33             return -1;
34         }
35         else {
36             int read = frontStream.read();
37             if (read == -1)
38                 return -1;
39             --remaining;
40             return read;
41         }
42     }
43
44     @Override
45     public int read(byte[] b, int off, int len) throws IOException {
46         ensureOpen();
47         if (frontStream == null)
48             return super.read(b, off, len);
49         else if (remaining <= 0) {
50             assert remaining == 0;
51             return -1;
52         }
53         else {
54             long requiredLength = Math.min(len, remaining);
55             assert requiredLength <= Integer.MAX_VALUE;
56             int readLength = frontStream.read(b, off, (int)requiredLength);
57             assert readLength != 0 : "Read Zero";
58             if (readLength >= 0)
59                 remaining -= readLength;
60             return readLength;
61         }
62     }
63
64     @Override
65     public long skip(long n) throws IOException {
66         ensureOpen();
67         if (frontStream == null)
68             return super.skip(n);
69         else if (remaining <= 0) {
70             assert remaining == 0;
71             return -1;
72         }
73         else {
74             long skipped = frontStream.skip(n);
75             if (skipped > 0)
76                 remaining -= skipped;
77             return skipped;
78         }
79     }
80
81     @Override
82     public int available() throws IOException {
83         return 0;
84     }
85
86     @Override
87     public boolean markSupported() {
88         return false;
89     }
90
91     @Override
92     public synchronized void mark(int limit) {
93         // not supported
94     }
95
96     @Override
97     public synchronized void reset() throws IOException {
98         throw new IOException("mark/reset not supported");
99     }
100
101     @Override
102     public void close() throws IOException {
103         ensureOpen();
104         try {
105             super.close();
106         }
107         finally {
108             frontStream = null;
109             closed = true;
110         }
111     }
112
113 }