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
This commit is contained in:
@@ -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
|
This stream acts as a cache. It caches the bytes to be written to the specified
|
||||||
output stream (See \helpref{wxFilterOutputStream}{wxfilteroutputstream}). The
|
output stream (See \helpref{wxFilterOutputStream}{wxfilteroutputstream}). The
|
||||||
datas are only written when the cache is full or when the buffered stream is
|
datas are only written when the cache is full, when the buffered stream is
|
||||||
destroyed.
|
destroyed or when calling SeekO().
|
||||||
|
|
||||||
This class may not be used without some other stream to write the data
|
This class may not be used without some other stream to write the data
|
||||||
to (such as a file stream or a memory stream).
|
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}
|
\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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -60,6 +60,7 @@ BEGIN_EVENT_TABLE(MyApp, wxApp)
|
|||||||
#if wxUSE_UNICODE
|
#if wxUSE_UNICODE
|
||||||
EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
|
EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
|
||||||
#endif
|
#endif
|
||||||
|
EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2)
|
||||||
EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
|
EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
|
||||||
EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo)
|
EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
@@ -89,6 +90,7 @@ bool MyApp::OnInit()
|
|||||||
test_menu->Append(TYPES_UNICODE, "&Unicode test");
|
test_menu->Append(TYPES_UNICODE, "&Unicode test");
|
||||||
#endif
|
#endif
|
||||||
test_menu->Append(TYPES_STREAM, "&Stream test");
|
test_menu->Append(TYPES_STREAM, "&Stream test");
|
||||||
|
test_menu->Append(TYPES_STREAM2, "&Stream seek test");
|
||||||
test_menu->AppendSeparator();
|
test_menu->AppendSeparator();
|
||||||
test_menu->Append(TYPES_MIME, "&MIME database test");
|
test_menu->Append(TYPES_MIME, "&MIME database test");
|
||||||
|
|
||||||
@@ -256,6 +258,78 @@ void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
|
|||||||
textCtrl.WriteText( tmp );
|
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
|
#if wxUSE_UNICODE
|
||||||
void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
|
void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
|
||||||
{
|
{
|
||||||
|
@@ -30,6 +30,7 @@ public:
|
|||||||
void DoVariantDemo(wxCommandEvent& event);
|
void DoVariantDemo(wxCommandEvent& event);
|
||||||
void DoByteOrderDemo(wxCommandEvent& event);
|
void DoByteOrderDemo(wxCommandEvent& event);
|
||||||
void DoStreamDemo(wxCommandEvent& event);
|
void DoStreamDemo(wxCommandEvent& event);
|
||||||
|
void DoStreamDemo2(wxCommandEvent& event);
|
||||||
#if wxUSE_UNICODE
|
#if wxUSE_UNICODE
|
||||||
void DoUnicodeDemo(wxCommandEvent& event);
|
void DoUnicodeDemo(wxCommandEvent& event);
|
||||||
#endif
|
#endif
|
||||||
@@ -74,6 +75,7 @@ enum
|
|||||||
TYPES_BYTEORDER,
|
TYPES_BYTEORDER,
|
||||||
TYPES_UNICODE,
|
TYPES_UNICODE,
|
||||||
TYPES_STREAM,
|
TYPES_STREAM,
|
||||||
|
TYPES_STREAM2,
|
||||||
TYPES_MIME
|
TYPES_MIME
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -381,7 +381,8 @@ bool wxImage::LoadFile( const wxString& filename, long type )
|
|||||||
if (wxFileExists(filename))
|
if (wxFileExists(filename))
|
||||||
{
|
{
|
||||||
wxFileInputStream stream(filename);
|
wxFileInputStream stream(filename);
|
||||||
return LoadFile(stream, type);
|
wxBufferedInputStream bstream( stream );
|
||||||
|
return LoadFile(bstream, type);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -400,7 +401,8 @@ bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
|
|||||||
if (wxFileExists(filename))
|
if (wxFileExists(filename))
|
||||||
{
|
{
|
||||||
wxFileInputStream stream(filename);
|
wxFileInputStream stream(filename);
|
||||||
return LoadFile(stream, mimetype);
|
wxBufferedInputStream bstream( stream );
|
||||||
|
return LoadFile(bstream, mimetype);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -419,7 +421,10 @@ bool wxImage::SaveFile( const wxString& filename, int type )
|
|||||||
wxFileOutputStream stream(filename);
|
wxFileOutputStream stream(filename);
|
||||||
|
|
||||||
if ( stream.LastError() == wxStream_NOERROR )
|
if ( stream.LastError() == wxStream_NOERROR )
|
||||||
return SaveFile(stream, type);
|
{
|
||||||
|
wxBufferedOutputStream bstream( stream );
|
||||||
|
return SaveFile(bstream, type);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
#endif // wxUSE_STREAMS
|
#endif // wxUSE_STREAMS
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -431,7 +436,10 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
|
|||||||
wxFileOutputStream stream(filename);
|
wxFileOutputStream stream(filename);
|
||||||
|
|
||||||
if ( stream.LastError() == wxStream_NOERROR )
|
if ( stream.LastError() == wxStream_NOERROR )
|
||||||
return SaveFile(stream, mimetype);
|
{
|
||||||
|
wxBufferedOutputStream bstream( stream );
|
||||||
|
return SaveFile(bstream, mimetype);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
#endif // wxUSE_STREAMS
|
#endif // wxUSE_STREAMS
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@@ -856,6 +856,7 @@ wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s)
|
|||||||
|
|
||||||
wxBufferedOutputStream::~wxBufferedOutputStream()
|
wxBufferedOutputStream::~wxBufferedOutputStream()
|
||||||
{
|
{
|
||||||
|
Sync();
|
||||||
delete m_o_streambuf;
|
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)
|
off_t wxBufferedOutputStream::SeekO(off_t pos, wxSeekMode mode)
|
||||||
{
|
{
|
||||||
|
Sync();
|
||||||
return m_o_streambuf->Seek(pos, mode);
|
return m_o_streambuf->Seek(pos, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user