OSDN Git Service

Add new source files
[armadillo/armadillo1.git] / src / jp / sfjp / armadillo / archive / ArchiveOutputStream.java
1 package jp.sfjp.armadillo.archive;
2
3 import java.io.*;
4
5 /**
6  * The OutputStream for Archive.
7  */
8 public abstract class ArchiveOutputStream extends FilterOutputStream {
9
10     protected OutputStream frontStream;
11     protected long written;
12
13     private boolean closed;
14
15     public ArchiveOutputStream(OutputStream out) {
16         super(out);
17     }
18
19     protected final void ensureOpen() throws IOException {
20         if (closed)
21             throw new IOException("stream closed");
22     }
23
24     @Override
25     public void write(int b) throws IOException {
26         ensureOpen();
27         if (frontStream == null)
28             super.write(b);
29         else
30             frontStream.write(b);
31         ++written;
32     }
33
34     @Override
35     public void write(byte[] b, int off, int len) throws IOException {
36         ensureOpen();
37         if (frontStream == null)
38             super.write(b, off, len);
39         else
40             frontStream.write(b, off, len);
41         written += len;
42     }
43
44     @Override
45     public void flush() throws IOException {
46         ensureOpen();
47         if (frontStream == null)
48             super.flush();
49         else
50             frontStream.flush();
51     }
52
53     @Override
54     public void close() throws IOException {
55         try {
56             flush();
57         }
58         finally {
59             closed = true;
60             frontStream = null;
61             super.close();
62         }
63     }
64
65 }