OSDN Git Service

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