| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { | 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { |
| 6 _FileInputStream(String name) | 6 _FileInputStream(String name) |
| 7 : _data = [], | 7 : _data = [], |
| 8 _position = 0, | 8 _position = 0, |
| 9 _filePosition = 0 { | 9 _filePosition = 0 { |
| 10 var file = new File(name); | 10 var file = new File(name); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 _fileLength = len; | 30 _fileLength = len; |
| 31 return _fillBuffer(); | 31 return _fillBuffer(); |
| 32 }); | 32 }); |
| 33 chained.handleException((e) { | 33 chained.handleException((e) { |
| 34 _reportError(e); | 34 _reportError(e); |
| 35 return true; | 35 return true; |
| 36 }); | 36 }); |
| 37 chained.then((ignored) => _checkScheduleCallbacks()); | 37 chained.then((ignored) => _checkScheduleCallbacks()); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void _closeFile() { |
| 41 if (!_openedFile.closed) { |
| 42 _openedFile.close().then((ignore) { |
| 43 _streamMarkedClosed = true; |
| 44 _checkScheduleCallbacks(); |
| 45 }); |
| 46 } |
| 47 } |
| 48 |
| 40 Future<int> _fillBuffer() { | 49 Future<int> _fillBuffer() { |
| 41 Expect.equals(_position, _data.length); | 50 Expect.equals(_position, _data.length); |
| 42 | 51 |
| 43 int size = Math.min(_bufferLength, _fileLength - _filePosition); | 52 int size = Math.min(_bufferLength, _fileLength - _filePosition); |
| 44 if (size == 0) { | 53 if (size == 0) { |
| 45 _streamMarkedClosed = true; | 54 _closeFile(); |
| 46 _openedFile.close(); | |
| 47 return new Future.immediate(0); | 55 return new Future.immediate(0); |
| 48 } | 56 } |
| 49 if (_data.length != size) { | 57 if (_data.length != size) { |
| 50 _data = new Uint8List(size); | 58 _data = new Uint8List(size); |
| 51 } | 59 } |
| 52 var future = _openedFile.readList(_data, 0, _data.length); | 60 var future = _openedFile.readList(_data, 0, _data.length); |
| 53 future = future.transform((read) { | 61 future = future.transform((read) { |
| 54 _filePosition += read; | 62 _filePosition += read; |
| 55 if (read != _data.length) { | 63 if (read != _data.length) { |
| 56 _data.removeRange(read, _data.length - read); | 64 _data.removeRange(read, _data.length - read); |
| 57 } | 65 } |
| 58 _position = 0; | 66 _position = 0; |
| 59 | 67 |
| 60 if (_fileLength == _filePosition) { | 68 if (_fileLength == _filePosition) { |
| 61 _streamMarkedClosed = true; | 69 _closeFile(); |
| 62 _openedFile.close(); | |
| 63 } | 70 } |
| 64 return read; | 71 return read; |
| 65 }); | 72 }); |
| 66 return future; | 73 return future; |
| 67 } | 74 } |
| 68 | 75 |
| 69 int available() { | 76 int available() { |
| 70 return closed ? 0 : _data.length - _position; | 77 return closed ? 0 : _data.length - _position; |
| 71 } | 78 } |
| 72 | 79 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 99 } | 106 } |
| 100 | 107 |
| 101 int _readInto(List<int> buffer, int offset, int len) { | 108 int _readInto(List<int> buffer, int offset, int len) { |
| 102 buffer.setRange(offset, len, _data, _position); | 109 buffer.setRange(offset, len, _data, _position); |
| 103 _position += len; | 110 _position += len; |
| 104 _finishRead(); | 111 _finishRead(); |
| 105 return len; | 112 return len; |
| 106 } | 113 } |
| 107 | 114 |
| 108 void _close() { | 115 void _close() { |
| 109 _streamMarkedClosed = true; | |
| 110 _data = []; | 116 _data = []; |
| 111 _position = 0; | 117 _position = 0; |
| 112 if (!_openedFile.closed) { | 118 _filePosition = _fileLength; |
| 113 _openedFile.close(); | 119 _closeFile(); |
| 114 } | |
| 115 } | 120 } |
| 116 | 121 |
| 117 static final int _bufferLength = 64 * 1024; | 122 static final int _bufferLength = 64 * 1024; |
| 118 | 123 |
| 119 RandomAccessFile _openedFile; | 124 RandomAccessFile _openedFile; |
| 120 List<int> _data; | 125 List<int> _data; |
| 121 int _position; | 126 int _position; |
| 122 int _filePosition; | 127 int _filePosition; |
| 123 int _fileLength; | 128 int _fileLength; |
| 124 } | 129 } |
| (...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 new FileIOException("File closed '$_name'")); | 1104 new FileIOException("File closed '$_name'")); |
| 1100 }); | 1105 }); |
| 1101 return completer.future; | 1106 return completer.future; |
| 1102 } | 1107 } |
| 1103 | 1108 |
| 1104 final String _name; | 1109 final String _name; |
| 1105 int _id; | 1110 int _id; |
| 1106 | 1111 |
| 1107 SendPort _fileService; | 1112 SendPort _fileService; |
| 1108 } | 1113 } |
| OLD | NEW |