OSDN Git Service

1.22raのバグフィクス
[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      * \88³\8fkSWF\82©\82Ç\82¤\82©\94»\92è\82·\82é.
21      * @param file \94»\92è\91Î\8fÛ.
22      * @return \88³\8fkSWF\82Å\82 \82ê\82Î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      * \88³\8fkSWF\82ð\93W\8aJ\82·\82é.
49      * @param in \93W\8aJ\91Î\8fÛ.
50      * @return \93W\8aJ\8cã\82Ì\83t\83@\83C\83\8b\91Î\8fÛ\82ª\88³\8fkSWF\82Å\82È\82¯\82ê\82Î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); // \82»\82Ì\91¼\83w\83b\83_
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 }