Language: ChineseGermanSpanishFrenchDutchItalianRussian
123 Flash Chat Forums

Go Back   TOPCMM Community > SWFKit > SWFKit Support

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-2009, 04:22 AM
Junior Member
 
Join Date: Apr 2008
Posts: 29
Default ByteArray qustion

how to convert a StringStream Object to ByteArray?
please help me!!
Reply With Quote
  #2 (permalink)  
Old 05-17-2009, 10:57 PM
Senior Member
 
Join Date: Dec 2002
Posts: 1,992
Default

It's automatic, so you need not do anything. For instance, if an activex component has a method like get(ByteArray data), you just need to do as follows
Code:
var ss = new StringStream;
ss.put(1);
ss.put(2);
...

// ss will be converted to a ByteArray by swfkit automatically.
x.get(ss);
Reply With Quote
  #3 (permalink)  
Old 05-26-2009, 02:28 AM
Junior Member
 
Join Date: Jan 2009
Posts: 9
Default

How would you get the contents of a StringStream back into Actionscript?

Say you had an FFish function, is this supposed to work?:

function FFishTest()
{
return new StringStream();
}

If the caller of the FFishTest function was my actionscript, and I was casting that value to a ByteArray could I expect the data to be a ByteArray?

If not how would you do it? Assume the potential for multiple megabytes of data.
Reply With Quote
  #4 (permalink)  
Old 05-26-2009, 01:46 PM
Senior Member
 
Join Date: Dec 2002
Posts: 1,992
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
Reply

Was this information helpful?    Yes No



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:21 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.