| 1 |
/* |
|---|
| 2 |
* ActionScript Thread Library |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the MIT License |
|---|
| 5 |
* |
|---|
| 6 |
* Copyright (c) 2008 BeInteractive! (www.be-interactive.org) and |
|---|
| 7 |
* Spark project (www.libspark.org) |
|---|
| 8 |
* |
|---|
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 10 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 11 |
* in the Software without restriction, including without limitation the rights |
|---|
| 12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 13 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 14 |
* furnished to do so, subject to the following conditions: |
|---|
| 15 |
* |
|---|
| 16 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 17 |
* all copies or substantial portions of the Software. |
|---|
| 18 |
* |
|---|
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 25 |
* THE SOFTWARE. |
|---|
| 26 |
* |
|---|
| 27 |
*/ |
|---|
| 28 |
package org.libspark.thread.threads.net |
|---|
| 29 |
{ |
|---|
| 30 |
import flash.errors.IOError; |
|---|
| 31 |
import flash.events.DataEvent; |
|---|
| 32 |
import flash.events.Event; |
|---|
| 33 |
import flash.events.IOErrorEvent; |
|---|
| 34 |
import flash.events.ProgressEvent; |
|---|
| 35 |
import flash.events.SecurityErrorEvent; |
|---|
| 36 |
import flash.net.FileReference; |
|---|
| 37 |
import flash.net.URLRequest; |
|---|
| 38 |
|
|---|
| 39 |
import org.libspark.thread.Thread; |
|---|
| 40 |
import org.libspark.thread.utils.IProgress; |
|---|
| 41 |
import org.libspark.thread.utils.IProgressNotifier; |
|---|
| 42 |
import org.libspark.thread.utils.Progress; |
|---|
| 43 |
|
|---|
| 44 |
/** |
|---|
| 45 |
* FileReference を用いてファイルをダウンロードするためのスレッドです. |
|---|
| 46 |
* |
|---|
| 47 |
* <p>このスレッドを開始すると、与えられた URLRequest を用いてダウンロード処理を開始し、 |
|---|
| 48 |
* ダウンロードが完了 (Event.COMPLETE) するとスレッドが終了します。</p> |
|---|
| 49 |
* |
|---|
| 50 |
* <p>join メソッドを用いると、簡単にダウンロード待ちをすることができます。</p> |
|---|
| 51 |
* |
|---|
| 52 |
* <p>ロード中にエラーが発生した場合は、以下の例外がスローされます。 |
|---|
| 53 |
* これらの例外は、このスレッドを開始したスレッド(親スレッド)で捕捉する事が出来ます。</p> |
|---|
| 54 |
* |
|---|
| 55 |
* <ul> |
|---|
| 56 |
* <li>flash.events.IOErrorEvent.IO_ERROR: flash.errors.IOError</li> |
|---|
| 57 |
* <li>flash.events.SecurityErrorEvent.SECURITY_ERROR: SecurityError</li> |
|---|
| 58 |
* </ul> |
|---|
| 59 |
* |
|---|
| 60 |
* @author seagirl |
|---|
| 61 |
* @author yossy:beinteractive |
|---|
| 62 |
*/ |
|---|
| 63 |
public class FileDownloadThread extends Thread implements IProgressNotifier |
|---|
| 64 |
{ |
|---|
| 65 |
/** |
|---|
| 66 |
* 新しい FileDownloadThread クラスのインスタンスを生成します. |
|---|
| 67 |
* |
|---|
| 68 |
* @param request ダウンロード対象となる URLRequest |
|---|
| 69 |
* @param fileReference ダウンロードに使用する FileReference 。省略もしくは null の場合、新たに作成した FileReference を使用します |
|---|
| 70 |
* @param defaultFileName アップロードの POST に使用するフィールド名。詳しくは FileReference.upload を参照してください |
|---|
| 71 |
*/ |
|---|
| 72 |
public function FileDownloadThread(request:URLRequest, fileReference:FileReference = null, defaultFileName:String = null) |
|---|
| 73 |
{ |
|---|
| 74 |
_request = request; |
|---|
| 75 |
_fileReference = fileReference != null ? fileReference : new FileReference(); |
|---|
| 76 |
_defaultFileName = defaultFileName; |
|---|
| 77 |
_progress = new Progress(); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
private var _request:URLRequest; |
|---|
| 81 |
private var _fileReference:FileReference; |
|---|
| 82 |
private var _defaultFileName:String; |
|---|
| 83 |
private var _progress:Progress; |
|---|
| 84 |
|
|---|
| 85 |
/** |
|---|
| 86 |
* ロード対象となる URLRequest を返します. |
|---|
| 87 |
*/ |
|---|
| 88 |
public function get request():URLRequest |
|---|
| 89 |
{ |
|---|
| 90 |
return _request; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
/** |
|---|
| 94 |
* ロードに使用する FileReference を返します. |
|---|
| 95 |
*/ |
|---|
| 96 |
public function get fileReference():FileReference |
|---|
| 97 |
{ |
|---|
| 98 |
return _fileReference; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
/** |
|---|
| 102 |
* ダウンロードするファイルとしてダイアログボックスに表示するデフォルトファイル名。詳しくは FileReference.download を参照してください. |
|---|
| 103 |
*/ |
|---|
| 104 |
public function get defaultFileName():String |
|---|
| 105 |
{ |
|---|
| 106 |
return _defaultFileName; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
/** |
|---|
| 110 |
* @private |
|---|
| 111 |
*/ |
|---|
| 112 |
public function set defaultFileName(value:String):void |
|---|
| 113 |
{ |
|---|
| 114 |
_defaultFileName = defaultFileName; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
/** |
|---|
| 118 |
* @inheritDoc |
|---|
| 119 |
*/ |
|---|
| 120 |
public function get progress():IProgress |
|---|
| 121 |
{ |
|---|
| 122 |
return _progress; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
/** |
|---|
| 126 |
* アップロード処理をキャンセルします. |
|---|
| 127 |
*/ |
|---|
| 128 |
public function cancel():void |
|---|
| 129 |
{ |
|---|
| 130 |
// 割り込みをかける |
|---|
| 131 |
interrupt(); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
/** |
|---|
| 135 |
* 実行 |
|---|
| 136 |
* |
|---|
| 137 |
* @private |
|---|
| 138 |
*/ |
|---|
| 139 |
override protected function run():void |
|---|
| 140 |
{ |
|---|
| 141 |
// イベントハンドラを設定 |
|---|
| 142 |
// Note: イベントハンドラを設定した場合、自動的に wait がかかる |
|---|
| 143 |
events(); |
|---|
| 144 |
|
|---|
| 145 |
// 割り込みハンドラを設定 |
|---|
| 146 |
interrupted(interruptedHandler); |
|---|
| 147 |
|
|---|
| 148 |
// ダウンロード開始 |
|---|
| 149 |
fileReference.download(request, defaultFileName); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
/** |
|---|
| 153 |
* イベントハンドラの登録 |
|---|
| 154 |
* |
|---|
| 155 |
* @private |
|---|
| 156 |
*/ |
|---|
| 157 |
private function events():void |
|---|
| 158 |
{ |
|---|
| 159 |
event(fileReference, Event.COMPLETE, completeHandler); |
|---|
| 160 |
event(fileReference, ProgressEvent.PROGRESS, progressHandler); |
|---|
| 161 |
event(fileReference, IOErrorEvent.IO_ERROR, ioErrorHandler); |
|---|
| 162 |
event(fileReference, SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
/** |
|---|
| 166 |
* まだ開始を通知していなければ通知する |
|---|
| 167 |
* |
|---|
| 168 |
* @private |
|---|
| 169 |
*/ |
|---|
| 170 |
private function notifyStartIfNeeded(total:Number):void |
|---|
| 171 |
{ |
|---|
| 172 |
if (!_progress.isStarted) { |
|---|
| 173 |
_progress.start(total); |
|---|
| 174 |
} |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
/** |
|---|
| 178 |
* ProgressEvent.PROGRESS ハンドラ |
|---|
| 179 |
* |
|---|
| 180 |
* @private |
|---|
| 181 |
*/ |
|---|
| 182 |
private function progressHandler(e:ProgressEvent):void |
|---|
| 183 |
{ |
|---|
| 184 |
trace(e); |
|---|
| 185 |
|
|---|
| 186 |
// 必要であれば開始を通知 |
|---|
| 187 |
notifyStartIfNeeded(e.bytesTotal); |
|---|
| 188 |
|
|---|
| 189 |
// 進捗を通知 |
|---|
| 190 |
_progress.progress(e.bytesLoaded); |
|---|
| 191 |
|
|---|
| 192 |
// 割り込みハンドラを設定 |
|---|
| 193 |
interrupted(interruptedHandler); |
|---|
| 194 |
|
|---|
| 195 |
// 再びイベント待ち |
|---|
| 196 |
events(); |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
/** |
|---|
| 200 |
* Event.COMPLETE ハンドラ |
|---|
| 201 |
* |
|---|
| 202 |
* @private |
|---|
| 203 |
*/ |
|---|
| 204 |
private function completeHandler(e:Event):void |
|---|
| 205 |
{ |
|---|
| 206 |
// 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される) |
|---|
| 207 |
notifyStartIfNeeded(0); |
|---|
| 208 |
|
|---|
| 209 |
// 完了を通知 |
|---|
| 210 |
_progress.complete(); |
|---|
| 211 |
|
|---|
| 212 |
// ここでスレッド終了 |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
/** |
|---|
| 216 |
* IOErrorEvent.IO_ERROR ハンドラ |
|---|
| 217 |
* |
|---|
| 218 |
* @private |
|---|
| 219 |
*/ |
|---|
| 220 |
private function ioErrorHandler(e:IOErrorEvent):void |
|---|
| 221 |
{ |
|---|
| 222 |
// 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される) |
|---|
| 223 |
notifyStartIfNeeded(0); |
|---|
| 224 |
|
|---|
| 225 |
// 失敗を通知 |
|---|
| 226 |
_progress.fail(); |
|---|
| 227 |
|
|---|
| 228 |
// IOError をスロー |
|---|
| 229 |
throw new IOError(e.text); |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
/** |
|---|
| 233 |
* SecurityErrorEvent.SECURITY_ERROR ハンドラ |
|---|
| 234 |
* |
|---|
| 235 |
* @private |
|---|
| 236 |
*/ |
|---|
| 237 |
private function securityErrorHandler(e:SecurityErrorEvent):void |
|---|
| 238 |
{ |
|---|
| 239 |
// 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される) |
|---|
| 240 |
notifyStartIfNeeded(0); |
|---|
| 241 |
|
|---|
| 242 |
// 失敗を通知 |
|---|
| 243 |
_progress.fail(); |
|---|
| 244 |
|
|---|
| 245 |
// SecurityError をスロー |
|---|
| 246 |
throw new SecurityError(e.text); |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
/** |
|---|
| 250 |
* 割り込みハンドラ |
|---|
| 251 |
* |
|---|
| 252 |
* @private |
|---|
| 253 |
*/ |
|---|
| 254 |
private function interruptedHandler():void |
|---|
| 255 |
{ |
|---|
| 256 |
// 必要であれば開始を通知 (問題が発生しなければ通常 progressHandler で通知される) |
|---|
| 257 |
notifyStartIfNeeded(0); |
|---|
| 258 |
|
|---|
| 259 |
// アップロードをキャンセル |
|---|
| 260 |
fileReference.cancel(); |
|---|
| 261 |
|
|---|
| 262 |
// キャンセルを通知 |
|---|
| 263 |
_progress.cancel(); |
|---|
| 264 |
} |
|---|
| 265 |
} |
|---|
| 266 |
} |
|---|