Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 } | 58 } |
| 59 | 59 |
| 60 void _fillBuffer() { | 60 void _fillBuffer() { |
| 61 Expect.equals(_position, _data.length); | 61 Expect.equals(_position, _data.length); |
| 62 if (_openedFile == null) return; // Called before the file is opened. | 62 if (_openedFile == null) return; // Called before the file is opened. |
| 63 int size = Math.min(_bufferLength, _fileLength - _filePosition); | 63 int size = Math.min(_bufferLength, _fileLength - _filePosition); |
| 64 if (size == 0) { | 64 if (size == 0) { |
| 65 _closeFile(); | 65 _closeFile(); |
| 66 return; | 66 return; |
| 67 } | 67 } |
| 68 if (_activeFillBufferCall) return; | |
|
Ivan Posva
2012/06/12 15:56:52
Comment about buffer filling in flight please.
| |
| 69 _activeFillBufferCall = true; | |
| 68 if (_data.length != size) { | 70 if (_data.length != size) { |
| 69 _data = new Uint8List(size); | 71 _data = new Uint8List(size); |
| 72 _position = _data.length; | |
|
Bill Hesse
2012/06/12 15:18:10
This was probably the cause of a bug. It could ca
Ivan Posva
2012/06/12 15:56:52
This is very confusing. Please add a comment expla
| |
| 70 } | 73 } |
| 71 var future = _openedFile.readList(_data, 0, _data.length); | 74 var future = _openedFile.readList(_data, 0, _data.length); |
| 72 future.then((read) { | 75 future.then((read) { |
| 73 _filePosition += read; | 76 _filePosition += read; |
| 74 if (read != _data.length) { | 77 if (read != _data.length) { |
| 75 _data = _data.getRange(0, read); | 78 _data = _data.getRange(0, read); |
| 76 } | 79 } |
| 77 _position = 0; | 80 _position = 0; |
| 81 _activeFillBufferCall = false; | |
| 78 | 82 |
| 79 if (_fileLength == _filePosition) { | 83 if (_fileLength == _filePosition) { |
| 80 _closeFile(); | 84 _closeFile(); |
| 81 } | 85 } |
| 82 _checkScheduleCallbacks(); | 86 _checkScheduleCallbacks(); |
| 83 }); | 87 }); |
| 88 future.handleException((e) { | |
| 89 _activeFillBufferCall = false; | |
| 90 _reportError(e); | |
| 91 return true; | |
| 92 }); | |
| 84 } | 93 } |
| 85 | 94 |
| 86 int available() { | 95 int available() { |
| 87 return closed ? 0 : _data.length - _position; | 96 return closed ? 0 : _data.length - _position; |
| 88 } | 97 } |
| 89 | 98 |
| 90 void pipe(OutputStream output, [bool close = true]) { | 99 void pipe(OutputStream output, [bool close = true]) { |
| 91 _pipe(this, output, close: close); | 100 _pipe(this, output, close: close); |
| 92 } | 101 } |
| 93 | 102 |
| 94 void _finishRead() { | 103 void _finishRead() { |
| 95 if (_position == _data.length && !_streamMarkedClosed) { | 104 if (_position == _data.length && !_streamMarkedClosed) { |
| 96 _fillBuffer(); | 105 _fillBuffer(); |
| 97 } else { | 106 } else { |
| 98 _checkScheduleCallbacks(); | 107 _checkScheduleCallbacks(); |
| 99 } | 108 } |
| 100 } | 109 } |
| 101 | 110 |
| 102 List<int> _read(int bytesToRead) { | 111 List<int> _read(int bytesToRead) { |
| 103 List<int> result; | 112 List<int> result; |
| 104 if (_position == 0 && bytesToRead == _data.length) { | 113 if (_position == 0 && bytesToRead == _data.length) { |
| 105 result = _data; | 114 result = _data; |
| 106 _data = []; | 115 _data = []; |
|
Ivan Posva
2012/06/12 15:56:52
_data = const [];
| |
| 107 } else { | 116 } else { |
| 108 result = new Uint8List(bytesToRead); | 117 result = new Uint8List(bytesToRead); |
| 109 result.setRange(0, bytesToRead, _data, _position); | 118 result.setRange(0, bytesToRead, _data, _position); |
| 110 _position += bytesToRead; | 119 _position += bytesToRead; |
| 111 } | 120 } |
| 112 _finishRead(); | 121 _finishRead(); |
| 113 return result; | 122 return result; |
| 114 } | 123 } |
| 115 | 124 |
| 116 int _readInto(List<int> buffer, int offset, int len) { | 125 int _readInto(List<int> buffer, int offset, int len) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 128 _closeFile(); | 137 _closeFile(); |
| 129 } | 138 } |
| 130 | 139 |
| 131 static final int _bufferLength = 64 * 1024; | 140 static final int _bufferLength = 64 * 1024; |
| 132 | 141 |
| 133 RandomAccessFile _openedFile; | 142 RandomAccessFile _openedFile; |
| 134 List<int> _data; | 143 List<int> _data; |
| 135 int _position; | 144 int _position; |
| 136 int _filePosition; | 145 int _filePosition; |
| 137 int _fileLength; | 146 int _fileLength; |
| 147 bool _activeFillBufferCall = false; | |
| 138 } | 148 } |
| 139 | 149 |
| 140 | 150 |
| 141 class _PendingOperation { | 151 class _PendingOperation { |
| 142 const _PendingOperation(this._id); | 152 const _PendingOperation(this._id); |
| 143 static final _PendingOperation CLOSE = const _PendingOperation(0); | 153 static final _PendingOperation CLOSE = const _PendingOperation(0); |
| 144 static final _PendingOperation FLUSH = const _PendingOperation(1); | 154 static final _PendingOperation FLUSH = const _PendingOperation(1); |
| 145 final int _id; | 155 final int _id; |
| 146 } | 156 } |
| 147 | 157 |
| (...skipping 993 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1141 new FileIOException("File closed '$_name'")); | 1151 new FileIOException("File closed '$_name'")); |
| 1142 }); | 1152 }); |
| 1143 return completer.future; | 1153 return completer.future; |
| 1144 } | 1154 } |
| 1145 | 1155 |
| 1146 final String _name; | 1156 final String _name; |
| 1147 int _id; | 1157 int _id; |
| 1148 | 1158 |
| 1149 SendPort _fileService; | 1159 SendPort _fileService; |
| 1150 } | 1160 } |
| OLD | NEW |