<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" viewSourceURL="srcview/index.html">

    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;
            
            private const UTF8_SAMPLE_STRING:String = "hoge foo bar";
            
            [Bindable]
            private var _readable:Boolean = false;
            
            private var _sample:ByteArray;
            private var _counter:uint = 0;
            
            private function writeSample():void
            {
                //clear log
                clearLog();
                
                _sample = new ByteArray();
                _sample.writeBoolean(true);
                _sample.writeInt(100);
                _sample.writeUTFBytes(UTF8_SAMPLE_STRING);
                
                var obj:Object = new Object();
                obj.hoge1 = "abc";
                obj.hoge2 = Math.round(Math.random()*10000);
                obj.hoge3 = false;
                obj.counter = _counter++;
                obj.date = new Date();
                
                _sample.writeObject(obj);
                
                _readable = true;
            }
            
            private function readSample():void
            {
                // clear offset
                _sample.position = 0;
                
                // clear log
                clearLog();
                
                var myData:Object = null;
                
                myData = _sample.readBoolean();
                addLog(myData);

                myData = _sample.readInt();
                addLog(myData);

                myData = _sample.readUTFBytes(UTF8_SAMPLE_STRING.length);
                addLog(myData);
                
                myData = _sample.readObject();
                addLog(myData);

            }
            
            private function addLog(data:Object):void
            {
                log.text += ObjectUtil.toString(data) + "\n";
            }
            
            private function clearLog():void
            {
                log.text = "";
            }
            
        ]]>
    </mx:Script>

    <mx:Button label="write sample data" click="writeSample()" />
    <mx:Button label="read sample data" enabled="{_readable}" click="readSample()" />
    
    <mx:TextArea id="log" width="300" height="200" />
    <mx:Button label="clear log" click="clearLog()" />
    
</mx:Application>