View Single Post
  #4 (permalink)  
Old 05-26-2009, 01:46 PM
SWFKit SWFKit is offline
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default

I thought it was the byte array of ActiveX components. Sorry for the misunderstanding. There isn't directly way of converting ffishscript StringStream to actionscript ByteArray.

A possible method is to first encode the binary data with base64 to a string and then decode it when it has been transferred between different scripting languages.

The following is the base64 code for ffish script
Code:
function Base64()
{
	this.keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

	/**
	 * Read a byte from the input string or file stream
	 */
	function readByte(input)
	{
		var ch = input.get();
		if (input.eof) return NaN;
		
		return ch;
	}

	/**
	 * Encode the input string or file stream, returns a stringstream
	 */
   	function encode(input) 
   	{
   		var output = new StringStream;
    	var chr1, chr2, chr3 ;
    	var enc1, enc2, enc3, enc4;

    	do 
    	{
       		chr1 = this.readByte(input);
        	chr2 = this.readByte(input);
        	chr3 = this.readByte(input);

        	enc1 = chr1 >> 2;
        	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) 
			{
				enc3 = enc4 = 64;
			} 
			else if (isNaN(chr3)) 
			{
				enc4 = 64;
			}

			output.write(this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + 
            	this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4));
            	
      	} while (!input.eof);

      	return output;
   	}

	/**
	 * decode a base64 encoded string, returns a string stream
	 */
   	function decode(input) 
   	{
      	var output = new StringStream;
      	var chr1, chr2, chr3;
      	var enc1, enc2, enc3, enc4;
      	var i = 0;

      	// have all characters that are not A-Z, a-z, 0-9, +, /, or =
      	var base64test = new RegExp("[^A-Za-z0-9+/=]", "g");
      	if (base64test.exec(input)) 
      	{
      		trace("Base64.decode: ", "invalid input string");
      		return null;
      	}     

      	do 
      	{
         	enc1 = this.keyStr.indexOf(input.charAt(i++));
         	enc2 = this.keyStr.indexOf(input.charAt(i++));
         	enc3 = this.keyStr.indexOf(input.charAt(i++));
         	enc4 = this.keyStr.indexOf(input.charAt(i++));

         	chr1 = (enc1 << 2) | (enc2 >> 4);
         	chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
         	chr3 = ((enc3 & 3) << 6) | enc4;

         	output.put(String.fromCharCode(chr1));

         	if (enc3 != 64) 
         	{
            	output.put(String.fromCharCode(chr2));
         	}
         	
         	if (enc4 != 64) 
         	{
            	output.put(String.fromCharCode(chr3));
         	}

      	} while (i < input.length);

   		return output;
   }
}

var converter = new Base64();
//var oldStream = StringStream.readFromFile("c:\\image001.JPG");
var out = converter.encode(new FileStream("c:\\image001.jpg"));//oldStream);
var str = out.toString();
//trace(str);
var newStream = converter.decode(str);
newStream.saveToFile("c:\\hello.jpg");
For AS3, you can check the following web site:
Dynamic Flash » ActionScript 3.0 Base64 encoder/decoder
Reply With Quote