<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Comments on snippet: 'Action script to string serialization and de-serialization'</title>
<link>http://snipplr.com</link>
<description>Snipplr comments feed'</description>
<language>en-us</language>
<pubDate>Tue, 14 Feb 2012 03:33:50 GMT</pubDate>
<item>
<title>jschatz1 said on 11/5/10</title>
<link>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</link>
<description><![CDATA[ That got messed up but you get the idea. 
The drain grabs anything in the buffers which we don't have anything (but you might). Yeah that <code>be.encode(bytes.readUTFBytes(bytes.length));</code> is the part that really kills it. because it's not UTF you want.. You want the actual bytes.
And You really need <code>dec.toByteArray()</code> because you want a byteArray not a string.  You never even wanna deal with strings because as soon as you do, the bytes in your ByteArray is no longer accurate because they have been converted to something else.  And that's the whole point of Base64 anyway: to take what you can't read (the actual characters of the bytes (in 8 bits), with all sorts of none ASCII characters, and turn them into readable ASCII characters (6 bits) and slide the extra 2 bits over to the next byte.) ]]></description>
<pubDate>Fri, 05 Nov 2010 21:04:11 GMT</pubDate>
<guid>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</guid>
</item>
<item>
<title>jschatz1 said on 11/5/10</title>
<link>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</link>
<description><![CDATA[ Here is the correct code:
This eliminates the end of file error you were getting:
<code>
public static function serializeToString(value:Object):String
			{
				var bytes:ByteArray = new ByteArray();
				bytes.writeObject(value);
				
				bytes.position = 0;
				var be:Base64Encoder = new Base64Encoder();
				be.encodeBytes(bytes)
				return be.drain();
			}
			
			public static function readObjectFromStringBytes(value:String):Object
			{
				var dec:Base64Decoder = new Base64Decoder();
				dec.decode(value);
				var result:ByteArray = dec.toByteArray();
				result.position = 0;
				return result.readObject();
			}
</code> ]]></description>
<pubDate>Fri, 05 Nov 2010 20:51:08 GMT</pubDate>
<guid>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</guid>
</item>
<item>
<title>hopewise said on 2/10/10</title>
<link>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</link>
<description><![CDATA[ No matter what I do, I keep having this error:

RangeError: Error #2006: The supplied index is out of bounds. at flash.utils::ByteArray/readObject()

I am dispertly looking for a solution .. ]]></description>
<pubDate>Wed, 10 Feb 2010 01:13:02 GMT</pubDate>
<guid>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</guid>
</item>
<item>
<title>etfd said on 1/21/10</title>
<link>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</link>
<description><![CDATA[ Thanks for the post! Very useful! I did have to modify the code a bit for it to work however (I kept getting "end of file" errors when I got to the result.readObject() line.  Hope this is helpful to others...  -David

here's the code

	public static function serializeToString(value:Object):String{
			
			if(value==null){				
				throw new Error("null isn't a legal serialization candidate");				
			}
			
			var bytes:ByteArray = new ByteArray();			
			bytes.writeObject(value);			
			bytes.position = 0;			
			var be:Base64Encoder = new Base64Encoder();
			be.encodeBytes( bytes );			
			return be.toString();			
		}
		
		public static function readObjectFromStringBytes(value:String):Object{			
			
			var dec:Base64Decoder=new Base64Decoder();			
			dec.decode(value);			
			var result:ByteArray= dec.toByteArray();
			result.position=0;			
			return result.readObject();			
		} ]]></description>
<pubDate>Thu, 21 Jan 2010 15:07:26 GMT</pubDate>
<guid>http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/</guid>
</item>
</channel>
</rss>
