⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 push.java

📁 java解析flash。对于flash在java上应用非常有帮助。比如解析flash到服务器端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * JSwiff is an open source Java API for Macromedia Flash file generation
 * and manipulation
 *
 * Copyright (C) 2004-2008 Ralf Terdic (contact@jswiff.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.jswiff.swfrecords.actions;

import com.jswiff.io.InputBitStream;
import com.jswiff.io.OutputBitStream;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * Pushes one or more values to the stack. Add <code>Push.StackValue</code>
 * instances using <code>addValue()</code>.
 * </p>
 * 
 * <p>
 * Performed stack operations: addition of one or more values to stack.
 * </p>
 * 
 * <p>
 * ActionScript equivalent: none (used internally, e.g. for parameter passing).
 * </p>
 *
 * @since SWF 4
 */
public final class Push extends Action {
  private List values = new ArrayList();

  /**
   * Creates a new Push action.
   */
  public Push() {
    code = ActionConstants.PUSH;
  }

  /*
   * Reads a Push action from a bit stream.
   */
  Push(InputBitStream stream) throws IOException {
    code = ActionConstants.PUSH;
    while (stream.available() > 0) {
      StackValue value = new StackValue(stream);
      values.add(value);
    }
  }

  /**
   * Returns the size of this action record in bytes.
   *
   * @return size of this record
   *
   * @see Action#getSize()
   */
  public int getSize() {
    int size = 3;
    for (int i = 0; i < values.size(); i++) {
      size += ((StackValue) values.get(i)).getSize();
    }
    return size;
  }

  /**
   * Returns a list of values this action is supposed to push to the stack. Use
   * this list in a read-only manner.
   *
   * @return a list of Push.StackValue instances
   */
  public List getValues() {
    return values;
  }

  /**
   * Adds a value to be pushed to the stack.
   *
   * @param value a <code>StackValue</code> instance
   */
  public void addValue(StackValue value) {
    values.add(value);
  }

  /**
   * Returns a short description of the action.
   *
   * @return <code>"Push"</code>
   */
  public String toString() {
    return "Push";
  }

  protected void writeData(
    OutputBitStream dataStream, OutputBitStream mainStream)
    throws IOException {
    for (int i = 0; i < values.size(); i++) {
      ((StackValue) values.get(i)).write(dataStream);
    }
  }

  /**
   * This class contains a value which can be pushed to the stack.  The default
   * value is <code>undefined</code>, the type is <code>TYPE_UNDEFINED</code>.
   * Use setters to change.
   */
  public static class StackValue implements Serializable {
    /** Indicates that the value to be pushed is a string. */
    public static final short TYPE_STRING      = 0;
    /** Indicates that the value to be pushed is a floating point number. */
    public static final short TYPE_FLOAT       = 1;
    /** Indicates that the value to be pushed is <code>null</code>. */
    public static final short TYPE_NULL        = 2;
    /** Indicates that the value to be pushed is <code>undefined</code>. */
    public static final short TYPE_UNDEFINED   = 3;
    /** Indicates that the value to be pushed is a register number. */
    public static final short TYPE_REGISTER    = 4;
    /** Indicates that the value to be pushed is a boolean. */
    public static final short TYPE_BOOLEAN     = 5;
    /**
     * Indicates that the value to be pushed is double-precision floating point
     * number.
     */
    public static final short TYPE_DOUBLE      = 6;
    /** Indicates that the value to be pushed is an integer. */
    public static final short TYPE_INTEGER     = 7;
    /** Indicates that the value to be pushed is an 8-bit constant pool index. */
    public static final short TYPE_CONSTANT_8  = 8;
    /** Indicates that the value to be pushed is a 16-bit constant pool index. */
    public static final short TYPE_CONSTANT_16 = 9;
    private short type                         = TYPE_UNDEFINED;
    private String string;
    private float floatValue;
    private short registerNumber;
    private boolean booleanValue;
    private double doubleValue;
    private long integerValue;
    private short constant8;
    private int constant16;

    /**
     * Creates a new StackValue instance. Initial type is
     * <code>TYPE_UNDEFINED</code>.
     */
    public StackValue() {
      // nothing to do
    }

    /*
     * Reads a PushEntry instance from a bit stream.
     */
    StackValue(InputBitStream stream) throws IOException {
      type = stream.readUI8();
      switch (type) {
        case TYPE_STRING:
          string = stream.readString();
          break;
        case TYPE_FLOAT:
          floatValue = stream.readFloat();
          break;
        case TYPE_REGISTER:
          registerNumber = stream.readUI8();
          break;
        case TYPE_BOOLEAN:
          booleanValue = (stream.readUI8() != 0);
          break;
        case TYPE_DOUBLE:
          doubleValue = stream.readDouble();
          break;
        case TYPE_INTEGER:
          integerValue = stream.readUI32();
          break;
        case TYPE_CONSTANT_8:
          constant8 = stream.readUI8();
          break;
        case TYPE_CONSTANT_16:
          constant16 = stream.readUI16();
          break;
      }
    }

    /**
     * Sets the push value to a boolean, and the type to TYPE_BOOLEAN.
     *
     * @param value a boolean value
     */
    public void setBoolean(boolean value) {
      this.booleanValue   = value;
      type                = TYPE_BOOLEAN;
    }

    /**
     * Returns the boolean the push value is set to. If the value type is not
     * TYPE_BOOLEAN, an IllegalStateException is thrown.
     *
     * @return push value as boolean
     */
    public boolean getBoolean() {
      return booleanValue;
    }

    /**
     * Sets the push value to a 16-bit constant pool index, and the type to
     * TYPE_BOOLEAN. Use 16-bit indexes when the constant pool contains more
     * than 256 constants.
     *
     * @param value an 8-bit constant pool index
     */
    public void setConstant16(int value) {
      this.constant16   = value;
      type              = TYPE_CONSTANT_16;
    }

    /**
     * Returns the 16-bit constant pool index the push value is set to. If the
     * value type is not TYPE_CONSTANT_16, an IllegalStateException is thrown.
     *
     * @return push value as 16-bit constant pool index
     *
     * @throws IllegalStateException if type is not TYPE_CONSTANT_16
     */
    public int getConstant16() {
      if (type != TYPE_CONSTANT_16) {
        throw new IllegalStateException("Value type is not TYPE_CONSTANT_16!");
      }
      return constant16;
    }

    /**
     * Sets the push value to an 8-bit constant pool index, and the type to
     * TYPE_BOOLEAN. Use 8-bit indexes when the constant pool contains less
     * than 256 constants.
     *
     * @param value an 8-bit constant pool index
     */
    public void setConstant8(short value) {
      this.constant8   = value;
      type             = TYPE_CONSTANT_8;
    }

    /**
     * Returns the 8-bit constant pool index the push value is set to. If the
     * value type is not TYPE_CONSTANT_8, an IllegalStateException is thrown.
     *
     * @return push value as 8-bit constant pool index
     *
     * @throws IllegalStateException if type is not TYPE_CONSTANT_8
     */
    public short getConstant8() {
      if (type != TYPE_CONSTANT_8) {
        throw new IllegalStateException("Value type is not TYPE_CONSTANT_8!");
      }
      return constant8;
    }

    /**
     * Sets the push value to a double-precision number, and the type to
     * TYPE_DOUBLE.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -