|
|||
|
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); |
|
|||
|
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. |
|
|||
|
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");
Dynamic Flash » ActionScript 3.0 Base64 encoder/decoder |
![]() |
Was this information helpful? Yes No
| Thread Tools | |
| Display Modes | |
|
|