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

Side by Side Diff: runtime/bin/file_impl.dart

Issue 9067004: Refactor the file input stream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/src/FileTest.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 implements InputStream { 5 class _FileInputStream extends _BaseDataInputStream implements InputStream {
6 _FileInputStream(File file) { 6 _FileInputStream(File file) {
7 _file = file.openSync(); 7 _file = file.openSync();
8 _length = _file.lengthSync(); 8 _length = _file.lengthSync();
9 _streamMarkedClosed = true;
9 _checkScheduleCallbacks(); 10 _checkScheduleCallbacks();
10 } 11 }
11 12
12 List<int> read([int len]) { 13 int available() {
13 if (_closed) return null; 14 return _length - _file.positionSync();
14 int bytesToRead = available();
15 if (bytesToRead == 0) {
16 _checkScheduleCallbacks();
17 return null;
18 }
19 if (len !== null) {
20 if (len <= 0) {
21 throw new StreamException("Illegal length $len");
22 } else if (bytesToRead > len) {
23 bytesToRead = len;
24 }
25 }
26 List<int> buffer = new List<int>(bytesToRead);
27 int bytesRead = _file.readListSync(buffer, 0, bytesToRead);
28 if (bytesRead < bytesToRead) {
29 List<int> newBuffer = new List<int>(bytesRead);
30 newBuffer.copyFrom(buffer, 0, 0, bytesRead);
31 return newBuffer;
32 } else {
33 return buffer;
34 }
35 } 15 }
36 16
37 int readInto(List<int> buffer, int offset, int len) { 17 void pipe(OutputStream output, [bool close = true]) {
38 if (offset === null) offset = 0; 18 _pipe(this, output, close: close);
39 if (len === null) len = buffer.length; 19 }
40 if (offset < 0) throw new StreamException("Illegal offset $offset"); 20
41 if (len < 0) throw new StreamException("Illegal length $len"); 21 List<int> _read(int bytesToRead) {
22 List<int> result = new List<int>(bytesToRead);
23 int bytesRead = _file.readListSync(result, 0, bytesToRead);
24 if (bytesRead < bytesToRead) {
25 List<int> buffer = new List<int>(bytesRead);
26 buffer.copyFrom(result, 0, 0, bytesRead);
27 result = buffer;
28 }
29 _checkScheduleCallbacks();
30 return result;
31 }
32
33 int _readInto(List<int> buffer, int offset, int len) {
42 int result = _file.readListSync(buffer, offset, len); 34 int result = _file.readListSync(buffer, offset, len);
43 _checkScheduleCallbacks(); 35 _checkScheduleCallbacks();
44 return result; 36 return result;
45 } 37 }
46 38
47 int available() => (_closing || _eof) ? 0 : _length - _file.positionSync();
48
49 void pipe(OutputStream output, [bool close = true]) {
50 _pipe(this, output, close: close);
51 }
52
53 bool get closed() => _eof;
54
55 void close() { 39 void close() {
56 _closing = true;
57 _file.closeSync(); 40 _file.closeSync();
58 } 41 _closeCallbackCalled = true;
59
60 void set dataHandler(void callback()) {
61 _clientDataHandler = callback;
62 _checkScheduleCallbacks();
63 }
64
65 void set closeHandler(void callback()) {
66 _clientCloseHandler = callback;
67 }
68
69 void set errorHandler(void callback()) {
70 // TODO(sgjesse): How to handle this?
71 }
72
73 void _checkScheduleCallbacks() {
74 void issueDataCallback(Timer timer) {
75 _scheduledDataCallback = null;
76 if (_clientDataHandler !== null) {
77 _clientDataHandler();
78 _checkScheduleCallbacks();
79 }
80 }
81
82 void issueCloseCallback(Timer timer) {
83 _scheduledCloseCallback = null;
84 if (!_closed) {
85 if (_clientCloseHandler !== null) _clientCloseHandler();
86 _closed = true;
87 }
88 }
89
90 // Schedule data callback if there is more data to read. Schedule
91 // close callback once when all data has been read. Only schedule
92 // a new callback if the previous one has actually been called.
93 if (!_closed) {
94 if (available() > 0) {
95 if (_scheduledDataCallback == null) {
96 _scheduledDataCallback = new Timer(issueDataCallback, 0);
97 }
98 } else if (!_eof) {
99 close();
100 if (_scheduledCloseCallback == null) {
101 _scheduledCloseCallback = new Timer(issueCloseCallback, 0);
102 }
103 _eof = true;
104 }
105 }
106 } 42 }
107 43
108 RandomAccessFile _file; 44 RandomAccessFile _file;
109 int _length; 45 int _length;
110 bool _eof = false;
111 bool _closing = false;
112 bool _closed = false;
113 Timer _scheduledDataCallback;
114 Timer _scheduledCloseCallback;
115 Function _clientDataHandler;
116 Function _clientCloseHandler;
117 } 46 }
118 47
119 48
120 class _FileOutputStream implements OutputStream { 49 class _FileOutputStream implements OutputStream {
121 _FileOutputStream(File file) { 50 _FileOutputStream(File file) {
122 _file = file.openSync(true); 51 _file = file.openSync(true);
123 } 52 }
124 53
125 bool write(List<int> buffer, [bool copyBuffer = false]) { 54 bool write(List<int> buffer, [bool copyBuffer = false]) {
126 return _write(buffer, 0, buffer.length); 55 return _write(buffer, 0, buffer.length);
(...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after
1117 var _readByteHandler; 1046 var _readByteHandler;
1118 var _readListHandler; 1047 var _readListHandler;
1119 var _noPendingWriteHandler; 1048 var _noPendingWriteHandler;
1120 var _positionHandler; 1049 var _positionHandler;
1121 var _setPositionHandler; 1050 var _setPositionHandler;
1122 var _truncateHandler; 1051 var _truncateHandler;
1123 var _lengthHandler; 1052 var _lengthHandler;
1124 var _flushHandler; 1053 var _flushHandler;
1125 var _errorHandler; 1054 var _errorHandler;
1126 } 1055 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/src/FileTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698