From: Robert Roebling Date: Mon, 6 Dec 1999 18:16:37 +0000 (+0000) Subject: Added tests for buffered streams. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/1b055864b65201f8cf2c704a03da62bd91e7f497 Added tests for buffered streams. Made wxBufferedOutputStream call Sync() in the destructor and SeekO(). Documented this. Use buffered streams in wxImage. Seems to significantly speed-up the sample. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4847 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/strmbfrd.tex b/docs/latex/wx/strmbfrd.tex index f6822b29cc..e9c636b25b 100644 --- a/docs/latex/wx/strmbfrd.tex +++ b/docs/latex/wx/strmbfrd.tex @@ -28,8 +28,9 @@ from (such as a file stream or a memory stream). This stream acts as a cache. It caches the bytes to be written to the specified output stream (See \helpref{wxFilterOutputStream}{wxfilteroutputstream}). The -datas are only written when the cache is full or when the buffered stream is -destroyed. +datas are only written when the cache is full, when the buffered stream is +destroyed or when calling SeekO(). + This class may not be used without some other stream to write the data to (such as a file stream or a memory stream). @@ -45,3 +46,35 @@ to (such as a file stream or a memory stream). \helpref{wxStreamBuffer}{wxstreamBuffer}, \helpref{wxOutputStream}{wxoutputstream} +% ---------- +% Members +% ---------- +\latexignore{\rtfignore{\wxheading{Members}}} + +\membersection{wxBufferedOutputStream::wxBufferedOutputStream} + +\func{}{wxBufferedOutputStream}{\param{const wxOutputStream\&}{ parent}} + +Creates a buffered stream using a buffer of a default size of 1024 bytes for cashing +the stream {\it parent}. + +\membersection{wxBufferedOutputStream::\destruct{wxBufferedOutputStream}} + +\func{}{\destruct{wxBufferedOutputStream}}{\void} + +Destructor. Calls Sync() and destroys the internal buffer. + +\membersection{wxBufferedOutputStream::SeekO} + +\func{off\_t}{SeekO}{\param{off\_t}{ pos}, \param{wxSeekMode}{ mode}} + +Calls Sync() and changes the stream position. + +\membersection{wxBufferedOutputStream::Sync} + +\func{void}{Sync}{\void} + +Flushes the buffer and calls Sync() on the parent stream. + + + diff --git a/samples/typetest/typetest.cpp b/samples/typetest/typetest.cpp index ed81e14691..cf93e4922a 100644 --- a/samples/typetest/typetest.cpp +++ b/samples/typetest/typetest.cpp @@ -60,6 +60,7 @@ BEGIN_EVENT_TABLE(MyApp, wxApp) #if wxUSE_UNICODE EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo) #endif + EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2) EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo) EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo) END_EVENT_TABLE() @@ -89,6 +90,7 @@ bool MyApp::OnInit() test_menu->Append(TYPES_UNICODE, "&Unicode test"); #endif test_menu->Append(TYPES_STREAM, "&Stream test"); + test_menu->Append(TYPES_STREAM2, "&Stream seek test"); test_menu->AppendSeparator(); test_menu->Append(TYPES_MIME, "&MIME database test"); @@ -256,6 +258,78 @@ void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) textCtrl.WriteText( tmp ); } +void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event)) +{ + wxTextCtrl& textCtrl = * GetTextCtrl(); + + textCtrl.Clear(); + textCtrl << _T("\nTest wxBufferedStream:\n\n"); + + char ch,ch2; + + textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream:\n\n" ); + + wxFileOutputStream file_output( "test_wx.dat" ); + wxBufferedOutputStream buf_output( file_output ); + for (ch = 0; ch < 10; ch++) + buf_output.Write( &ch, 1 ); + buf_output.Sync(); + + wxFileInputStream file_input( "test_wx.dat" ); + for (ch2 = 0; ch2 < 10; ch2++) + { + file_input.Read( &ch, 1 ); + textCtrl.WriteText( (char)(ch + '0') ); + } + textCtrl.WriteText( "\n\n\n" ); + + textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream, then\n" ); + textCtrl.WriteText( "seeking back to #3 and writing 3:\n\n" ); + + wxFileOutputStream file_output2( "test_wx2.dat" ); + wxBufferedOutputStream buf_output2( file_output2 ); + for (ch = 0; ch < 10; ch++) + buf_output2.Write( &ch, 1 ); + buf_output2.SeekO( 3 ); + ch = 3; + buf_output2.Write( &ch, 1 ); + buf_output2.Sync(); + + wxFileInputStream file_input2( "test_wx2.dat" ); + for (ch2 = 0; ch2 < 10; ch2++) + { + file_input2.Read( &ch, 1 ); + textCtrl.WriteText( (char)(ch + '0') ); + } + textCtrl.WriteText( "\n\n\n" ); + + // now append 2000 bytes to file (bigger than buffer) + buf_output2.SeekO( 0, wxFromEnd ); + ch = 1; + for (int i = 0; i < 2000; i++) + buf_output2.Write( &ch, 1 ); + buf_output2.Sync(); + + textCtrl.WriteText( "Reading number 0 to 9 from buffered wxFileInputStream, then\n" ); + textCtrl.WriteText( "seeking back to #3 and reading 3:\n\n" ); + + wxFileInputStream file_input3( "test_wx2.dat" ); + wxBufferedInputStream buf_input3( file_input3 ); + for (ch2 = 0; ch2 < 10; ch2++) + { + buf_input3.Read( &ch, 1 ); + textCtrl.WriteText( (char)(ch + '0') ); + } + for (int j = 0; j < 2000; j++) + buf_input3.Read( &ch, 1 ); + textCtrl.WriteText( "\n" ); + buf_input3.SeekI( 3 ); + buf_input3.Read( &ch, 1 ); + textCtrl.WriteText( (char)(ch + '0') ); + textCtrl.WriteText( "\n\n\n" ); + +} + #if wxUSE_UNICODE void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event)) { diff --git a/samples/typetest/typetest.h b/samples/typetest/typetest.h index a8f6124b38..c7b9d15e0f 100644 --- a/samples/typetest/typetest.h +++ b/samples/typetest/typetest.h @@ -30,6 +30,7 @@ public: void DoVariantDemo(wxCommandEvent& event); void DoByteOrderDemo(wxCommandEvent& event); void DoStreamDemo(wxCommandEvent& event); + void DoStreamDemo2(wxCommandEvent& event); #if wxUSE_UNICODE void DoUnicodeDemo(wxCommandEvent& event); #endif @@ -74,6 +75,7 @@ enum TYPES_BYTEORDER, TYPES_UNICODE, TYPES_STREAM, + TYPES_STREAM2, TYPES_MIME }; diff --git a/src/common/image.cpp b/src/common/image.cpp index 29e7087ea3..41c25dcc68 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -381,7 +381,8 @@ bool wxImage::LoadFile( const wxString& filename, long type ) if (wxFileExists(filename)) { wxFileInputStream stream(filename); - return LoadFile(stream, type); + wxBufferedInputStream bstream( stream ); + return LoadFile(bstream, type); } else { @@ -400,7 +401,8 @@ bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype ) if (wxFileExists(filename)) { wxFileInputStream stream(filename); - return LoadFile(stream, mimetype); + wxBufferedInputStream bstream( stream ); + return LoadFile(bstream, mimetype); } else { @@ -419,7 +421,10 @@ bool wxImage::SaveFile( const wxString& filename, int type ) wxFileOutputStream stream(filename); if ( stream.LastError() == wxStream_NOERROR ) - return SaveFile(stream, type); + { + wxBufferedOutputStream bstream( stream ); + return SaveFile(bstream, type); + } else #endif // wxUSE_STREAMS return FALSE; @@ -431,7 +436,10 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) wxFileOutputStream stream(filename); if ( stream.LastError() == wxStream_NOERROR ) - return SaveFile(stream, mimetype); + { + wxBufferedOutputStream bstream( stream ); + return SaveFile(bstream, mimetype); + } else #endif // wxUSE_STREAMS return FALSE; diff --git a/src/common/stream.cpp b/src/common/stream.cpp index e18c411643..84a0b26973 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -856,6 +856,7 @@ wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s) wxBufferedOutputStream::~wxBufferedOutputStream() { + Sync(); delete m_o_streambuf; } @@ -868,6 +869,7 @@ wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size) off_t wxBufferedOutputStream::SeekO(off_t pos, wxSeekMode mode) { + Sync(); return m_o_streambuf->Seek(pos, mode); }