summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-13 23:08:47 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-13 23:08:47 +0000
commitb2ca508afc56d4b7f6d5b036dd721604440210b3 (patch)
tree69b9b1146c0af1e063b5f38ce3e2307b7a223399 /net
parent2ce5d948eb4ac06b30fc411d276eb6fd70ad1721 (diff)
downloadchromium_src-b2ca508afc56d4b7f6d5b036dd721604440210b3.zip
chromium_src-b2ca508afc56d4b7f6d5b036dd721604440210b3.tar.gz
chromium_src-b2ca508afc56d4b7f6d5b036dd721604440210b3.tar.bz2
Add a new method ReadUntilComplete to FileStream which ensures that all requested bytes are read from the file in one call, assuming no errors occurr or EOF is reached.
Review URL: http://codereview.chromium.org/21363 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9803 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/file_stream.h7
-rw-r--r--net/base/file_stream_posix.cc21
-rw-r--r--net/base/file_stream_win.cc21
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())