Index: mojo/python/mojo_utils/data_pipe_utils.py |
diff --git a/mojo/python/mojo_utils/data_pipe_utils.py b/mojo/python/mojo_utils/data_pipe_utils.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..707c37e848f30bd9c815be0516df283b6f17a45b |
--- /dev/null |
+++ b/mojo/python/mojo_utils/data_pipe_utils.py |
@@ -0,0 +1,34 @@ |
+# Copyright 2015 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import mojo_system |
+ |
+def BlockingCopyFromDataPipeIntoString(data_pipe, deadline): |
+ """ |
+ Reads from |data_pipe| until either (1) there is no more data to read or |
+ (2) an error is encountered. |
+ Returns a tuple of the result status and output string. If the status is not |
+ mojo_system.RESULT_OK, the returned string will be None. |
+ """ |
+ BUFFER_SIZE = 1000 |
qsr
2015/02/25 15:22:35
Use a power of 2, usual values are 1 to 4k
blundell
2015/02/26 16:33:53
Done.
|
+ input_bytes = bytearray(BUFFER_SIZE) |
+ output_bytes = bytearray() |
qsr
2015/02/25 15:22:35
Instead of transferring in BUFFER_SIZE chunk, beca
blundell
2015/02/26 16:33:53
Done.
|
+ while True: |
+ status, read_bytes = data_pipe.ReadData(input_bytes) |
+ if status == mojo_system.RESULT_OK: |
+ output_bytes = output_bytes + read_bytes |
+ continue |
+ |
+ if status == mojo_system.RESULT_SHOULD_WAIT: |
+ status, _ = data_pipe.Wait(mojo_system.HANDLE_SIGNAL_READABLE, deadline) |
+ |
+ # If the wait results in RESULT_OK, try to read more bytes. |
+ if status == mojo_system.RESULT_OK: |
+ continue |
qsr
2015/02/25 15:22:35
What happen is you get another result here? In par
blundell
2015/02/26 16:33:53
Any result other than OK or FAILED_PRECONDITION is
qsr
2015/02/26 16:45:19
Hum, misread the alignment. Ok.
|
+ |
+ # Treat a failed precondition as EOF. |
+ if status == mojo_system.RESULT_FAILED_PRECONDITION: |
+ return (mojo_system.RESULT_OK, output_bytes.decode("utf-8")) |
qsr
2015/02/25 15:22:35
This method being a generic utility method, it sho
blundell
2015/02/26 16:33:52
Done.
|
+ |
+ return (status, None) |