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

Unified Diff: runtime/bin/stream_util.dart

Issue 8953030: Add input streams based on supplying data from lists (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years 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
Index: runtime/bin/stream_util.dart
diff --git a/runtime/bin/stream_util.dart b/runtime/bin/stream_util.dart
index 1c9cca3123e394754a7c2fbef3039f0087918946..aae250d8fd60b86290fe9135578dbe19e83306dd 100644
--- a/runtime/bin/stream_util.dart
+++ b/runtime/bin/stream_util.dart
@@ -2,6 +2,97 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+class _BaseDataInputStream {
+ abstract int available();
+
+ List<int> read([int len]) {
+ if (_closeCallbackCalled) return null;
+ int bytesToRead = available();
+ if (bytesToRead == 0) {
+ _checkScheduleCallbacks();
+ return null;
+ }
+ if (len !== null) {
+ if (len <= 0) {
+ throw new StreamException("Illegal length $len");
+ } else if (bytesToRead > len) {
+ bytesToRead = len;
+ }
+ }
+ return _read(bytesToRead);
+ }
+
+ int readInto(List<int> buffer, [int offset = 0, int len]) {
+ if (_closeCallbackCalled) return null;
+ if (len === null) len = buffer.length;
+ if (offset < 0) throw new StreamException("Illegal offset $offset");
+ if (len < 0) throw new StreamException("Illegal length $len");
+ int bytesToRead = Math.min(len, available());
+ return _readInto(buffer, offset, bytesToRead);
+ }
+
+ bool get closed() => _closeCallbackCalled;
+
+ void set dataHandler(void callback()) {
+ _clientDataHandler = callback;
+ _checkScheduleCallbacks();
+ }
+
+ void set closeHandler(void callback()) {
+ _clientCloseHandler = callback;
+ _checkScheduleCallbacks();
+ }
+
+ void set errorHandler(void callback()) {
+ // No errors emitted by default.
+ }
+
+ abstract List<int> _read(int bytesToRead);
+
+ void _checkScheduleCallbacks() {
+ // TODO(sgjesse): Find a better way of scheduling callbacks from
Mads Ager (google) 2011/12/19 15:04:18 This comment used to be about the Timer with a 0ms
Søren Gjesse 2011/12/20 09:45:15 Done.
+ // the event loop.
+ void issueDataCallback(Timer timer) {
+ _scheduledDataCallback = null;
+ if (_clientDataHandler !== null) {
+ _clientDataHandler();
+ _checkScheduleCallbacks();
+ }
+ }
+
+ void issueCloseCallback(Timer timer) {
+ _scheduledCloseCallback = null;
+ if (_clientCloseHandler !== null) _clientCloseHandler();
+ }
+
+ // Schedule data callback if there is more data to read. Schedule
+ // close callback once when all data has been read. Only schedule
+ // a new callback if the previous one has actually been called.
+ if (!_closeCallbackCalled) {
+ if (available() > 0) {
+ if (_scheduledDataCallback == null) {
+ _scheduledDataCallback = new Timer(issueDataCallback, 0);
+ }
+ } else if (_streamMarkedClosed && !_closeCallbackCalled) {
+ _scheduledCloseCallback = new Timer(issueCloseCallback, 0);
+ _closeCallbackCalled = true;
+ }
+ }
+ }
+
+ // Stream is marked closed available is now all remaining data.
Mads Ager (google) 2011/12/19 15:04:18 Comment not completely understood. I think you sho
Søren Gjesse 2011/12/20 09:45:15 Done.
+ bool _streamMarkedClosed = false;
+
+ // The close callback has now been called and stream is fully closed.
Mads Ager (google) 2011/12/19 15:04:18 and stream -> and the stream
Søren Gjesse 2011/12/20 09:45:15 Done.
+ bool _closeCallbackCalled = false;
+
+ Timer _scheduledDataCallback;
+ Timer _scheduledCloseCallback;
+ var _clientDataHandler;
+ var _clientCloseHandler;
+}
+
+
void _pipe(InputStream input, OutputStream output, [bool close]) {
Function pipeDataHandler;
Function pipeCloseHandler;

Powered by Google App Engine
This is Rietveld 408576698