OSDN Git Service

Add new source files
[armadillo/armadillo1.git] / src / jp / sfjp / armadillo / archive / cab / CabCfData.java
1 package jp.sfjp.armadillo.archive.cab;
2
3 import java.io.*;
4 import java.util.zip.*;
5
6 public final class CabCfData extends FilterOutputStream {
7
8     ByteArrayOutputStream bos = new ByteArrayOutputStream();
9     OutputStream os; // front
10     private Deflater deflater;
11
12     int uncompsize = 0;
13     boolean finished;
14     private final CabCompressionType type;
15
16     public CabCfData(CabCompressionType type) {
17         super(null);
18         this.os = bos;
19         this.type = type;
20         switch (type) {
21             case No:
22                 os = bos;
23                 break;
24             case MSZIP:
25                 bos.write('C');
26                 bos.write('K');
27                 this.deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
28                 deflater.reset();
29                 os = new DeflaterOutputStream(bos, deflater);
30                 break;
31             case Quantum:
32             case LZX:
33             case Unknown:
34                 throw new UnsupportedOperationException("comptype: " + type);
35         }
36     }
37
38     @Override
39     public void write(int b) throws IOException {
40         if (finished)
41             throw new IOException("stream closed");
42         os.write(b);
43         ++uncompsize;
44     }
45
46     @Override
47     public void close() throws IOException {
48         finish();
49     }
50
51     public void finish() throws IOException {
52         if (finished)
53             return;
54         switch (type) {
55             case MSZIP:
56                 if (os instanceof DeflaterOutputStream) {
57                     DeflaterOutputStream dos = (DeflaterOutputStream)os;
58                     dos.finish();
59                     dos.flush();
60                 }
61                 break;
62             case No:
63                 os.flush();
64                 break;
65             default:
66                 // do nothing
67         }
68         os = bos;
69         finished = true;
70     }
71
72     public void writeInto(OutputStream out) throws IOException {
73         if (!finished)
74             throw new IOException("stream is not closed yet");
75         bos.writeTo(out);
76     }
77
78 }