m_fixed = true;
}
-wxStreamBuffer::wxStreamBuffer(BufMode mode)
+void wxStreamBuffer::InitWithStream(wxStreamBase& stream, BufMode mode)
{
Init();
- m_stream = NULL;
+ m_stream = &stream;
m_mode = mode;
- m_flushable = false;
+ m_flushable = true;
}
-wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
+wxStreamBuffer::wxStreamBuffer(BufMode mode)
{
Init();
- m_stream = &stream;
+ m_stream = NULL;
m_mode = mode;
- m_flushable = true;
+ m_flushable = false;
}
wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
// wxBufferedInputStream
// ----------------------------------------------------------------------------
-wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s,
+namespace
+{
+
+// helper function used for initializing the buffer used by
+// wxBufferedInput/OutputStream: it simply returns the provided buffer if it's
+// not NULL or creates a buffer of the given size otherwise
+template <typename T>
+wxStreamBuffer *
+CreateBufferIfNeeded(T& stream, wxStreamBuffer *buffer, size_t bufsize = 1024)
+{
+ return buffer ? buffer : new wxStreamBuffer(stream, bufsize);
+}
+
+} // anonymous namespace
+
+wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
wxStreamBuffer *buffer)
- : wxFilterInputStream(s)
+ : wxFilterInputStream(stream)
{
- if ( buffer )
- {
- // use the buffer provided by the user
- m_i_streambuf = buffer;
- }
- else // create a default buffer
- {
- m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
+ m_i_streambuf = CreateBufferIfNeeded(*this, buffer);
+}
- m_i_streambuf->SetBufferIO(1024);
- }
+wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
+ size_t bufsize)
+ : wxFilterInputStream(stream)
+{
+ m_i_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
}
wxBufferedInputStream::~wxBufferedInputStream()
size -= m_lastcount;
buf = (char *)buf + m_lastcount;
- // the call to wxStreamBuffer::Read() below will reset our m_lastcount,
- // so save it
+ // the call to wxStreamBuffer::Read() below may reset our m_lastcount
+ // (but it also may not do it if the buffer is associated to another
+ // existing stream and wasn't created by us), so save it
size_t countOld = m_lastcount;
- m_i_streambuf->Read(buf, size);
+ // the new count of the bytes read is the count of bytes read this time
+ m_lastcount = m_i_streambuf->Read(buf, size);
+ // plus those we had read before
m_lastcount += countOld;
}
// wxBufferedOutputStream
// ----------------------------------------------------------------------------
-wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s,
+wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
wxStreamBuffer *buffer)
- : wxFilterOutputStream(s)
+ : wxFilterOutputStream(stream)
{
- if ( buffer )
- {
- m_o_streambuf = buffer;
- }
- else // create a default one
- {
- m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
+ m_o_streambuf = CreateBufferIfNeeded(*this, buffer);
+}
- m_o_streambuf->SetBufferIO(1024);
- }
+wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
+ size_t bufsize)
+ : wxFilterOutputStream(stream)
+{
+ m_o_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
}
wxBufferedOutputStream::~wxBufferedOutputStream()