OSDN Git Service

ConverterのProfileをインタフェース化
[coroid/inqubus.git] / frontend / src / yukihane / swf / Cws2Fws.java
1 /** $Id$ */
2 package yukihane.swf;
3
4 import java.io.BufferedInputStream;
5 import java.io.BufferedOutputStream;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12 import java.util.zip.InflaterInputStream;
13
14 public class Cws2Fws {
15
16     private static final String CWS = "CWS";
17     private static final String FWS = "FWS";
18
19     /**
20      * 圧縮SWFかどうか判定する.
21      * @param file 判定対象.
22      * @return 圧縮SWFであればtrue.
23      */
24     public static boolean isCws(File file) {
25         BufferedInputStream bis = null;
26         try {
27             bis = new BufferedInputStream(new FileInputStream(file));
28             byte header[] = new byte[CWS.length()];
29             bis.read(header, 0, header.length);
30             if (CWS.equals(new String(header))) {
31                 return true;
32             }
33         } catch (IOException ex) {
34             Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
35         } finally {
36             if (bis != null) {
37                 try {
38                     bis.close();
39                 } catch (IOException ex) {
40                     Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
41                 }
42             }
43         }
44         return false;
45     }
46
47     /**
48      * 圧縮SWFを展開する.
49      * @param in 展開対象.
50      * @return 展開後のファイル. 対象が圧縮SWFでなければnull.
51      */
52     public static File createFws(File in, File out) {
53         if (!isCws(in)) {
54             return null;
55         }
56         BufferedInputStream bis = null;
57         BufferedOutputStream bos = null;
58         try {
59             byte buffer[] = new byte[1024];
60             bis = new BufferedInputStream(new FileInputStream(in));
61             bis.read(buffer, 0, CWS.length()); // CWS
62             bis.read(buffer, 0, 5); // その他ヘッダ
63
64             bos = new BufferedOutputStream(new FileOutputStream(out));
65             bos.write(FWS.getBytes());
66             bos.write(buffer, 0, 5);
67
68             InflaterInputStream iis = new InflaterInputStream(bis);
69             while (true) {
70                 int res = iis.read(buffer);
71                 if (res < 0) {
72                     break;
73                 }
74                 bos.write(buffer, 0, res);
75             }
76             return out;
77         } catch (IOException ex) {
78             Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
79         } finally {
80             if (bis != null) {
81                 try {
82                     bis.close();
83                 } catch (IOException ex) {
84                     Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
85                 }
86             }
87             if (bos != null) {
88                 try {
89                     bos.close();
90                 } catch (IOException ex) {
91                     Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
92                 }
93             }
94         }
95         return null;
96     }
97 }