OSDN Git Service

ニコニコ動画仕様変更対応. swfの取得にas3=1を追加.
[coroid/inqubus.git] / frontend / src / saccubus / util / Cws2Fws.java
1 package saccubus.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 import java.util.zip.InflaterInputStream;
12
13 /**
14  *
15  * @author yuki
16  */
17 public class Cws2Fws {
18
19         private static final String CWS = "CWS";
20         private static final String FWS = "FWS";
21
22         /**
23          * \88³\8fkSWF\82©\82Ç\82¤\82©\94»\92è\82·\82é.
24          * @param file \94»\92è\91Î\8fÛ.
25          * @return \88³\8fkSWF\82Å\82 \82ê\82Îtrue.
26          */
27         public static boolean isCws(File file) {
28                 BufferedInputStream bis = null;
29                 try {
30                         bis = new BufferedInputStream(new FileInputStream(file));
31                         byte header[] = new byte[CWS.length()];
32                         bis.read(header, 0, header.length);
33                         if (CWS.equals(new String(header))) {
34                                 return true;
35                         }
36                 } catch (IOException ex) {
37                         Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
38                 } finally {
39                         if (bis != null) {
40                                 try {
41                                         bis.close();
42                                 } catch (IOException ex) {
43                                         Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
44                                 }
45                         }
46                 }
47                 return false;
48         }
49
50         /**
51          * \88³\8fkSWF\82ð\93W\8aJ\82·\82é.
52          * @param in \93W\8aJ\91Î\8fÛ.
53          * @return \93W\8aJ\8cã\82Ì\83t\83@\83C\83\8b\91Î\8fÛ\82ª\88³\8fkSWF\82Å\82È\82¯\82ê\82Înull.
54          */
55         public static File createFws(File in) {
56                 if (!isCws(in)) {
57                         return null;
58                 }
59                 File out = new File("fws_tmp.swf");
60                 BufferedInputStream bis = null;
61                 BufferedOutputStream bos = null;
62                 try {
63                         byte buffer[] = new byte[1024];
64                         bis = new BufferedInputStream(new FileInputStream(in));
65                         bis.read(buffer, 0, CWS.length()); // CWS
66                         bis.read(buffer, 0, 5); // \82»\82Ì\91¼\83w\83b\83_
67
68                         bos = new BufferedOutputStream(new FileOutputStream(out));
69                         bos.write(FWS.getBytes());
70                         bos.write(buffer);
71
72                         InflaterInputStream iis = new InflaterInputStream(bis);
73                         while (true) {
74                                 int res = iis.read(buffer);
75                                 if (res < 0) {
76                                         break;
77                                 }
78                                 bos.write(buffer, 0, res);
79                         }
80                         return out;
81                 } catch (IOException ex) {
82                         Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
83                 } finally {
84                         if (bis != null) {
85                                 try {
86                                         bis.close();
87                                 } catch (IOException ex) {
88                                         Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
89                                 }
90                         }
91                         if (bos != null) {
92                                 try {
93                                         bos.close();
94                                 } catch (IOException ex) {
95                                         Logger.getLogger(Cws2Fws.class.getName()).log(Level.SEVERE, null, ex);
96                                 }
97                         }
98                 }
99                 return null;
100         }
101 }