root/as3/Utils/src/org/libspark/utils/FileUtil.as

リビジョン 804, 3.9 kB (コミッタ: michi, コミット時期: 2 年 前)

asdoc追加

  • svn:executable 属性の設定値: *
Line 
1 /**
2  * The File Utility Class for ActionScript 3.0
3  *
4  * @author      Copyright (c) 2008 daoki2
5  * @version     1.0.0
6  * @link        http://snippets.libspark.org/
7  * @link        http://homepage.mac.com/daoki2/
8  *
9  * Copyright (c) 2008 daoki2
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  */
29
30 package org.libspark.utils {
31     import flash.errors.IllegalOperationError;
32     import flash.filesystem.File;
33     import flash.filesystem.FileStream;
34        
35         /**
36          * ファイル操作を行うためのユーティリティクラスです <span style="color:#FF0000; font-weight:bold;">(AIR Only)</span>
37          */
38     public class FileUtil {
39
40        /**
41         * Constructor
42                 * @private
43         */
44         public function FileUtil() {
45             throw new IllegalOperationError("SqlUtil class can not create instance");
46         }
47
48        /**
49         * Get the line count of the file
50         * @param        filestream      The filestream to count
51         * @return                       The line count of the specified file
52         */
53         public static function getLineCount(filestream:FileStream):Number {
54             var result:Number = 0;
55             var skip:Number;
56             while(filestream.bytesAvailable > 0) {
57                 skip = getLineEnd(filestream);
58                 result++;
59             }
60             filestream.position = 0;
61             return result;
62         }
63
64        /**
65         * Read line from the file
66         * @param        filestream      The filestream to read
67         * @return                       The line of the specified file
68         */
69         public static function readln(filestream:FileStream):String {
70             var result:String = new String();
71             var startpos:Number = filestream.position;
72             var skip:Number = getLineEnd(filestream);
73             var len:Number = filestream.position - startpos - skip;
74             filestream.position = startpos;
75             result = filestream.readUTFBytes(len);
76             filestream.position += skip;
77             return result;
78         }
79
80        /**
81         * private method
82         */
83
84        /**
85         * Move the file pointer to the end of the line
86         */
87         private static function getLineEnd(filestream:FileStream):Number {
88             var skip:Number = 0;
89             var code:int;
90             while(filestream.bytesAvailable > 0) {
91                 code = filestream.readByte();
92                 if (code == 0x0A) {
93                     skip = 1;
94                     break;
95                 }
96                 if (code == 0x0D) {
97                     skip = 1;
98                     if (filestream.bytesAvailable > 0) {
99                         code = filestream.readByte();
100                         if (code != 0x0A) {
101                             filestream.position -= 1;
102                         } else {
103                             skip = 2;
104                         }
105                         break;
106                     } else
107                         break;
108                 }
109             }
110             return skip;
111         }
112     }
113 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。