Ignore:
Timestamp:
10/26/07 17:54:04 (4 years ago)
Author:
sys
Message:

swf2props : parsing des formats FLV et SWF

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Jav_EXT/com/scenari/ext/swf/SwfHeader.java

    r9867 r9878  
    11package com.scenari.ext.swf; 
     2 
    23/* 
    3  * FSHeader.java 
     4 * Code inspiré de FSHeader.java pour la partie SWF : 
    45 *  
    56 * Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved. 
     
    2829 * OF THE POSSIBILITY OF SUCH DAMAGE. 
    2930 */ 
    30 import java.io.File; 
    31 import java.io.FileInputStream; 
    32 import java.io.FileNotFoundException; 
     31 
    3332import java.io.IOException; 
    3433import java.io.InputStream; 
     
    3635import java.util.zip.Inflater; 
    3736 
     37public class SwfHeader extends FlashHeader { 
    3838 
    39 public final class FSHeader { 
    4039        public static void main(String[] args) { 
    4140                try { 
    42                         FSHeader header = new FSHeader(args[0]); 
     41                        SwfHeader header = new SwfHeader(args[0]); 
    4342 
    4443                        StringBuffer buffer = new StringBuffer(); 
    4544 
    4645                        buffer.append("File: ").append(args[0]).append("\n"); 
    47                         buffer.append("Signature: ").append(header.getSignature()).append( 
    48                                         "\n"); 
     46                        buffer.append("Signature: ").append(header.getSignature()).append("\n"); 
    4947                        buffer.append("Version: ").append(header.getVersion()).append("\n"); 
    5048                        buffer.append("Frame: "); 
    51                         buffer.append("(").append(header.getMinX()).append(",").append( 
    52                                         header.getMinY()).append(") "); 
    53                         buffer.append("(").append(header.getMaxX()).append(",").append( 
    54                                         header.getMaxY()).append(")\n"); 
    55                         buffer.append("Length: ").append(header.getLength() + " bytes") 
    56                                         .append("\n"); 
    57                         buffer.append("FrameRate: ").append( 
    58                                         header.getFrameRate() + " per second").append("\n"); 
    59                         buffer.append("Number Of Frames: ").append( 
    60                                         header.getNumberOfFrames()).append("\n"); 
     49                        buffer.append("(").append(header.getMinX()).append(",").append(header.getMinY()).append(") "); 
     50                        buffer.append("(").append(header.getMaxX()).append(",").append(header.getMaxY()).append(")\n"); 
     51                        buffer.append("Length: ").append(header.getLength() + " bytes").append("\n"); 
     52                        buffer.append("FrameRate: ").append(header.getFrameRate() + " per second").append("\n"); 
     53                        buffer.append("Number Of Frames: ").append(header.getNumberOfFrames()).append("\n"); 
    6154 
    6255                        System.out.println(buffer.toString()); 
     
    6659        } 
    6760 
    68         private String signature = null; 
     61        protected int fMinX = 0; 
    6962 
    70         private int version = 0; 
     63        protected int fMinY = 0; 
    7164 
    72         private int minX = 0; 
     65        protected int fMaxX = 0; 
    7366 
    74         private int minY = 0; 
     67        protected int fMaxY = 0; 
    7568 
    76         private int maxX = 0; 
     69        protected int fLength = 0; 
    7770 
    78         private int maxY = 0; 
     71        protected float fFrameRate = 0.0f; 
    7972 
    80         private int length = 0; 
     73        protected int fFrameCount = 0; 
    8174 
    82         private float frameRate = 0.0f; 
    83  
    84         private int frameCount = 0; 
    85          
    86         protected InputStream fStreamForMore = null; 
    87  
    88         public FSHeader(String filename) throws IOException, DataFormatException { 
     75        public SwfHeader(String filename) throws Exception { 
    8976                decodeFromFile(filename); 
    9077        } 
    9178 
    92         public FSHeader(byte[] pData) throws IOException, DataFormatException { 
    93                 decodeFromData(pData); 
     79        public SwfHeader(byte[] pData) throws IOException, DataFormatException { 
     80                decodeFromData(pData, null); 
    9481        } 
    9582 
    96         public FSHeader(byte[] pData, InputStream pStreamForMoreData) throws IOException, DataFormatException { 
    97                 fStreamForMore = pStreamForMoreData; 
    98                 decodeFromData(pData); 
    99         } 
    100  
    101         public String getSignature() { 
    102                 return signature; 
    103         } 
    104  
    105         public int getVersion() { 
    106                 return version; 
     83        public SwfHeader(byte[] pData, InputStream pStreamForMoreData) throws IOException, DataFormatException { 
     84                decodeFromData(pData, pStreamForMoreData); 
    10785        } 
    10886 
    10987        public int getLength() { 
    110                 return length; 
     88                return fLength; 
    11189        } 
    11290 
    11391        public int getMinX() { 
    114                 return minX; 
     92                return fMinX; 
    11593        } 
    11694 
    11795        public int getMaxX() { 
    118                 return maxX; 
     96                return fMaxX; 
    11997        } 
    12098 
    12199        public int getMinY() { 
    122                 return minY; 
     100                return fMinY; 
    123101        } 
    124102 
    125103        public int getMaxY() { 
    126                 return maxY; 
     104                return fMaxY; 
    127105        } 
    128106 
    129107        public float getFrameRate() { 
    130                 return frameRate; 
     108                return fFrameRate; 
    131109        } 
    132110 
    133111        public float getNumberOfFrames() { 
    134                 return frameCount; 
     112                return fFrameCount; 
    135113        } 
    136114 
    137         public void decodeFromFile(String fileName) throws FileNotFoundException, 
    138                         DataFormatException, IOException { 
    139                 FileInputStream fileContents = null; 
     115        public void decodeFromData(byte[] pBytes, InputStream pStreamForMoreData) throws DataFormatException, IOException { 
     116                FSCoder vCoder = null; 
    140117 
    141                 File swfFile = new File(fileName); 
     118                if (!isSwf(pBytes)) throw new DataFormatException("Data does not start with a valid Flash (SWF) signature."); 
    142119 
    143                 fileContents = new FileInputStream(swfFile); 
     120                if (pBytes[0] == 0x43) { 
     121                        vCoder = new FSCoder(FSCoder.LITTLE_ENDIAN, unzip(pBytes, pStreamForMoreData)); 
     122                } else { 
     123                        vCoder = new FSCoder(FSCoder.LITTLE_ENDIAN, pBytes); 
     124                } 
    144125 
    145                 int fileLength = (int) swfFile.length(); 
    146                 byte[] contents = new byte[fileLength]; 
     126                fSignature = vCoder.readString(3, "UTF8"); 
     127                fVersion = vCoder.readWord(1, false); 
     128                fLength = vCoder.readWord(4, false); 
    147129 
    148                 fileContents.read(contents); 
    149                 fileContents.close(); 
     130                vCoder.alignToByte(); 
    150131 
    151                 decodeFromData(contents); 
     132                int vFieldSize = vCoder.readBits(5, false); 
     133 
     134                fMinX = vCoder.readBits(vFieldSize, true); 
     135                fMaxX = vCoder.readBits(vFieldSize, true); 
     136                fMinY = vCoder.readBits(vFieldSize, true); 
     137                fMaxY = vCoder.readBits(vFieldSize, true); 
     138 
     139                vCoder.alignToByte(); 
     140 
     141                fFrameRate = vCoder.readFixedWord(1, 1); 
     142                fFrameCount = vCoder.readWord(2, false); 
    152143        } 
    153144 
    154         public void decodeFromData(byte[] bytes) throws DataFormatException, 
    155                         IOException { 
    156                 FSCoder coder = null; 
    157  
    158                 isFlash(bytes); 
    159  
    160                 if (bytes[0] == 0x43) 
    161                         coder = new FSCoder(FSCoder.LITTLE_ENDIAN, unzip(bytes)); 
    162                 else 
    163                         coder = new FSCoder(FSCoder.LITTLE_ENDIAN, bytes); 
    164  
    165                 signature = coder.readString(3, "UTF8"); 
    166                 version = coder.readWord(1, false); 
    167                 length = coder.readWord(4, false); 
    168  
    169                 coder.alignToByte(); 
    170  
    171                 int fieldSize = coder.readBits(5, false); 
    172  
    173                 minX = coder.readBits(fieldSize, true); 
    174                 maxX = coder.readBits(fieldSize, true); 
    175                 minY = coder.readBits(fieldSize, true); 
    176                 maxY = coder.readBits(fieldSize, true); 
    177  
    178                 coder.alignToByte(); 
    179  
    180                 frameRate = coder.readFixedWord(1, 1); 
    181                 frameCount = coder.readWord(2, false); 
     145        protected static boolean isSwf(byte[] pBytes) throws DataFormatException { 
     146                if (pBytes == null || pBytes.length < 8) return false; 
     147                return (pBytes[0] == 0x43 || pBytes[0] == 0x46) && pBytes[1] == 0x57 && pBytes[2] == 0x53; 
    182148        } 
    183149 
    184         private void isFlash(byte[] bytes) throws DataFormatException { 
    185                 if (bytes == null || bytes.length < 8) 
    186                         throw new DataFormatException("Flash data is null or empty."); 
    187  
    188                 boolean isFlash = (bytes[0] == 0x43 || bytes[0] == 0x46) 
    189                                 && bytes[1] == 0x57 && bytes[2] == 0x53; 
    190  
    191                 if (isFlash == false) 
    192                         throw new DataFormatException( 
    193                                         "Data does not start with a valid Flash signature."); 
    194         } 
    195  
    196         private byte[] unzip(byte[] bytes) throws DataFormatException { 
     150        private byte[] unzip(byte[] pBytes, InputStream pStreamForMoreData) throws DataFormatException { 
    197151                int movieLength = 0; 
    198152 
    199153                for (int i = 0; i < 4; i++) 
    200                         movieLength += (bytes[i + 4] & 0x000000FF) << (i * 8); 
     154                        movieLength += (pBytes[i + 4] & 0x000000FF) << (i * 8); 
    201155 
    202156                movieLength = Math.min(movieLength, 512); 
     
    206160 
    207161                for (int i = 0; i < 8; i++) 
    208                         data[i] = bytes[i]; 
     162                        data[i] = pBytes[i]; 
    209163 
    210164                int vRead = 0; 
    211165                Inflater inflater = new Inflater(); 
    212                 inflater.setInput(bytes, 8, bytes.length - 8); 
     166                inflater.setInput(pBytes, 8, pBytes.length - 8); 
    213167                vRead = inflater.inflate(data, 8, movieLength - 8) + 8; 
    214                 if(fStreamForMore != null) while(vRead < data.length && ! inflater.finished()) { 
     168                if (pStreamForMoreData != null) while (vRead < data.length && !inflater.finished()) { 
    215169                        try { 
    216                                 int vBytesRead = fStreamForMore.read(bytes); 
    217                                 inflater.setInput(bytes, 0, vBytesRead); 
     170                                int vBytesRead = pStreamForMoreData.read(pBytes); 
     171                                inflater.setInput(pBytes, 0, vBytesRead); 
    218172                                vRead += inflater.inflate(data, vRead, movieLength - vRead); 
    219                         } catch  (Exception e) { 
    220                                 // 
     173                        } catch (Exception e) { 
    221174                        } 
    222175                } 
    223176                inflater.end(); 
    224                  
     177 
    225178                return data; 
    226179        } 
     180         
     181 
     182        public String toString() { 
     183                StringBuffer vBuffer = new StringBuffer(); 
     184                vBuffer.append("Signature: ").append(getSignature()).append("\n"); 
     185                vBuffer.append("Version: ").append(getVersion()).append("\n"); 
     186                vBuffer.append("Frame: "); 
     187                vBuffer.append("(").append(getMinX()).append(",").append(getMinY()).append(") "); 
     188                vBuffer.append("(").append(getMaxX()).append(",").append(getMaxY()).append(")\n"); 
     189                vBuffer.append("Length: ").append(getLength() + " bytes").append("\n"); 
     190                vBuffer.append("FrameRate: ").append(getFrameRate() + " per second").append("\n"); 
     191                vBuffer.append("Number Of Frames: ").append(getNumberOfFrames()).append("\n"); 
     192                return vBuffer.toString(); 
     193        } 
    227194} 
Note: See TracChangeset for help on using the changeset viewer.