1 4 5 9 10 package org.openlaszlo.connection; 11 12 import org.openlaszlo.iv.flash.api.action.Actions; 13 import org.openlaszlo.iv.flash.api.action.Program; 14 import org.openlaszlo.iv.flash.util.FlashBuffer; 15 import org.openlaszlo.iv.flash.util.Tag; 16 import org.openlaszlo.xml.internal.DataCompiler; 17 import java.io.FileNotFoundException ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.awt.geom.Rectangle2D ; 21 import java.util.Vector ; 22 import org.jdom.Element; 23 24 26 28 public class SwfByte 29 { 30 31 static private final int DEFAULT_FLASH_BUFFER_SIZE = 100; 32 33 34 static public final int DEFAULT_HEADER_LENGTH = 65535; 35 36 37 static public final int DEFAULT_HEADER_X_MIN = 0; 38 39 40 static public final int DEFAULT_HEADER_X_MAX = 400; 41 42 43 static public final int DEFAULT_HEADER_Y_MIN = 0; 44 45 46 static public final int DEFAULT_HEADER_Y_MAX = 400; 47 48 49 static public final double DEFAULT_HEADER_FRAME_RATE = 12.0; 50 51 52 static public final int DEFAULT_HEADER_FRAME_COUNT = 4200; 53 54 55 private FlashBuffer mBuf = null; 56 57 58 private int mFlashVersion = 5; 59 60 62 private void ensureBuffer() 63 { 64 if (mBuf == null) 65 mBuf = new FlashBuffer(DEFAULT_FLASH_BUFFER_SIZE); 66 } 67 68 71 public byte[] getBuf() 72 { 73 return getExactBytes(mBuf); 74 } 75 76 79 public SwfByte setLength() 80 { 81 return this.setLength(mBuf.getPos()); 82 } 83 84 87 public SwfByte setLength(int length) 88 { 89 mBuf.writeDWordAt(length, 4); 90 return this; 91 } 92 93 94 98 public SwfByte setHeader(int version) 99 { 100 ensureBuffer(); 101 102 return this.setHeader(version, 103 DEFAULT_HEADER_X_MIN, DEFAULT_HEADER_X_MAX, 104 DEFAULT_HEADER_Y_MIN, DEFAULT_HEADER_Y_MAX, 105 DEFAULT_HEADER_FRAME_RATE, DEFAULT_HEADER_FRAME_COUNT); 106 } 107 108 109 110 121 public SwfByte setHeader(int version, 122 int xMin, int xMax, 123 int yMin, int yMax, 124 double frameRate, int frameCount) 125 { 126 ensureBuffer(); 127 128 int w = xMax - xMin; 129 int h = yMax - yMin; 130 Rectangle2D rect = new Rectangle2D.Double (xMin, yMin, w, h); 131 return this.setHeader(version, rect, frameRate, frameCount); 132 } 133 134 135 143 public SwfByte setHeader(int version, Rectangle2D rect, 144 double frameRate, int frameCount) 145 { 146 ensureBuffer(); 147 148 mBuf.writeByte('F'); 150 mBuf.writeByte('W'); 151 mBuf.writeByte('S'); 152 153 mBuf.writeByte(version); 155 156 mBuf.skip(4); 158 159 mBuf.write(rect); 161 162 mBuf.writeByte(0); 165 mBuf.writeByte(12); 166 167 mBuf.writeWord(frameCount); 169 170 return this; 171 } 172 173 174 178 public SwfByte actionSetVariable(String lVal, String rVal) 179 { 180 ensureBuffer(); 181 182 Program prg = new Program(); 183 prg.push(lVal); 184 prg.push(rVal); 185 prg.setVar(); 186 prg.none(); 187 188 FlashBuffer prgBuf = prg.body(); 191 prgBuf.setSize(prgBuf.getPos()); 192 193 mBuf.writeTag(Tag.DOACTION, prgBuf.getSize()); 194 mBuf.writeFOB(prgBuf); 195 return this; 196 } 197 198 199 200 public SwfByte actionSetElement(Element el) 201 { 202 ensureBuffer(); 203 Program prg = DataCompiler.makeProgram(el, mFlashVersion); 204 prg.none(); 205 FlashBuffer prgBuf = prg.body(); 206 mBuf.writeTag(Tag.DOACTION, prgBuf.getSize()); 207 mBuf.writeFOB(prgBuf); 208 return this; 209 } 210 211 212 213 215 public SwfByte setShowFrame() 216 { 217 ensureBuffer(); 218 mBuf.writeTag(Tag.SHOWFRAME, 0); 219 return this; 220 } 221 222 228 static public byte[] getExactBytes(FlashBuffer buf) 229 { 230 int len = buf.getPos(); 231 byte[] oldBuf = buf.getBuf(); 232 byte[] newBuf = new byte[len]; 233 System.arraycopy(oldBuf, 0, newBuf, 0, len); 234 return newBuf; 235 } 236 } 237 | Popular Tags |