fs/io/BufferedOutputStream: add code comments
This commit is contained in:
@@ -41,17 +41,22 @@ bool
|
|||||||
BufferedOutputStream::Write(const void *data, size_t size)
|
BufferedOutputStream::Write(const void *data, size_t size)
|
||||||
{
|
{
|
||||||
if (gcc_unlikely(last_error.IsDefined()))
|
if (gcc_unlikely(last_error.IsDefined()))
|
||||||
|
/* the stream has already failed */
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
/* try to append to the current buffer */
|
||||||
if (AppendToBuffer(data, size))
|
if (AppendToBuffer(data, size))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
/* not enough room in the buffer - flush it */
|
||||||
if (!Flush())
|
if (!Flush())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
/* see if there's now enough room */
|
||||||
if (AppendToBuffer(data, size))
|
if (AppendToBuffer(data, size))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
/* too large for the buffer: direct write */
|
||||||
return os.Write(data, size, last_error);
|
return os.Write(data, size, last_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,6 +30,14 @@
|
|||||||
class OutputStream;
|
class OutputStream;
|
||||||
class Error;
|
class Error;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An #OutputStream wrapper that buffers its output to reduce the
|
||||||
|
* number of OutputStream::Write() calls.
|
||||||
|
*
|
||||||
|
* It simplifies error handling by managing an #Error attribute.
|
||||||
|
* Invoke any number of writes, and check for errors in the end using
|
||||||
|
* Check().
|
||||||
|
*/
|
||||||
class BufferedOutputStream {
|
class BufferedOutputStream {
|
||||||
OutputStream &os;
|
OutputStream &os;
|
||||||
|
|
||||||
@@ -47,11 +55,18 @@ public:
|
|||||||
gcc_printf(2,3)
|
gcc_printf(2,3)
|
||||||
bool Format(const char *fmt, ...);
|
bool Format(const char *fmt, ...);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns false if an error has occurred.
|
||||||
|
*/
|
||||||
gcc_pure
|
gcc_pure
|
||||||
bool Check() const {
|
bool Check() const {
|
||||||
return !last_error.IsDefined();
|
return !last_error.IsDefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns false if an error has occurred. In that case, a
|
||||||
|
* copy of the #Error is returned.
|
||||||
|
*/
|
||||||
bool Check(Error &error) const {
|
bool Check(Error &error) const {
|
||||||
if (last_error.IsDefined()) {
|
if (last_error.IsDefined()) {
|
||||||
error.Set(last_error);
|
error.Set(last_error);
|
||||||
@@ -60,6 +75,9 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write buffer contents to the #OutputStream.
|
||||||
|
*/
|
||||||
bool Flush();
|
bool Flush();
|
||||||
|
|
||||||
bool Flush(Error &error);
|
bool Flush(Error &error);
|
||||||
|
Reference in New Issue
Block a user