Changeset 9878 for trunk/Jav_EXT/com/scenari/ext/swf/SwfHeader.java
- Timestamp:
- 10/26/07 17:54:04 (4 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Jav_EXT/com/scenari/ext/swf/SwfHeader.java
r9867 r9878 1 1 package com.scenari.ext.swf; 2 2 3 /* 3 * FSHeader.java4 * Code inspiré de FSHeader.java pour la partie SWF : 4 5 * 5 6 * Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved. … … 28 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 */ 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.FileNotFoundException; 31 33 32 import java.io.IOException; 34 33 import java.io.InputStream; … … 36 35 import java.util.zip.Inflater; 37 36 37 public class SwfHeader extends FlashHeader { 38 38 39 public final class FSHeader {40 39 public static void main(String[] args) { 41 40 try { 42 FSHeader header = new FSHeader(args[0]);41 SwfHeader header = new SwfHeader(args[0]); 43 42 44 43 StringBuffer buffer = new StringBuffer(); 45 44 46 45 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"); 49 47 buffer.append("Version: ").append(header.getVersion()).append("\n"); 50 48 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"); 61 54 62 55 System.out.println(buffer.toString()); … … 66 59 } 67 60 68 pr ivate String signature = null;61 protected int fMinX = 0; 69 62 70 pr ivate int version= 0;63 protected int fMinY = 0; 71 64 72 pr ivate int minX = 0;65 protected int fMaxX = 0; 73 66 74 pr ivate int minY = 0;67 protected int fMaxY = 0; 75 68 76 pr ivate int maxX= 0;69 protected int fLength = 0; 77 70 78 pr ivate int maxY = 0;71 protected float fFrameRate = 0.0f; 79 72 80 pr ivate int length= 0;73 protected int fFrameCount = 0; 81 74 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 { 89 76 decodeFromFile(filename); 90 77 } 91 78 92 public FSHeader(byte[] pData) throws IOException, DataFormatException {93 decodeFromData(pData );79 public SwfHeader(byte[] pData) throws IOException, DataFormatException { 80 decodeFromData(pData, null); 94 81 } 95 82 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); 107 85 } 108 86 109 87 public int getLength() { 110 return length;88 return fLength; 111 89 } 112 90 113 91 public int getMinX() { 114 return minX;92 return fMinX; 115 93 } 116 94 117 95 public int getMaxX() { 118 return maxX;96 return fMaxX; 119 97 } 120 98 121 99 public int getMinY() { 122 return minY;100 return fMinY; 123 101 } 124 102 125 103 public int getMaxY() { 126 return maxY;104 return fMaxY; 127 105 } 128 106 129 107 public float getFrameRate() { 130 return f rameRate;108 return fFrameRate; 131 109 } 132 110 133 111 public float getNumberOfFrames() { 134 return f rameCount;112 return fFrameCount; 135 113 } 136 114 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; 140 117 141 File swfFile = new File(fileName);118 if (!isSwf(pBytes)) throw new DataFormatException("Data does not start with a valid Flash (SWF) signature."); 142 119 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 } 144 125 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); 147 129 148 fileContents.read(contents); 149 fileContents.close(); 130 vCoder.alignToByte(); 150 131 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); 152 143 } 153 144 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; 182 148 } 183 149 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 { 197 151 int movieLength = 0; 198 152 199 153 for (int i = 0; i < 4; i++) 200 movieLength += ( bytes[i + 4] & 0x000000FF) << (i * 8);154 movieLength += (pBytes[i + 4] & 0x000000FF) << (i * 8); 201 155 202 156 movieLength = Math.min(movieLength, 512); … … 206 160 207 161 for (int i = 0; i < 8; i++) 208 data[i] = bytes[i];162 data[i] = pBytes[i]; 209 163 210 164 int vRead = 0; 211 165 Inflater inflater = new Inflater(); 212 inflater.setInput( bytes, 8, bytes.length - 8);166 inflater.setInput(pBytes, 8, pBytes.length - 8); 213 167 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()) { 215 169 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); 218 172 vRead += inflater.inflate(data, vRead, movieLength - vRead); 219 } catch (Exception e) { 220 // 173 } catch (Exception e) { 221 174 } 222 175 } 223 176 inflater.end(); 224 177 225 178 return data; 226 179 } 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 } 227 194 }
Note: See TracChangeset
for help on using the changeset viewer.