Flex to read binary file

Flex to read binary file

Flex can be used to read binary file class has four: ByteArray, FileStream, Socket, URLStream. They all realized IdataInput interfaces, which are located flash.utils, flash.filesystem, flash.net, flash.net package. Be noted that, FileStream only applies AIR project.

Details of the four classes and the use of methods, please refer to the official Adobe documentation: ActionScript3.0 Language and Components Reference: http://www.adobe.com/livedocs/flex/3_cn/ .

URLStream read with the attached format ESRI's shapfile example, this sample is based on Adobe Flash Builder4 development environment for ActionScript projects.

package
{
        import flash.display.Sprite;
        import flash.errors.IOError;
        import flash.events.*;
        import flash.net.URLRequest;
        import flash.net.URLStream;
        import flash.utils.Endian;

        public class ShapeReader extends Sprite
        {
                private var _stream:URLStream;
                public function ShapeReader()
                {
                        this._stream = new URLStream();
                        this.readfile("http://localhost/data/regionprov_Clip.shp");
                }

                private function readfile(urlPath:String):void
                {
                        var request:URLRequest = new URLRequest(urlPath);
                        this._stream.addEventListener(Event.COMPLETE,onComplete);

                        try
                        {
                                this._stream.load(request);
                        }
                        catch (error:Error)
                        {
                                trace("Unable to load requested URL.");
                        }
                }

                private function onComplete(event:Event):void
                {
                        try
                        {
                                //.........................Begin of FileHeader
                                var code:int = this._stream.readInt();//File Code
                                this._stream.readUTFBytes(5*4);//skip int bytes in number of 5
                                var length:int = this._stream.readInt();//File Length
                                this._stream.endian = Endian.LITTLE_ENDIAN;//change byte order
                                var version:int = this._stream.readInt();//Version
                                var type:int = this._stream.readInt();//Shape Type
                                var xMin:Number = this._stream.readDouble();//Xmin
                                var yMin:Number = this._stream.readDouble();//Ymin
                                var xMax:Number = this._stream.readDouble();//Xmax
                                var yMax:Number = this._stream.readDouble();//Ymax
                                this._stream.readUTFBytes(4*8);// skip double bytes in number of 4
                                //.........................End of FileHeader
                                this._stream.close();// close stream
                        }
                        catch(error:IOError)
                        {
                                trace(error.toString());
                        }
                }
        }
}

LookAtYou 2010.8.25

分类:Flash 时间:2010-08-25 人气:226
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.426 (s). 12 q(s)