KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > connection > SwfByte


1 /* ****************************************************************************
2  * SWFByte.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

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 JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.awt.geom.Rectangle2D JavaDoc;
21 import java.util.Vector JavaDoc;
22 import org.jdom.Element;
23
24 //**** Note: SWF files are stored little-endian (least significant bits first in order)
25

26 /** Helper class to send dribbled event information down to Flash client.
27  */

28 public class SwfByte
29 {
30     /** Default buffer size to set FlashBuffer */
31     static private final int DEFAULT_FLASH_BUFFER_SIZE = 100;
32
33     /** Default header length to send */
34     static public final int DEFAULT_HEADER_LENGTH = 65535;
35
36     /** Default header x-min */
37     static public final int DEFAULT_HEADER_X_MIN = 0;
38
39     /** Default header x-max */
40     static public final int DEFAULT_HEADER_X_MAX = 400;
41
42     /** Default header y-min */
43     static public final int DEFAULT_HEADER_Y_MIN = 0;
44
45     /** Default header y-max */
46     static public final int DEFAULT_HEADER_Y_MAX = 400;
47
48     /** Default header frame rate */
49     static public final double DEFAULT_HEADER_FRAME_RATE = 12.0;
50
51     /** Default header frame count */
52     static public final int DEFAULT_HEADER_FRAME_COUNT = 4200;
53
54     /** Flash buffer to send partial information */
55     private FlashBuffer mBuf = null;
56
57
58     private int mFlashVersion = 5;
59
60     /** Makes sure that a buffer has been created.
61      */

62     private void ensureBuffer()
63     {
64         if (mBuf == null)
65             mBuf = new FlashBuffer(DEFAULT_FLASH_BUFFER_SIZE);
66     }
67
68     /** Returns exact number of bytes written to buffer.
69      * @return buffer
70      */

71     public byte[] getBuf()
72     {
73         return getExactBytes(mBuf);
74     }
75
76     /** Sets the exact length of bytes written to header at moment this method
77      * is called.
78      */

79     public SwfByte setLength()
80     {
81         return this.setLength(mBuf.getPos());
82     }
83
84     /** Sets the specified length of bytes to header.
85      * @param length length to set
86      */

87     public SwfByte setLength(int length)
88     {
89         mBuf.writeDWordAt(length, 4);
90         return this;
91     }
92
93
94     /** Creates SWF header with default values. Length must be explicitly set by
95      * calling on the setLength() methods. If header has been set before this,
96      * it will be overridden.
97      */

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     /** Creates SWF header. Length must be explicitly set by calling on the
111      * setLength() methods. If header has been set before this,
112      * it will be overridden.
113      * @param version version number of SWF
114      * @param xMin x-min coordinates for frame size (in TWIPS)
115      * @param xMax x-max coordinates for frame size (in TWIPS)
116      * @param yMin x-min coordinates for frame size (in TWIPS)
117      * @param yMax y-max coordinates for frame size (in TWIPS)
118      * @param frameRate (not used)
119      * @param frameCount total number of frames in movie
120      */

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 JavaDoc rect = new Rectangle2D.Double JavaDoc(xMin, yMin, w, h);
131         return this.setHeader(version, rect, frameRate, frameCount);
132     }
133
134
135     /** Creates SWF header. Length must be explicitly set by calling on the
136      * setLength() methods. If header has been set before this,
137      * it will be overridden.
138      * @param version version number of SWF
139      * @param rect rectangular coordinates for frame size (in TWIPS)
140      * @param frameRate (not used)
141      * @param frameCount total number of frames in movie
142      */

143     public SwfByte setHeader(int version, Rectangle2D JavaDoc rect,
144                              double frameRate, int frameCount)
145     {
146         ensureBuffer();
147
148         // Signature - 1 byte for each letter
149
mBuf.writeByte('F');
150         mBuf.writeByte('W');
151         mBuf.writeByte('S');
152
153         // Version - 1 byte
154
mBuf.writeByte(version);
155
156         // Length of file - 4 bytes (skip for now)
157
mBuf.skip(4);
158
159         // Frame size - RECT
160
mBuf.write(rect);
161         
162         // Frame rate - 2 bytes (8.8 fixed) [made it 12.0]
163
// (make sure this fixed point value is written little-endian)
164
mBuf.writeByte(0);
165         mBuf.writeByte(12);
166
167         // Frame count - 2 bytes
168
mBuf.writeWord(frameCount);
169
170         return this;
171     }
172
173
174     /** Write byte-code to set a variable with a value.
175      * @param lVal variable to set
176      * @param rVal value to set variable with
177      */

178     public SwfByte actionSetVariable(String JavaDoc lVal, String JavaDoc 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         // Setting the size of prgBuf will to pos will ensure we copy the exact
189
// number of bytes
190
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     /** Uses DataCompiler to generate action bytecode based on XML. */
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     /** Write byte-code to show frame.
214      */

215     public SwfByte setShowFrame()
216     {
217         ensureBuffer();
218         mBuf.writeTag(Tag.SHOWFRAME, 0);
219         return this;
220     }
221     
222     /** Returns the actual amount written to the FlashBuffer, instead of
223      * returning the buffer itself. The buffer size is greater than or equal to
224      * the last position.
225      * @param buf flash buffer
226      * @return the actual bytes being stored in the buffer
227      */

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