Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Unified Diff: sdk/lib/io/file_impl.dart

Issue 11308226: Add RandomAccessFile.read method and use it for file input stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/file.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/file_impl.dart
diff --git a/sdk/lib/io/file_impl.dart b/sdk/lib/io/file_impl.dart
index 17dc2ab4241b2de96402f46e86e98b27761519b6..0d7a548139af1c29264a1e4f01d403cc2a753747 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -65,22 +65,15 @@ class _FileInputStream extends _BaseDataInputStream implements InputStream {
_closeFile();
return;
}
- // If there is currently a _fillBuffer call waiting on readList,
+ // If there is currently a _fillBuffer call waiting on read,
// let it fill the buffer instead of us.
if (_activeFillBufferCall) return;
_activeFillBufferCall = true;
- if (_data.length != size) {
- _data = new Uint8List(size);
- // Maintain the invariant signalling that the buffer is empty.
- _position = _data.length;
- }
- var future = _openedFile.readList(_data, 0, _data.length);
- future.then((read) {
- _filePosition += read;
- if (read != _data.length) {
- _data = _data.getRange(0, read);
- }
+ var future = _openedFile.read(size);
+ future.then((data) {
+ _data = data;
_position = 0;
+ _filePosition += _data.length;
_activeFillBufferCall = false;
if (_fileLength == _filePosition) {
@@ -319,8 +312,9 @@ const int _LAST_MODIFIED_REQUEST = 12;
const int _FLUSH_REQUEST = 13;
const int _READ_BYTE_REQUEST = 14;
const int _WRITE_BYTE_REQUEST = 15;
-const int _READ_LIST_REQUEST = 16;
-const int _WRITE_LIST_REQUEST = 17;
+const int _READ_REQUEST = 16;
+const int _READ_LIST_REQUEST = 17;
+const int _WRITE_LIST_REQUEST = 18;
// Base class for _File and _RandomAccessFile with shared functions.
class _FileBase {
@@ -787,6 +781,43 @@ class _RandomAccessFile extends _FileBase implements RandomAccessFile {
return result;
}
+ Future<List<int>> read(int bytes) {
+ _ensureFileService();
+ Completer<List<int>> completer = new Completer<List<int>>();
+ if (bytes is !int) {
+ // Complete asynchronously so the user has a chance to setup
+ // handlers without getting exceptions when registering the
+ // then handler.
+ new Timer(0, (t) {
+ completer.completeException(new FileIOException(
+ "Invalid arguments to read for file '$_name'"));
+ });
+ return completer.future;
+ };
+ if (closed) return _completeWithClosedException(completer);
+ List request = new List(3);
+ request[0] = _READ_REQUEST;
+ request[1] = _id;
+ request[2] = bytes;
+ return _fileService.call(request).transform((response) {
+ if (_isErrorResponse(response)) {
+ throw _exceptionFromResponse(response,
+ "read failed for file '$_name'");
+ }
+ return response[1];
+ });
+ }
+
+ external static _read(int id, int bytes);
+
+ List<int> readSync(int bytes) {
+ if (bytes is !int) {
+ throw new FileIOException(
+ "Invalid arguments to readSync for file '$_name'");
+ }
+ return _read(_id, bytes);
+ }
+
Future<int> readList(List<int> buffer, int offset, int bytes) {
_ensureFileService();
Completer<int> completer = new Completer<int>();
« no previous file with comments | « sdk/lib/io/file.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698