diff options
-rw-r--r-- | net/base/file_stream.h | 7 | ||||
-rw-r--r-- | net/base/file_stream_posix.cc | 21 | ||||
-rw-r--r-- | net/base/file_stream_win.cc | 21 |
3 files changed, 49 insertions, 0 deletions
diff --git a/net/base/file_stream.h b/net/base/file_stream.h index 20466b4..eaf6a0d 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -81,6 +81,13 @@ class FileStream { // You can pass NULL as the callback for synchronous I/O. int Read(char* buf, int buf_len, CompletionCallback* callback); + // Performs the same as Read, but ensures that exactly buf_len bytes + // are copied into buf. A partial read may occur, but only as a result of + // end-of-file or fatal error. Returns the number of bytes copied into buf, + // 0 if at end-of-file and no bytes have been read into buf yet, + // or an error code if the operation could not be performed. + int ReadUntilComplete(char *buf, int buf_len); + // Call this method to write data at the current stream position. Up to // buf_len bytes will be written from buf. (In other words, partial writes are // allowed.) Returns the number of bytes written, or an error code if the diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index 7b248a4..151cf5d 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -154,6 +154,27 @@ int FileStream::Read( } } +int FileStream::ReadUntilComplete(char *buf, int buf_len) { + int to_read = buf_len; + int bytes_total = 0; + + do { + int bytes_read = Read(buf, to_read, NULL); + if (bytes_read <= 0) { + if (bytes_total == 0) + return bytes_read; + + return bytes_total; + } + + bytes_total += bytes_read; + buf += bytes_read; + to_read -= bytes_read; + } while (bytes_total < buf_len); + + return bytes_total; +} + // TODO(deanm): async. int FileStream::Write( const char* buf, int buf_len, CompletionCallback* callback) { diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index ca492a2..1ab71df 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -234,6 +234,27 @@ int FileStream::Read( return rv; } +int FileStream::ReadUntilComplete(char *buf, int buf_len) { + int to_read = buf_len; + int bytes_total = 0; + + do { + int bytes_read = Read(buf, to_read, NULL); + if (bytes_read <= 0) { + if (bytes_total == 0) + return bytes_read; + + return bytes_total; + } + + bytes_total += bytes_read; + buf += bytes_read; + to_read -= bytes_read; + } while (bytes_total < buf_len); + + return bytes_total; +} + int FileStream::Write( const char* buf, int buf_len, CompletionCallback* callback) { if (!IsOpen()) |