Last active
December 23, 2015 13:29
-
-
Save k0t0vich/6642358 to your computer and use it in GitHub Desktop.
StreamTarParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ru.k0t0vich.utils.net.tar { | |
| import flash.utils.IDataInput; | |
| [Event(name="fileParsed", type="ru.riot.utils.net.tar.TarParserEvent")] | |
| [Event(name="archiveParsed", type="ru.riot.utils.net.tar.TarParserEvent")] | |
| public class StreamTarParser extends TarParser { | |
| private static const BLOCK_SIZE: int = 512; | |
| private var _operation: Function; | |
| private var _needSize: uint; | |
| private var _currentFile: TarFile; | |
| public function StreamTarParser(bytes: IDataInput) { | |
| super(bytes); | |
| _needSize = BLOCK_SIZE; | |
| operation = headerOperation; | |
| } | |
| override public function read():void { | |
| if (_data.bytesAvailable >= _needSize) { | |
| _operation.call(); | |
| } | |
| } | |
| private function headerOperation():void { | |
| _currentFile = readFileHeader(); | |
| if (_currentFile){ | |
| // если не каталог или пустой файл, читаем контент | |
| if (_currentFile.size > 0) { | |
| _needSize = _currentFile.size; | |
| operation = contentOperation; | |
| } else { | |
| // иначе читаем следующий заголовок | |
| read(); | |
| } | |
| } else { | |
| _needSize = BLOCK_SIZE * 2 - 100; | |
| operation = endArchiveOperation; | |
| } | |
| } | |
| private function contentOperation():void { | |
| _data.readBytes(_currentFile.data, 0, _currentFile.size); | |
| var pad:uint = getPad(_currentFile.size); | |
| if (pad > 0 ) { | |
| _needSize = pad; | |
| operation = padOperation; | |
| } else { | |
| operation = endFileOperation; | |
| } | |
| } | |
| private function padOperation():void { | |
| skipBytes(_needSize); | |
| operation = endFileOperation; | |
| } | |
| private function endArchiveOperation():void { | |
| skipBytes(_needSize); | |
| var event:TarParserEvent = new TarParserEvent(TarParserEvent.ARCHIVE_PARSED); | |
| dispatchEvent(event); | |
| } | |
| private function endFileOperation():void { | |
| var event:TarParserEvent = new TarParserEvent(TarParserEvent.FILE_PARSED, _currentFile); | |
| dispatchEvent(event); | |
| _needSize = BLOCK_SIZE; | |
| operation = headerOperation; | |
| } | |
| private function set operation(value:Function):void { | |
| if( _operation !== value){ | |
| _operation = value; | |
| read(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment