1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStreamBase and its derived classes
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 These enumeration values are returned by various functions in the context
16 wxSTREAM_NO_ERROR
= 0, //!< No error occurred.
17 wxSTREAM_EOF
, //!< EOF reached in Read() or similar.
18 wxSTREAM_WRITE_ERROR
, //!< generic write error on the last write call.
19 wxSTREAM_READ_ERROR
//!< generic read error on the last read call.
25 This class is the base class of most stream related classes in wxWidgets.
26 It must not be used directly.
37 Creates a dummy stream object. It doesn't do anything.
44 virtual ~wxStreamBase();
47 This function returns the last error.
49 wxStreamError
GetLastError() const;
52 Returns the length of the stream in bytes. If the length cannot be
53 determined (this is always the case for socket streams for example),
54 returns ::wxInvalidOffset.
58 virtual wxFileOffset
GetLength() const;
61 This function returns the size of the stream.
62 For example, for a file it is the size of the file.
65 There are streams which do not have size by definition, such as socket
66 streams. In that cases, GetSize() returns 0 so you should always test its
69 virtual size_t GetSize() const;
72 Returns @true if no error occurred on the stream.
76 virtual bool IsOk() const;
79 Returns @true if the streams supports seeking to arbitrary offsets.
81 virtual bool IsSeekable() const;
87 It is called when the stream needs to change the current position.
92 One of the ::wxSeekMode enumeration values.
94 @return The new stream position or ::wxInvalidOffset on error.
96 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
100 It is called when the stream needs to know the real position.
102 @return The current stream position.
104 virtual wxFileOffset
OnSysTell() const;
108 @class wxStreamBuffer
110 wxStreamBuffer is a cache manager for wxStreamBase: it manages a stream buffer
113 Each stream always has one autoinitialized stream buffer, but you may
114 attach more of them to the same stream.
119 @see wxStreamBase, @ref overview_stream
126 Constructor, creates a new stream buffer using @a stream as a parent stream
127 and mode as the IO mode.
132 Can be: wxStreamBuffer::read, wxStreamBuffer::write, wxStreamBuffer::read_write.
134 One stream can have many stream buffers but only one is used internally
135 to pass IO call (e.g. wxInputStream::Read() -> wxStreamBuffer::Read()),
136 but you can call directly wxStreamBuffer::Read without any problems.
137 Note that all errors and messages linked to the stream are stored in the
138 stream, not the stream buffers:
141 streambuffer.Read(...);
142 streambuffer2.Read(...);
143 // This call erases previous error messages set by 'streambuffer'
144 // assuming that both instances are stream buffers for the same stream
149 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
152 Constructor for an input buffer of the specified size.
154 Using it is equivalent to using the constructor above with read mode
155 and calling SetBufferIO() but is more convenient.
159 wxStreamBuffer(wxInputStream
& stream
, size_t bufsize
);
162 Constructor for an output buffer of the specified size.
164 Using it is equivalent to using the constructor above with write mode
165 and calling SetBufferIO() but is more convenient.
169 wxStreamBuffer(wxOutputStream
& stream
, size_t bufsize
);
172 Constructor; creates a new empty stream buffer which won't flush any data
173 to a stream. mode specifies the type of the buffer (read, write, read_write).
175 This stream buffer has the advantage to be stream independent and to work
176 only on memory buffers but it is still compatible with the rest of the
177 wxStream classes. You can write, read to this special stream and it will
178 grow (if it is allowed by the user) its internal buffer.
179 Briefly, it has all functionality of a "normal" stream.
182 The "read_write" mode doesn't currently work for standalone stream buffers.
186 wxStreamBuffer(BufMode mode
);
191 This method initializes the stream buffer with the data of the specified
192 stream buffer. The new stream buffer has the same attributes, size, position
193 and they share the same buffer. This will cause problems if the stream to
194 which the stream buffer belong is destroyed and the newly cloned stream
195 buffer continues to be used, trying to call functions in the (destroyed)
196 stream. It is advised to use this feature only in very local area of the
199 wxStreamBuffer(const wxStreamBuffer
& buffer
);
203 It finalizes all IO calls and frees all internal buffers if necessary.
213 Toggles the fixed flag. Usually this flag is toggled at the same time as
214 @e flushable. This flag allows (when it has the @false value) or forbids
215 (when it has the @true value) the stream buffer to resize dynamically the
220 void Fixed(bool fixed
);
223 Flushes the IO buffer.
228 Toggles the flushable flag.
229 If @a flushable is disabled, no data are sent to the parent stream.
231 void Flushable(bool flushable
);
234 Returns a pointer on the end of the stream buffer.
236 void* GetBufferEnd() const;
239 Returns a pointer on the current position of the stream buffer.
241 void* GetBufferPos() const;
244 Returns the size of the buffer.
246 size_t GetBufferSize() const;
249 Returns a pointer on the start of the stream buffer.
251 void* GetBufferStart() const;
254 Gets a single char from the stream buffer. It acts like the Read() call.
257 You aren't directly notified if an error occurred during the IO call.
261 virtual char GetChar();
264 Returns the amount of available data in the buffer.
266 size_t GetDataLeft();
269 Returns the current position (counted in bytes) in the stream buffer.
271 size_t GetIntPosition() const;
274 Returns the amount of bytes read during the last IO call to the parent stream.
276 size_t GetLastAccess() const;
279 Puts a single char to the stream buffer.
282 You aren't directly notified if an error occurred during the IO call.
286 virtual void PutChar(char c
);
289 Reads a block of the specified size and stores the data in buffer.
290 This function tries to read from the buffer first and if more data has
291 been requested, reads more data from the associated stream and updates
292 the buffer accordingly until all requested data is read.
294 @return It returns the size of the data read. If the returned size is
295 different of the specified size, an error has occurred and
296 should be tested using GetLastError().
298 virtual size_t Read(void* buffer
, size_t size
);
301 Copies data to @a buffer.
302 The function returns when @a buffer is full or when there isn't
303 any more data in the current buffer.
307 size_t Read(wxStreamBuffer
* buffer
);
310 Resets to the initial state variables concerning the buffer.
315 Changes the current position.
316 Parameter @a mode may be one of the following:
318 - @b wxFromStart: The position is counted from the start of the stream.
319 - @b wxFromCurrent: The position is counted from the current position of the stream.
320 - @b wxFromEnd: The position is counted from the end of the stream.
322 @return Upon successful completion, it returns the new offset as
323 measured in bytes from the beginning of the stream.
324 Otherwise, it returns ::wxInvalidOffset.
326 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
329 Specifies which pointers to use for stream buffering.
330 You need to pass a pointer on the start of the buffer end and another
331 on the end. The object will use this buffer to cache stream data.
332 It may be used also as a source/destination buffer when you create an
333 empty stream buffer (See wxStreamBuffer::wxStreamBuffer).
336 When you use this function, you will have to destroy the IO buffers
337 yourself after the stream buffer is destroyed or don't use it anymore.
338 In the case you use it with an empty buffer, the stream buffer will not
339 resize it when it is full.
341 @see wxStreamBuffer(), Fixed(), Flushable()
343 void SetBufferIO(void* start
, void* end
, bool takeOwnership
= false);
346 Destroys or invalidates the previous IO buffer and allocates a new one of the
350 All previous pointers aren't valid anymore.
353 The created IO buffer is growable by the object.
355 @see Fixed(), Flushable()
357 void SetBufferIO(size_t bufsize
);
360 Sets the current position (in bytes) in the stream buffer.
363 Since it is a very low-level function, there is no check on the position:
364 specifying an invalid position can induce unexpected results.
366 void SetIntPosition(size_t pos
);
369 Returns the parent stream of the stream buffer.
370 @deprecated use GetStream() instead
372 wxStreamBase
* Stream();
375 Gets the current position in the stream. This position is calculated from
376 the @e real position in the stream and from the internal buffer position: so
377 it gives you the position in the @e real stream counted from the start of
380 @return Returns the current position in the stream if possible,
381 ::wxInvalidOffset in the other case.
383 virtual wxFileOffset
Tell() const;
386 Truncates the buffer to the current position.
388 @note Truncate() cannot be used to enlarge the buffer. This is
389 usually not needed since the buffer expands automatically.
394 Writes a block of the specified size using data of buffer.
395 The data are cached in a buffer before being sent in one block to the stream.
397 virtual size_t Write(const void* buffer
, size_t size
);
402 size_t Write(wxStreamBuffer
* buffer
);
408 @class wxOutputStream
410 wxOutputStream is an abstract base class which may not be used directly.
411 It is the base class of all streams which provide a Write() function,
412 i.e. which can be used to output data (e.g. to a file, to a socket, etc).
414 If you want to create your own output stream, you'll need to derive from this
415 class and implement the protected OnSysWrite() function only.
420 class wxOutputStream
: public wxStreamBase
424 Creates a dummy wxOutputStream object.
431 virtual ~wxOutputStream();
434 Closes the stream, returning @false if an error occurs.
435 The stream is closed implicitly in the destructor if Close() is not
438 If this stream wraps another stream or some other resource such
439 as a file, then the underlying resource is closed too if it is owned
440 by this stream, or left open otherwise.
442 virtual bool Close();
445 Returns the number of bytes written during the last Write().
446 It may return 0 even if there is no error on the stream if it is
447 only temporarily impossible to write to it.
449 virtual size_t LastWrite() const;
452 Puts the specified character in the output queue and increments the
458 Changes the stream current position.
463 One of wxFromStart, wxFromEnd, wxFromCurrent.
465 @return The new stream position or ::wxInvalidOffset on error.
467 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
470 Returns the current stream position.
472 virtual wxFileOffset
TellO() const;
475 Writes up to the specified amount of bytes using the data of buffer.
476 Note that not all data can always be written so you must check the number
477 of bytes really written to the stream using LastWrite() when this function
480 In some cases (for example a write end of a pipe which is currently full)
481 it is even possible that there is no errors and zero bytes have been written.
482 This function returns a reference on the current object, so the user can
483 test any states of the stream right away.
485 virtual wxOutputStream
& Write(const void* buffer
, size_t size
);
488 Reads data from the specified input stream and stores them
489 in the current stream. The data is read until an error is raised
490 by one of the two streams.
492 wxOutputStream
& Write(wxInputStream
& stream_in
);
496 Internal function. It is called when the stream wants to write data of the
497 specified size @a bufsize into the given @a buffer.
499 It should return the size that was actually wrote (which maybe zero if
500 @a bufsize is zero or if an error occurred; in this last case the internal
501 variable @c m_lasterror should be appropriately set).
503 size_t OnSysWrite(const void* buffer
, size_t bufsize
);
510 wxInputStream is an abstract base class which may not be used directly.
511 It is the base class of all streams which provide a Read() function,
512 i.e. which can be used to read data from a source (e.g. a file, a socket, etc).
514 If you want to create your own input stream, you'll need to derive from this
515 class and implement the protected OnSysRead() function only.
520 class wxInputStream
: public wxStreamBase
524 Creates a dummy input stream.
531 virtual ~wxInputStream();
534 Returns @true if some data is available in the stream right now, so that
535 calling Read() wouldn't block.
537 virtual bool CanRead() const;
540 Returns @true after an attempt has been made to read past the end of the
543 virtual bool Eof() const;
546 Returns the first character in the input queue and removes it,
547 blocking until it appears if necessary.
549 On success returns a value between 0 - 255; on end of file returns @c wxEOF.
554 Returns the last number of bytes read.
556 virtual size_t LastRead() const;
559 Returns the first character in the input queue without removing it.
564 Reads the specified amount of bytes and stores the data in buffer.
565 To check if the call was successfull you must use LastRead() to check
566 if this call did actually read @a size bytes (if it didn't, GetLastError()
567 should return a meaningful value).
570 The buffer absolutely needs to have at least the specified size.
572 @return This function returns a reference on the current object, so the
573 user can test any states of the stream right away.
575 virtual wxInputStream
& Read(void* buffer
, size_t size
);
578 Reads data from the input queue and stores it in the specified output stream.
579 The data is read until an error is raised by one of the two streams.
581 @return This function returns a reference on the current object, so the
582 user can test any states of the stream right away.
584 wxInputStream
& Read(wxOutputStream
& stream_out
);
587 Changes the stream current position.
592 One of wxFromStart, wxFromEnd, wxFromCurrent.
594 @return The new stream position or ::wxInvalidOffset on error.
596 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
599 Returns the current stream position.
601 virtual wxFileOffset
TellI() const;
604 This function is only useful in read mode.
605 It is the manager of the "Write-Back" buffer. This buffer acts like a
606 temporary buffer where data which has to be read during the next read IO
607 call are put. This is useful when you get a big block of data which you
608 didn't want to read: you can replace them at the top of the input queue
611 Be very careful about this call in connection with calling SeekI() on
612 the same stream. Any call to SeekI() will invalidate any previous call
613 to this method (otherwise you could SeekI() to one position, "unread" a
614 few bytes there, SeekI() to another position and data would be either
617 @return Returns the amount of bytes saved in the Write-Back buffer.
619 size_t Ungetch(const void* buffer
, size_t size
);
622 This function acts like the previous one except that it takes only one
623 character: it is sometimes shorter to use than the generic function.
625 bool Ungetch(char c
);
630 Internal function. It is called when the stream wants to read data of the
631 specified size @a bufsize and wants it to be placed inside @a buffer.
633 It should return the size that was actually read or zero if EOF has been
634 reached or an error occurred (in this last case the internal @c m_lasterror
635 variable should be set accordingly as well).
637 size_t OnSysRead(void* buffer
, size_t bufsize
);
644 @class wxCountingOutputStream
646 wxCountingOutputStream is a specialized output stream which does not write any
647 data anywhere, instead it counts how many bytes would get written if this were a
648 normal stream. This can sometimes be useful or required if some data gets
649 serialized to a stream or compressed by using stream compression and thus the
650 final size of the stream cannot be known other than pretending to write the stream.
651 One case where the resulting size would have to be known is if the data has
652 to be written to a piece of memory and the memory has to be allocated before
653 writing to it (which is probably always the case when writing to a memory stream).
658 class wxCountingOutputStream
: public wxOutputStream
662 Creates a wxCountingOutputStream object.
664 wxCountingOutputStream();
669 virtual ~wxCountingOutputStream();
672 Returns the current size of the stream.
674 size_t GetSize() const;
679 @class wxBufferedInputStream
681 This stream acts as a cache. It caches the bytes read from the specified
682 input stream (see wxFilterInputStream).
683 It uses wxStreamBuffer and sets the default in-buffer size to 1024 bytes.
684 This class may not be used without some other stream to read the data
685 from (such as a file stream or a memory stream).
690 @see wxStreamBuffer, wxInputStream, wxBufferedOutputStream
692 class wxBufferedInputStream
: public wxFilterInputStream
696 Constructor using the provided buffer or default.
699 The associated low-level stream.
701 The buffer to use if non-@NULL. Notice that the ownership of this
702 buffer is taken by the stream, i.e. it will delete it. If this
703 parameter is @NULL a default 1KB buffer is used.
705 wxBufferedInputStream(wxInputStream
& stream
,
706 wxStreamBuffer
*buffer
= NULL
);
709 Constructor allowing to specify the size of the buffer.
711 This is just a more convenient alternative to creating a wxStreamBuffer
712 of the given size and using the other overloaded constructor of this
716 The associated low-level stream.
718 The size of the buffer, in bytes.
722 wxBufferedInputStream(wxInputStream
& stream
, size_t bufsize
);
727 virtual ~wxBufferedInputStream();
734 Enumeration values used by wxFilterClassFactory.
736 enum wxStreamProtocolType
738 wxSTREAM_PROTOCOL
, //!< wxFileSystem protocol (should be only one).
739 wxSTREAM_MIMETYPE
, //!< MIME types the stream handles.
740 wxSTREAM_ENCODING
, //!< The HTTP Content-Encodings the stream handles.
741 wxSTREAM_FILEEXT
//!< File extensions the stream handles.
745 @class wxFilterClassFactory
747 Allows the creation of filter streams to handle compression formats such
750 For example, given a filename you can search for a factory that will
751 handle it and create a stream to decompress it:
754 factory = wxFilterClassFactory::Find(filename, wxSTREAM_FILEEXT);
756 stream = factory->NewStream(new wxFFileInputStream(filename));
759 wxFilterClassFactory::Find can also search for a factory by MIME type,
760 HTTP encoding or by wxFileSystem protocol.
761 The available factories can be enumerated using wxFilterClassFactory::GetFirst()
762 and wxFilterClassFactory::GetNext().
767 @see wxFilterInputStream, wxFilterOutputStream, wxArchiveClassFactory,
768 @ref overview_archive
770 class wxFilterClassFactory
: public wxObject
774 Returns @true if this factory can handle the given protocol, MIME type, HTTP
775 encoding or file extension.
777 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
778 can be a complete filename rather than just an extension.
780 bool CanHandle(const wxString
& protocol
,
781 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const;
784 A static member that finds a factory that can handle a given protocol, MIME
785 type, HTTP encoding or file extension. Returns a pointer to the class
786 factory if found, or @NULL otherwise.
787 It does not give away ownership of the factory.
789 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
790 can be a complete filename rather than just an extension.
792 static const wxFilterClassFactory
* Find(const wxString
& protocol
,
793 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
);
797 GetFirst and GetNext can be used to enumerate the available factories.
798 For example, to list them:
802 const wxFilterClassFactory *factory = wxFilterClassFactory::GetFirst();
805 list << factory->GetProtocol() << _T("\n");
806 factory = factory->GetNext();
810 GetFirst()/GetNext() return a pointer to a factory or @NULL if no more
811 are available. They do not give away ownership of the factory.
813 static const wxFilterClassFactory
* GetFirst();
814 const wxFilterClassFactory
* GetNext() const;
818 Returns the wxFileSystem protocol supported by this factory.
819 Equivalent to @code wxString(*GetProtocols()) @endcode.
821 wxString
GetProtocol() const;
824 Returns the protocols, MIME types, HTTP encodings or file extensions
825 supported by this factory, as an array of null terminated strings.
826 It does not give away ownership of the array or strings.
828 For example, to list the file extensions a factory supports:
832 const wxChar *const *p;
834 for (p = factory->GetProtocols(wxSTREAM_FILEEXT); *p; p++)
835 list << *p << _T("\n");
838 virtual const wxChar
* const* GetProtocols(wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const = 0;
842 Create a new input or output stream to decompress or compress a given stream.
844 If the parent stream is passed as a pointer then the new filter stream
845 takes ownership of it. If it is passed by reference then it does not.
847 virtual wxFilterInputStream
* NewStream(wxInputStream
& stream
) const = 0;
848 virtual wxFilterOutputStream
* NewStream(wxOutputStream
& stream
) const = 0;
849 virtual wxFilterInputStream
* NewStream(wxInputStream
* stream
) const = 0;
850 virtual wxFilterOutputStream
* NewStream(wxOutputStream
* stream
) const = 0;
854 Remove the file extension of @a location if it is one of the file
855 extensions handled by this factory.
857 wxString
PopExtension(const wxString
& location
) const;
860 Adds this class factory to the list returned by GetFirst()/GetNext().
862 It is not necessary to do this to use the filter streams. It is usually
863 used when implementing streams, typically the implementation will
864 add a static instance of its factory class.
866 It can also be used to change the order of a factory already in the list,
867 bringing it to the front. This isn't a thread safe operation so can't be
868 done when other threads are running that will be using the list.
870 The list does not take ownership of the factory.
875 Removes this class factory from the list returned by GetFirst()/GetNext().
876 Removing from the list isn't a thread safe operation so can't be done
877 when other threads are running that will be using the list.
879 The list does not own the factories, so removing a factory does not delete it.
887 @class wxFilterOutputStream
889 A filter stream has the capability of a normal stream but it can be placed
890 on top of another stream. So, for example, it can compress, encrypt the data
891 which are passed to it and write them to another stream.
894 The use of this class is exactly the same as of wxOutputStream.
895 Only a constructor differs and it is documented below.
900 @see wxFilterClassFactory, wxFilterInputStream
902 class wxFilterOutputStream
: public wxOutputStream
907 Initializes a "filter" stream.
909 If the parent stream is passed as a pointer then the new filter stream
910 takes ownership of it. If it is passed by reference then it does not.
912 wxFilterOutputStream(wxOutputStream
& stream
);
913 wxFilterOutputStream(wxOutputStream
* stream
);
920 @class wxFilterInputStream
922 A filter stream has the capability of a normal stream but it can be placed on
923 top of another stream. So, for example, it can uncompress or decrypt the data which
924 are read from another stream and pass it to the requester.
927 The interface of this class is the same as that of wxInputStream.
928 Only a constructor differs and it is documented below.
933 @see wxFilterClassFactory, wxFilterOutputStream
935 class wxFilterInputStream
: public wxInputStream
940 Initializes a "filter" stream.
942 If the parent stream is passed as a pointer then the new filter stream
943 takes ownership of it. If it is passed by reference then it does not.
945 wxFilterInputStream(wxInputStream
& stream
);
946 wxFilterInputStream(wxInputStream
* stream
);
953 @class wxBufferedOutputStream
955 This stream acts as a cache. It caches the bytes to be written to the specified
956 output stream (See wxFilterOutputStream). The data is only written when the
957 cache is full, when the buffered stream is destroyed or when calling SeekO().
959 This class may not be used without some other stream to write the data
960 to (such as a file stream or a memory stream).
965 @see wxStreamBuffer, wxOutputStream
967 class wxBufferedOutputStream
: public wxFilterOutputStream
971 Constructor using the provided buffer or default.
974 The associated low-level stream.
976 The buffer to use if non-@NULL. Notice that the ownership of this
977 buffer is taken by the stream, i.e. it will delete it. If this
978 parameter is @NULL a default 1KB buffer is used.
980 wxBufferedOutputStream(wxOutputStream
& stream
,
981 wxStreamBuffer
*buffer
= NULL
);
984 Constructor allowing to specify the size of the buffer.
986 This is just a more convenient alternative to creating a wxStreamBuffer
987 of the given size and using the other overloaded constructor of this
991 The associated low-level stream.
993 The size of the buffer, in bytes.
997 wxBufferedOutputStream(wxOutputStream
& stream
, size_t bufsize
);
1000 Destructor. Calls Sync() and destroys the internal buffer.
1002 virtual ~wxBufferedOutputStream();
1005 Calls Sync() and changes the stream position.
1007 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
1010 Flushes the buffer and calls Sync() on the parent stream.
1012 virtual void Sync();