1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStreamBase and its derived classes
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxCountingOutputStream
12 wxCountingOutputStream is a specialized output stream which does not write any
13 data anywhere, instead it counts how many bytes would get written if this were a
14 normal stream. This can sometimes be useful or required if some data gets
15 serialized to a stream or compressed by using stream compression and thus the
16 final size of the stream cannot be known other than pretending to write the stream.
17 One case where the resulting size would have to be known is if the data has
18 to be written to a piece of memory and the memory has to be allocated before
19 writing to it (which is probably always the case when writing to a memory stream).
24 class wxCountingOutputStream
: public wxOutputStream
28 Creates a wxCountingOutputStream object.
30 wxCountingOutputStream();
35 virtual ~wxCountingOutputStream();
38 Returns the current size of the stream.
40 size_t GetSize() const;
46 @class wxBufferedInputStream
48 This stream acts as a cache. It caches the bytes read from the specified
49 input stream (see wxFilterInputStream).
50 It uses wxStreamBuffer and sets the default in-buffer size to 1024 bytes.
51 This class may not be used without some other stream to read the data
52 from (such as a file stream or a memory stream).
57 @see wxStreamBuffer, wxInputStream, wxBufferedOutputStream
59 class wxBufferedInputStream
: public wxFilterInputStream
63 Constructor using the provided buffer or default.
66 The associated low-level stream.
68 The buffer to use if non-@NULL. Notice that the ownership of this
69 buffer is taken by the stream, i.e. it will delete it. If this
70 parameter is @NULL a default 1KB buffer is used.
72 wxBufferedInputStream(wxInputStream
& stream
,
73 wxStreamBuffer
*buffer
= NULL
);
76 Constructor allowing to specify the size of the buffer.
78 This is just a more convenient alternative to creating a wxStreamBuffer
79 of the given size and using the other overloaded constructor of this
83 The associated low-level stream.
85 The size of the buffer, in bytes.
89 wxBufferedInputStream(wxInputStream
& stream
, size_t bufsize
);
94 virtual ~wxBufferedInputStream();
100 @class wxStreamBuffer
102 wxStreamBuffer is a cache manager for wxStreamBase: it manages a stream buffer
105 Each stream always has one autoinitialized stream buffer, but you may
106 attach more of them to the same stream.
111 @see wxStreamBase, @ref overview_stream
118 Constructor, creates a new stream buffer using @a stream as a parent stream
119 and mode as the IO mode.
124 Can be: wxStreamBuffer::read, wxStreamBuffer::write, wxStreamBuffer::read_write.
126 One stream can have many stream buffers but only one is used internally
127 to pass IO call (e.g. wxInputStream::Read() -> wxStreamBuffer::Read()),
128 but you can call directly wxStreamBuffer::Read without any problems.
129 Note that all errors and messages linked to the stream are stored in the
130 stream, not the stream buffers:
133 streambuffer.Read(...);
134 streambuffer2.Read(...);
135 // This call erases previous error messages set by 'streambuffer'
136 // assuming that both instances are stream buffers for the same stream
141 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
144 Constructor for an input buffer of the specified size.
146 Using it is equivalent to using the constructor above with read mode
147 and calling SetBufferIO() but is more convenient.
151 wxStreamBuffer(wxInputStream
& stream
, size_t bufsize
);
154 Constructor for an output buffer of the specified size.
156 Using it is equivalent to using the constructor above with write mode
157 and calling SetBufferIO() but is more convenient.
161 wxStreamBuffer(wxOutputStream
& stream
, size_t bufsize
);
164 Constructor; creates a new empty stream buffer which won't flush any data
165 to a stream. mode specifies the type of the buffer (read, write, read_write).
167 This stream buffer has the advantage to be stream independent and to work
168 only on memory buffers but it is still compatible with the rest of the
169 wxStream classes. You can write, read to this special stream and it will
170 grow (if it is allowed by the user) its internal buffer.
171 Briefly, it has all functionality of a "normal" stream.
174 The "read_write" mode doesn't currently work for standalone stream buffers.
178 wxStreamBuffer(BufMode mode
);
183 This method initializes the stream buffer with the data of the specified
184 stream buffer. The new stream buffer has the same attributes, size, position
185 and they share the same buffer. This will cause problems if the stream to
186 which the stream buffer belong is destroyed and the newly cloned stream
187 buffer continues to be used, trying to call functions in the (destroyed)
188 stream. It is advised to use this feature only in very local area of the
191 wxStreamBuffer(const wxStreamBuffer
& buffer
);
195 It finalizes all IO calls and frees all internal buffers if necessary.
205 Toggles the fixed flag. Usually this flag is toggled at the same time as
206 @e flushable. This flag allows (when it has the @false value) or forbids
207 (when it has the @true value) the stream buffer to resize dynamically the
212 void Fixed(bool fixed
);
215 Flushes the IO buffer.
220 Toggles the flushable flag.
221 If @a flushable is disabled, no data are sent to the parent stream.
223 void Flushable(bool flushable
);
226 Returns a pointer on the end of the stream buffer.
228 void* GetBufferEnd() const;
231 Returns a pointer on the current position of the stream buffer.
233 void* GetBufferPos() const;
236 Returns the size of the buffer.
238 size_t GetBufferSize() const;
241 Returns a pointer on the start of the stream buffer.
243 void* GetBufferStart() const;
246 Gets a single char from the stream buffer. It acts like the Read() call.
249 You aren't directly notified if an error occurred during the IO call.
253 virtual char GetChar();
256 Returns the amount of available data in the buffer.
258 size_t GetDataLeft();
261 Returns the current position (counted in bytes) in the stream buffer.
263 size_t GetIntPosition() const;
266 Returns the amount of bytes read during the last IO call to the parent stream.
268 size_t GetLastAccess() const;
271 Puts a single char to the stream buffer.
274 You aren't directly notified if an error occurred during the IO call.
278 virtual void PutChar(char c
);
281 Reads a block of the specified size and stores the data in buffer.
282 This function tries to read from the buffer first and if more data has
283 been requested, reads more data from the associated stream and updates
284 the buffer accordingly until all requested data is read.
286 @return It returns the size of the data read. If the returned size is
287 different of the specified size, an error has occurred and
288 should be tested using GetLastError().
290 virtual size_t Read(void* buffer
, size_t size
);
293 Copies data to @a buffer.
294 The function returns when @a buffer is full or when there isn't
295 any more data in the current buffer.
299 size_t Read(wxStreamBuffer
* buffer
);
302 Resets to the initial state variables concerning the buffer.
307 Changes the current position.
308 Parameter @a mode may be one of the following:
310 - @b wxFromStart: The position is counted from the start of the stream.
311 - @b wxFromCurrent: The position is counted from the current position of the stream.
312 - @b wxFromEnd: The position is counted from the end of the stream.
314 @return Upon successful completion, it returns the new offset as
315 measured in bytes from the beginning of the stream.
316 Otherwise, it returns wxInvalidOffset.
318 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
321 Specifies which pointers to use for stream buffering.
322 You need to pass a pointer on the start of the buffer end and another
323 on the end. The object will use this buffer to cache stream data.
324 It may be used also as a source/destination buffer when you create an
325 empty stream buffer (See wxStreamBuffer::wxStreamBuffer).
328 When you use this function, you will have to destroy the IO buffers
329 yourself after the stream buffer is destroyed or don't use it anymore.
330 In the case you use it with an empty buffer, the stream buffer will not
331 resize it when it is full.
333 @see wxStreamBuffer(), Fixed(), Flushable()
335 void SetBufferIO(void* start
, void* end
, bool takeOwnership
= false);
338 Destroys or invalidates the previous IO buffer and allocates a new one of the
342 All previous pointers aren't valid anymore.
345 The created IO buffer is growable by the object.
347 @see Fixed(), Flushable()
349 void SetBufferIO(size_t bufsize
);
352 Sets the current position (in bytes) in the stream buffer.
355 Since it is a very low-level function, there is no check on the position:
356 specifying an invalid position can induce unexpected results.
358 void SetIntPosition(size_t pos
);
361 Returns the parent stream of the stream buffer.
362 @deprecated use GetStream() instead
364 wxStreamBase
* Stream();
367 Gets the current position in the stream. This position is calculated from
368 the @e real position in the stream and from the internal buffer position: so
369 it gives you the position in the @e real stream counted from the start of
372 @return Returns the current position in the stream if possible,
373 wxInvalidOffset in the other case.
375 virtual wxFileOffset
Tell() const;
378 Truncates the buffer to the current position.
380 @note Truncate() cannot be used to enlarge the buffer. This is
381 usually not needed since the buffer expands automatically.
386 Writes a block of the specified size using data of buffer.
387 The data are cached in a buffer before being sent in one block to the stream.
389 virtual size_t Write(const void* buffer
, size_t size
);
394 size_t Write(wxStreamBuffer
* buffer
);
400 @class wxOutputStream
402 wxOutputStream is an abstract base class which may not be used directly.
407 class wxOutputStream
: public wxStreamBase
411 Creates a dummy wxOutputStream object.
418 virtual ~wxOutputStream();
421 Closes the stream, returning @false if an error occurs.
422 The stream is closed implicitly in the destructor if Close() is not
425 If this stream wraps another stream or some other resource such
426 as a file, then the underlying resource is closed too if it is owned
427 by this stream, or left open otherwise.
429 virtual bool Close();
432 Returns the number of bytes written during the last Write().
433 It may return 0 even if there is no error on the stream if it is
434 only temporarily impossible to write to it.
436 virtual size_t LastWrite() const;
439 Puts the specified character in the output queue and increments the
445 Changes the stream current position.
450 One of wxFromStart, wxFromEnd, wxFromCurrent.
452 @return The new stream position or wxInvalidOffset on error.
454 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
457 Returns the current stream position.
459 virtual wxFileOffset
TellO() const;
462 Writes up to the specified amount of bytes using the data of buffer.
463 Note that not all data can always be written so you must check the number
464 of bytes really written to the stream using LastWrite() when this function
467 In some cases (for example a write end of a pipe which is currently full)
468 it is even possible that there is no errors and zero bytes have been written.
469 This function returns a reference on the current object, so the user can
470 test any states of the stream right away.
472 virtual wxOutputStream
& Write(const void* buffer
, size_t size
);
475 Reads data from the specified input stream and stores them
476 in the current stream. The data is read until an error is raised
477 by one of the two streams.
479 wxOutputStream
& Write(wxInputStream
& stream_in
);
484 Enumeration values used by wxFilterClassFactory.
486 enum wxStreamProtocolType
488 wxSTREAM_PROTOCOL
, //!< wxFileSystem protocol (should be only one).
489 wxSTREAM_MIMETYPE
, //!< MIME types the stream handles.
490 wxSTREAM_ENCODING
, //!< The HTTP Content-Encodings the stream handles.
491 wxSTREAM_FILEEXT
//!< File extensions the stream handles.
496 @class wxFilterClassFactory
498 Allows the creation of filter streams to handle compression formats such
501 For example, given a filename you can search for a factory that will
502 handle it and create a stream to decompress it:
505 factory = wxFilterClassFactory::Find(filename, wxSTREAM_FILEEXT);
507 stream = factory-NewStream(new wxFFileInputStream(filename));
510 wxFilterClassFactory::Find can also search for a factory by MIME type,
511 HTTP encoding or by wxFileSystem protocol.
512 The available factories can be enumerated using wxFilterClassFactory::GetFirst()
513 and wxFilterClassFactory::GetNext().
518 @see wxFilterInputStream, wxFilterOutputStream, wxArchiveClassFactory,
519 @ref overview_archive
521 class wxFilterClassFactory
: public wxObject
525 Returns @true if this factory can handle the given protocol, MIME type, HTTP
526 encoding or file extension.
528 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
529 can be a complete filename rather than just an extension.
531 bool CanHandle(const wxString
& protocol
,
532 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const;
535 A static member that finds a factory that can handle a given protocol, MIME
536 type, HTTP encoding or file extension. Returns a pointer to the class
537 factory if found, or @NULL otherwise.
538 It does not give away ownership of the factory.
540 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
541 can be a complete filename rather than just an extension.
543 static const wxFilterClassFactory
* Find(const wxString
& protocol
,
544 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
);
548 GetFirst and GetNext can be used to enumerate the available factories.
549 For example, to list them:
553 const wxFilterClassFactory *factory = wxFilterClassFactory::GetFirst();
556 list << factory->GetProtocol() << _T("\n");
557 factory = factory->GetNext();
561 GetFirst()/GetNext() return a pointer to a factory or @NULL if no more
562 are available. They do not give away ownership of the factory.
564 static const wxFilterClassFactory
* GetFirst() const;
565 const wxFilterClassFactory
* GetNext() const;
569 Returns the wxFileSystem protocol supported by this factory.
570 Equivalent to @code wxString(*GetProtocols()) @endcode.
572 wxString
GetProtocol() const;
575 Returns the protocols, MIME types, HTTP encodings or file extensions
576 supported by this factory, as an array of null terminated strings.
577 It does not give away ownership of the array or strings.
579 For example, to list the file extensions a factory supports:
583 const wxChar *const *p;
585 for (p = factory->GetProtocols(wxSTREAM_FILEEXT); *p; p++)
586 list << *p << _T("\n");
589 virtual const wxChar
* const* GetProtocols(wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const = 0;
593 Create a new input or output stream to decompress or compress a given stream.
595 If the parent stream is passed as a pointer then the new filter stream
596 takes ownership of it. If it is passed by reference then it does not.
598 wxFilterInputStream
* NewStream(wxInputStream
& stream
) const;
599 wxFilterOutputStream
* NewStream(wxOutputStream
& stream
) const;
600 wxFilterInputStream
* NewStream(wxInputStream
* stream
) const;
601 wxFilterOutputStream
* NewStream(wxOutputStream
* stream
) const;
605 Remove the file extension of @a location if it is one of the file
606 extensions handled by this factory.
608 wxString
PopExtension(const wxString
& location
) const;
611 Adds this class factory to the list returned by GetFirst()/GetNext().
613 It is not necessary to do this to use the filter streams. It is usually
614 used when implementing streams, typically the implementation will
615 add a static instance of its factory class.
617 It can also be used to change the order of a factory already in the list,
618 bringing it to the front. This isn't a thread safe operation so can't be
619 done when other threads are running that will be using the list.
621 The list does not take ownership of the factory.
626 Removes this class factory from the list returned by GetFirst()/GetNext().
627 Removing from the list isn't a thread safe operation so can't be done
628 when other threads are running that will be using the list.
630 The list does not own the factories, so removing a factory does not delete it.
638 @class wxFilterOutputStream
640 A filter stream has the capability of a normal stream but it can be placed
641 on top of another stream. So, for example, it can compress, encrypt the data
642 which are passed to it and write them to another stream.
645 The use of this class is exactly the same as of wxOutputStream.
646 Only a constructor differs and it is documented below.
651 @see wxFilterClassFactory, wxFilterInputStream
653 class wxFilterOutputStream
: public wxOutputStream
658 Initializes a "filter" stream.
660 If the parent stream is passed as a pointer then the new filter stream
661 takes ownership of it. If it is passed by reference then it does not.
663 wxFilterOutputStream(wxOutputStream
& stream
);
664 wxFilterOutputStream(wxOutputStream
* stream
);
671 @class wxFilterInputStream
673 A filter stream has the capability of a normal stream but it can be placed on
674 top of another stream. So, for example, it can uncompress or decrypt the data which
675 are read from another stream and pass it to the requester.
678 The interface of this class is the same as that of wxInputStream.
679 Only a constructor differs and it is documented below.
684 @see wxFilterClassFactory, wxFilterOutputStream
686 class wxFilterInputStream
: public wxInputStream
691 Initializes a "filter" stream.
693 If the parent stream is passed as a pointer then the new filter stream
694 takes ownership of it. If it is passed by reference then it does not.
696 wxFilterInputStream(wxInputStream
& stream
);
697 wxFilterInputStream(wxInputStream
* stream
);
704 @class wxBufferedOutputStream
706 This stream acts as a cache. It caches the bytes to be written to the specified
707 output stream (See wxFilterOutputStream). The data is only written when the
708 cache is full, when the buffered stream is destroyed or when calling SeekO().
710 This class may not be used without some other stream to write the data
711 to (such as a file stream or a memory stream).
716 @see wxStreamBuffer, wxOutputStream
718 class wxBufferedOutputStream
: public wxFilterOutputStream
722 Constructor using the provided buffer or default.
725 The associated low-level stream.
727 The buffer to use if non-@NULL. Notice that the ownership of this
728 buffer is taken by the stream, i.e. it will delete it. If this
729 parameter is @NULL a default 1KB buffer is used.
731 wxBufferedOutputStream(wxOutputStream
& stream
,
732 wxStreamBuffer
*buffer
= NULL
);
735 Constructor allowing to specify the size of the buffer.
737 This is just a more convenient alternative to creating a wxStreamBuffer
738 of the given size and using the other overloaded constructor of this
742 The associated low-level stream.
744 The size of the buffer, in bytes.
748 wxBufferedOutputStream(wxOutputStream
& stream
, size_t bufsize
);
751 Destructor. Calls Sync() and destroys the internal buffer.
753 virtual ~wxBufferedOutputStream();
756 Calls Sync() and changes the stream position.
758 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
761 Flushes the buffer and calls Sync() on the parent stream.
771 wxInputStream is an abstract base class which may not be used directly.
776 class wxInputStream
: public wxStreamBase
780 Creates a dummy input stream.
787 virtual ~wxInputStream();
790 Returns @true if some data is available in the stream right now, so that
791 calling Read() wouldn't block.
793 virtual bool CanRead() const;
796 Returns @true after an attempt has been made to read past the end of the
799 virtual bool Eof() const;
802 Returns the first character in the input queue and removes it,
803 blocking until it appears if necessary.
805 On success returns a value between 0 - 255; on end of file returns @c wxEOF.
810 Returns the last number of bytes read.
812 virtual size_t LastRead() const;
815 Returns the first character in the input queue without removing it.
820 Reads the specified amount of bytes and stores the data in buffer.
823 The buffer absolutely needs to have at least the specified size.
825 @return This function returns a reference on the current object, so the
826 user can test any states of the stream right away.
828 virtual wxInputStream
& Read(void* buffer
, size_t size
);
831 Reads data from the input queue and stores it in the specified output stream.
832 The data is read until an error is raised by one of the two streams.
834 @return This function returns a reference on the current object, so the
835 user can test any states of the stream right away.
837 wxInputStream
& Read(wxOutputStream
& stream_out
);
840 Changes the stream current position.
845 One of wxFromStart, wxFromEnd, wxFromCurrent.
847 @return The new stream position or wxInvalidOffset on error.
849 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
852 Returns the current stream position.
854 virtual wxFileOffset
TellI() const;
857 This function is only useful in read mode.
858 It is the manager of the "Write-Back" buffer. This buffer acts like a
859 temporary buffer where data which has to be read during the next read IO
860 call are put. This is useful when you get a big block of data which you
861 didn't want to read: you can replace them at the top of the input queue
864 Be very careful about this call in connection with calling SeekI() on
865 the same stream. Any call to SeekI() will invalidate any previous call
866 to this method (otherwise you could SeekI() to one position, "unread" a
867 few bytes there, SeekI() to another position and data would be either
870 @return Returns the amount of bytes saved in the Write-Back buffer.
872 size_t Ungetch(const void* buffer
, size_t size
);
875 This function acts like the previous one except that it takes only one
876 character: it is sometimes shorter to use than the generic function.
878 bool Ungetch(char c
);
883 These enumeration values are returned by various functions in the context
888 wxSTREAM_NO_ERROR
= 0, //!< No error occurred.
889 wxSTREAM_EOF
, //!< EOF reached in Read() or similar.
890 wxSTREAM_WRITE_ERROR
, //!< generic write error on the last write call.
891 wxSTREAM_READ_ERROR
//!< generic read error on the last read call.
897 This class is the base class of most stream related classes in wxWidgets.
898 It must not be used directly.
909 Creates a dummy stream object. It doesn't do anything.
916 virtual ~wxStreamBase();
919 This function returns the last error.
921 wxStreamError
GetLastError() const;
924 Returns the length of the stream in bytes. If the length cannot be
925 determined (this is always the case for socket streams for example),
926 returns @c wxInvalidOffset.
930 virtual wxFileOffset
GetLength() const;
933 This function returns the size of the stream.
934 For example, for a file it is the size of the file.
937 There are streams which do not have size by definition, such as socket
938 streams. In that cases, GetSize returns 0 so you should always test its
941 virtual size_t GetSize() const;
944 Returns @true if no error occurred on the stream.
948 virtual bool IsOk() const;
951 Returns @true if the streams supports seeking to arbitrary offsets.
953 virtual bool IsSeekable() const;
956 Internal function. It is called when the stream wants to read data of the
957 specified size. It should return the size that was actually read.
959 size_t OnSysRead(void* buffer
, size_t bufsize
);
964 size_t OnSysWrite(const void* buffer
, size_t bufsize
);
971 It is called when the stream needs to change the current position.
973 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
977 It is called when the stream needs to know the real position.
979 virtual wxFileOffset
OnSysTell() const;