Add wxInputStream::ReadAll() and wxOutputStream::WriteAll().

Unlike Read() and Write(), these functions always transfer exactly the
specified number of bytes or fail.

See #12056.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74034 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2013-05-20 13:15:16 +00:00
parent 54582f9ac6
commit cc437b9654
4 changed files with 107 additions and 0 deletions

View File

@@ -913,6 +913,42 @@ wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
return *this;
}
bool wxInputStream::ReadAll(void *buffer_, size_t size)
{
char* buffer = static_cast<char*>(buffer_);
m_lastcount = 0;
for ( ;; )
{
const size_t lastCount = Read(buffer, size).LastRead();
// There is no point in continuing looping if we can't read anything at
// all.
if ( !lastCount )
break;
m_lastcount += lastCount;
// ... Or if an error occurred on the stream.
if ( !IsOk() )
break;
// Return successfully if we read exactly the requested number of
// bytes (normally the ">" case should never occur and so we could use
// "==" test, but be safe and avoid overflowing size even in case of
// bugs in LastRead()).
if ( lastCount >= size )
return true;
// Advance the buffer before trying to read the rest of data.
size -= lastCount;
buffer += lastCount;
}
return false;
}
wxFileOffset wxInputStream::SeekI(wxFileOffset pos, wxSeekMode mode)
{
// RR: This code is duplicated in wxBufferedInputStream. This is
@@ -1030,6 +1066,34 @@ wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in)
return *this;
}
bool wxOutputStream::WriteAll(const void *buffer_, size_t size)
{
// This exactly mirrors ReadAll(), see there for more comments.
const char* buffer = static_cast<const char*>(buffer_);
m_lastcount = 0;
for ( ;; )
{
const size_t lastCount = Write(buffer, size).LastWrite();
if ( !lastCount )
break;
m_lastcount += lastCount;
if ( !IsOk() )
break;
if ( lastCount >= size )
return true;
size -= lastCount;
buffer += lastCount;
}
return false;
}
wxFileOffset wxOutputStream::TellO() const
{
return OnSysTell();