Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import mojo_system | |
| 6 | |
| 7 def BlockingCopyFromDataPipeIntoString(data_pipe, deadline): | |
| 8 """ | |
| 9 Reads from |data_pipe| until either (1) there is no more data to read or | |
| 10 (2) an error is encountered. | |
| 11 Returns a tuple of the result status and output string. If the status is not | |
| 12 mojo_system.RESULT_OK, the returned string will be None. | |
| 13 """ | |
| 14 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.
| |
| 15 input_bytes = bytearray(BUFFER_SIZE) | |
| 16 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.
| |
| 17 while True: | |
| 18 status, read_bytes = data_pipe.ReadData(input_bytes) | |
| 19 if status == mojo_system.RESULT_OK: | |
| 20 output_bytes = output_bytes + read_bytes | |
| 21 continue | |
| 22 | |
| 23 if status == mojo_system.RESULT_SHOULD_WAIT: | |
| 24 status, _ = data_pipe.Wait(mojo_system.HANDLE_SIGNAL_READABLE, deadline) | |
| 25 | |
| 26 # If the wait results in RESULT_OK, try to read more bytes. | |
| 27 if status == mojo_system.RESULT_OK: | |
| 28 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.
| |
| 29 | |
| 30 # Treat a failed precondition as EOF. | |
| 31 if status == mojo_system.RESULT_FAILED_PRECONDITION: | |
| 32 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.
| |
| 33 | |
| 34 return (status, None) | |
| OLD | NEW |