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
64 If a non @NULL buffer is given to the stream, it will be deleted by it.
66 wxBufferedInputStream(wxInputStream
& stream
,
67 wxStreamBuffer
*buffer
= NULL
);
72 virtual ~wxBufferedInputStream();
80 @todo WRITE A DESCRIPTION
92 Constructor, creates a new stream buffer using @a stream as a parent stream
93 and mode as the IO mode.
98 Can be: wxStreamBuffer::read, wxStreamBuffer::write, wxStreamBuffer::read_write.
100 One stream can have many stream buffers but only one is used internally
101 to pass IO call (e.g. wxInputStream::Read() -> wxStreamBuffer::Read()),
102 but you can call directly wxStreamBuffer::Read without any problems.
103 Note that all errors and messages linked to the stream are stored in the
104 stream, not the stream buffers:
107 streambuffer.Read(...);
108 streambuffer2.Read(...); // This call erases previous error messages set by 'streambuffer'
113 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
116 Constructor; creates a new empty stream buffer which won't flush any data
117 to a stream. mode specifies the type of the buffer (read, write, read_write).
119 This stream buffer has the advantage to be stream independent and to work
120 only on memory buffers but it is still compatible with the rest of the
121 wxStream classes. You can write, read to this special stream and it will
122 grow (if it is allowed by the user) its internal buffer.
123 Briefly, it has all functionality of a "normal" stream.
126 The "read_write" mode doesn't currently work for standalone stream buffers.
130 wxStreamBuffer(BufMode mode
);
135 This method initializes the stream buffer with the data of the specified
136 stream buffer. The new stream buffer has the same attributes, size, position
137 and they share the same buffer. This will cause problems if the stream to
138 which the stream buffer belong is destroyed and the newly cloned stream
139 buffer continues to be used, trying to call functions in the (destroyed)
140 stream. It is advised to use this feature only in very local area of the
143 wxStreamBuffer(const wxStreamBuffer
& buffer
);
147 It finalizes all IO calls and frees all internal buffers if necessary.
157 Toggles the fixed flag. Usually this flag is toggled at the same time as
158 @e flushable. This flag allows (when it has the @false value) or forbids
159 (when it has the @true value) the stream buffer to resize dynamically the
164 void Fixed(bool fixed
);
167 Flushes the IO buffer.
172 Toggles the flushable flag.
173 If @a flushable is disabled, no data are sent to the parent stream.
175 void Flushable(bool flushable
);
178 Returns a pointer on the end of the stream buffer.
180 void* GetBufferEnd() const;
183 Returns a pointer on the current position of the stream buffer.
185 void* GetBufferPos() const;
188 Returns the size of the buffer.
190 size_t GetBufferSize() const;
193 Returns a pointer on the start of the stream buffer.
195 void* GetBufferStart() const;
198 Gets a single char from the stream buffer. It acts like the Read() call.
201 You aren't directly notified if an error occurred during the IO call.
205 virtual char GetChar();
208 Returns the amount of available data in the buffer.
210 size_t GetDataLeft();
213 Returns the current position (counted in bytes) in the stream buffer.
215 wxFileOffset
GetIntPosition() const;
218 Returns the amount of bytes read during the last IO call to the parent stream.
220 size_t GetLastAccess() const;
223 Puts a single char to the stream buffer.
226 You aren't directly notified if an error occurred during the IO call.
230 virtual void PutChar(char c
);
233 Reads a block of the specified size and stores the data in buffer.
234 This function tries to read from the buffer first and if more data has
235 been requested, reads more data from the associated stream and updates
236 the buffer accordingly until all requested data is read.
238 @return It returns the size of the data read. If the returned size is
239 different of the specified size, an error has occurred and
240 should be tested using GetLastError().
242 virtual size_t Read(void* buffer
, size_t size
);
245 Copies data to @a buffer.
246 The function returns when @a buffer is full or when there isn't
247 any more data in the current buffer.
251 Return value
size_t Read(wxStreamBuffer
* buffer
);
254 Resets to the initial state variables concerning the buffer.
259 Changes the current position.
260 Parameter @a mode may be one of the following:
262 - @b wxFromStart: The position is counted from the start of the stream.
263 - @b wxFromCurrent: The position is counted from the current position of the stream.
264 - @b wxFromEnd: The position is counted from the end of the stream.
266 @return Upon successful completion, it returns the new offset as
267 measured in bytes from the beginning of the stream.
268 Otherwise, it returns wxInvalidOffset.
270 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
273 Specifies which pointers to use for stream buffering.
274 You need to pass a pointer on the start of the buffer end and another
275 on the end. The object will use this buffer to cache stream data.
276 It may be used also as a source/destination buffer when you create an
277 empty stream buffer (See wxStreamBuffer::wxStreamBuffer).
280 When you use this function, you will have to destroy the IO buffers
281 yourself after the stream buffer is destroyed or don't use it anymore.
282 In the case you use it with an empty buffer, the stream buffer will not
283 resize it when it is full.
285 @see wxStreamBuffer(), Fixed(), Flushable()
287 void SetBufferIO(char* buffer_start
, char* buffer_end
);
290 Destroys or invalidates the previous IO buffer and allocates a new one of the
294 All previous pointers aren't valid anymore.
297 The created IO buffer is growable by the object.
299 @see Fixed(), Flushable()
301 void SetBufferIO(size_t bufsize
);
304 Sets the current position (in bytes) in the stream buffer.
307 Since it is a very low-level function, there is no check on the position:
308 specifying an invalid position can induce unexpected results.
310 void SetIntPosition(size_t pos
);
313 Returns the parent stream of the stream buffer.
315 wxStreamBase
* Stream();
318 Gets the current position in the stream. This position is calculated from
319 the @e real position in the stream and from the internal buffer position: so
320 it gives you the position in the @e real stream counted from the start of
323 @return Returns the current position in the stream if possible,
324 wxInvalidOffset in the other case.
326 virtual wxFileOffset
Tell() const;
329 Truncates the buffer to the current position.
331 @note Truncate() cannot be used to enlarge the buffer. This is
332 usually not needed since the buffer expands automatically.
337 Writes a block of the specified size using data of buffer.
338 The data are cached in a buffer before being sent in one block to the stream.
340 virtual size_t Write(const void* buffer
, size_t size
);
345 size_t Write(wxStreamBuffer
* buffer
);
351 @class wxOutputStream
353 wxOutputStream is an abstract base class which may not be used directly.
358 class wxOutputStream
: public wxStreamBase
362 Creates a dummy wxOutputStream object.
369 virtual ~wxOutputStream();
372 Closes the stream, returning @false if an error occurs.
373 The stream is closed implicitly in the destructor if Close() is not
376 If this stream wraps another stream or some other resource such
377 as a file, then the underlying resource is closed too if it is owned
378 by this stream, or left open otherwise.
380 virtual bool Close();
383 Returns the number of bytes written during the last Write().
384 It may return 0 even if there is no error on the stream if it is
385 only temporarily impossible to write to it.
387 virtual size_t LastWrite() const;
390 Puts the specified character in the output queue and increments the
396 Changes the stream current position.
401 One of wxFromStart, wxFromEnd, wxFromCurrent.
403 @return The new stream position or wxInvalidOffset on error.
405 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
408 Returns the current stream position.
410 virtual wxFileOffset
TellO() const;
413 Writes up to the specified amount of bytes using the data of buffer.
414 Note that not all data can always be written so you must check the number
415 of bytes really written to the stream using LastWrite() when this function
418 In some cases (for example a write end of a pipe which is currently full)
419 it is even possible that there is no errors and zero bytes have been written.
420 This function returns a reference on the current object, so the user can
421 test any states of the stream right away.
423 wxOutputStream
Write(const void* buffer
, size_t size
);
426 Reads data from the specified input stream and stores them
427 in the current stream. The data is read until an error is raised
428 by one of the two streams.
430 wxOutputStream
Write(wxInputStream
& stream_in
);
435 Enumeration values used by wxFilterClassFactory.
437 enum wxStreamProtocolType
439 wxSTREAM_PROTOCOL
, //!< wxFileSystem protocol (should be only one).
440 wxSTREAM_MIMETYPE
, //!< MIME types the stream handles.
441 wxSTREAM_ENCODING
, //!< The HTTP Content-Encodings the stream handles.
442 wxSTREAM_FILEEXT
//!< File extensions the stream handles.
447 @class wxFilterClassFactory
449 Allows the creation of filter streams to handle compression formats such
452 For example, given a filename you can search for a factory that will
453 handle it and create a stream to decompress it:
456 factory = wxFilterClassFactory::Find(filename, wxSTREAM_FILEEXT);
458 stream = factory-NewStream(new wxFFileInputStream(filename));
461 wxFilterClassFactory::Find can also search for a factory by MIME type,
462 HTTP encoding or by wxFileSystem protocol.
463 The available factories can be enumerated using wxFilterClassFactory::GetFirst()
464 and wxFilterClassFactory::GetNext().
469 @see wxFilterInputStream, wxFilterOutputStream, wxArchiveClassFactory,
470 @ref overview_archive
472 class wxFilterClassFactory
: public wxObject
476 Returns @true if this factory can handle the given protocol, MIME type, HTTP
477 encoding or file extension.
479 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
480 can be a complete filename rather than just an extension.
482 bool CanHandle(const wxString
& protocol
,
483 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const;
486 A static member that finds a factory that can handle a given protocol, MIME
487 type, HTTP encoding or file extension. Returns a pointer to the class
488 factory if found, or @NULL otherwise.
489 It does not give away ownership of the factory.
491 When using @c wxSTREAM_FILEEXT for the second parameter, the first parameter
492 can be a complete filename rather than just an extension.
494 static const wxFilterClassFactory
* Find(const wxString
& protocol
,
495 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
);
499 GetFirst and GetNext can be used to enumerate the available factories.
500 For example, to list them:
504 const wxFilterClassFactory *factory = wxFilterClassFactory::GetFirst();
507 list << factory->GetProtocol() << _T("\n");
508 factory = factory->GetNext();
512 GetFirst()/GetNext() return a pointer to a factory or @NULL if no more
513 are available. They do not give away ownership of the factory.
515 static const wxFilterClassFactory
* GetFirst() const;
516 const wxFilterClassFactory
* GetNext() const;
520 Returns the wxFileSystem protocol supported by this factory.
521 Equivalent to @code wxString(*GetProtocols()) @endcode.
523 wxString
GetProtocol() const;
526 Returns the protocols, MIME types, HTTP encodings or file extensions
527 supported by this factory, as an array of null terminated strings.
528 It does not give away ownership of the array or strings.
530 For example, to list the file extensions a factory supports:
534 const wxChar *const *p;
536 for (p = factory->GetProtocols(wxSTREAM_FILEEXT); *p; p++)
537 list << *p << _T("\n");
540 const wxChar
* const* GetProtocols(wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const;
544 Create a new input or output stream to decompress or compress a given stream.
546 If the parent stream is passed as a pointer then the new filter stream
547 takes ownership of it. If it is passed by reference then it does not.
549 wxFilterInputStream
* NewStream(wxInputStream
& stream
) const;
550 wxFilterOutputStream
* NewStream(wxOutputStream
& stream
) const;
551 wxFilterInputStream
* NewStream(wxInputStream
* stream
) const;
552 wxFilterOutputStream
* NewStream(wxOutputStream
* stream
) const;
556 Remove the file extension of @a location if it is one of the file
557 extensions handled by this factory.
559 wxString
PopExtension(const wxString
& location
) const;
562 Adds this class factory to the list returned by GetFirst()/GetNext().
564 It is not necessary to do this to use the filter streams. It is usually
565 used when implementing streams, typically the implementation will
566 add a static instance of its factory class.
568 It can also be used to change the order of a factory already in the list,
569 bringing it to the front. This isn't a thread safe operation so can't be
570 done when other threads are running that will be using the list.
572 The list does not take ownership of the factory.
577 Removes this class factory from the list returned by GetFirst()/GetNext().
578 Removing from the list isn't a thread safe operation so can't be done
579 when other threads are running that will be using the list.
581 The list does not own the factories, so removing a factory does not delete it.
589 @class wxFilterOutputStream
591 A filter stream has the capability of a normal stream but it can be placed
592 on top of another stream. So, for example, it can compress, encrypt the data
593 which are passed to it and write them to another stream.
596 The use of this class is exactly the same as of wxOutputStream.
597 Only a constructor differs and it is documented below.
602 @see wxFilterClassFactory, wxFilterInputStream
604 class wxFilterOutputStream
: public wxOutputStream
609 Initializes a "filter" stream.
611 If the parent stream is passed as a pointer then the new filter stream
612 takes ownership of it. If it is passed by reference then it does not.
614 wxFilterOutputStream(wxOutputStream
& stream
);
615 wxFilterOutputStream(wxOutputStream
* stream
);
622 @class wxFilterInputStream
624 A filter stream has the capability of a normal stream but it can be placed on
625 top of another stream. So, for example, it can uncompress or decrypt the data which
626 are read from another stream and pass it to the requester.
629 The interface of this class is the same as that of wxInputStream.
630 Only a constructor differs and it is documented below.
635 @see wxFilterClassFactory, wxFilterOutputStream
637 class wxFilterInputStream
: public wxInputStream
642 Initializes a "filter" stream.
644 If the parent stream is passed as a pointer then the new filter stream
645 takes ownership of it. If it is passed by reference then it does not.
647 wxFilterInputStream(wxInputStream
& stream
);
648 wxFilterInputStream(wxInputStream
* stream
);
655 @class wxBufferedOutputStream
657 This stream acts as a cache. It caches the bytes to be written to the specified
658 output stream (See wxFilterOutputStream). The data is only written when the
659 cache is full, when the buffered stream is destroyed or when calling SeekO().
661 This class may not be used without some other stream to write the data
662 to (such as a file stream or a memory stream).
667 @see wxStreamBuffer, wxOutputStream
669 class wxBufferedOutputStream
: public wxFilterOutputStream
673 @todo WRITE DESCRIPTION
675 wxBufferedOutputStream(wxOutputStream
& stream
,
676 wxStreamBuffer
*buffer
= NULL
);
678 Destructor. Calls Sync() and destroys the internal buffer.
680 virtual ~wxBufferedOutputStream();
683 Calls Sync() and changes the stream position.
685 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
)
688 Flushes the buffer and calls Sync() on the parent stream.
698 wxInputStream is an abstract base class which may not be used directly.
703 class wxInputStream
: public wxStreamBase
707 Creates a dummy input stream.
714 virtual ~wxInputStream();
717 Returns @true if some data is available in the stream right now, so that
718 calling Read() wouldn't block.
720 virtual bool CanRead() const;
723 Returns @true after an attempt has been made to read past the end of the
726 virtual bool Eof() const;
729 Returns the first character in the input queue and removes it,
730 blocking until it appears if necessary.
735 Returns the last number of bytes read.
737 virtual size_t LastRead() const;
740 Returns the first character in the input queue without removing it.
745 Reads the specified amount of bytes and stores the data in buffer.
748 The buffer absolutely needs to have at least the specified size.
750 @return This function returns a reference on the current object, so the
751 user can test any states of the stream right away.
753 wxInputStream
Read(void* buffer
, size_t size
);
756 Reads data from the input queue and stores it in the specified output stream.
757 The data is read until an error is raised by one of the two streams.
759 @return This function returns a reference on the current object, so the
760 user can test any states of the stream right away.
762 wxInputStream
& Read(wxOutputStream
& stream_out
);
765 Changes the stream current position.
770 One of wxFromStart, wxFromEnd, wxFromCurrent.
772 @return The new stream position or wxInvalidOffset on error.
774 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
777 Returns the current stream position.
779 virtual wxFileOffset
TellI() const;
782 This function is only useful in read mode.
783 It is the manager of the "Write-Back" buffer. This buffer acts like a
784 temporary buffer where data which has to be read during the next read IO
785 call are put. This is useful when you get a big block of data which you
786 didn't want to read: you can replace them at the top of the input queue
789 Be very careful about this call in connection with calling SeekI() on
790 the same stream. Any call to SeekI() will invalidate any previous call
791 to this method (otherwise you could SeekI() to one position, "unread" a
792 few bytes there, SeekI() to another position and data would be either
795 @return Returns the amount of bytes saved in the Write-Back buffer.
797 size_t Ungetch(const char* buffer
, size_t size
);
800 This function acts like the previous one except that it takes only one
801 character: it is sometimes shorter to use than the generic function.
803 Return value
bool Ungetch(char c
);
808 These enumeration values are returned by various functions in the context
813 wxSTREAM_NO_ERROR
= 0, //!< No error occurred.
814 wxSTREAM_EOF
, //!< EOF reached in Read() or similar.
815 wxSTREAM_WRITE_ERROR
, //!< generic write error on the last write call.
816 wxSTREAM_READ_ERROR
//!< generic read error on the last read call.
822 This class is the base class of most stream related classes in wxWidgets.
823 It must not be used directly.
834 Creates a dummy stream object. It doesn't do anything.
841 virtual ~wxStreamBase();
844 This function returns the last error.
846 wxStreamError
GetLastError() const;
849 Returns the length of the stream in bytes. If the length cannot be
850 determined (this is always the case for socket streams for example),
851 returns @c wxInvalidOffset.
855 virtual wxFileOffset
GetLength() const;
858 This function returns the size of the stream.
859 For example, for a file it is the size of the file.
862 There are streams which do not have size by definition, such as socket
863 streams. In that cases, GetSize returns 0 so you should always test its
866 virtual size_t GetSize() const;
869 Returns @true if no error occurred on the stream.
873 virtual bool IsOk() const;
876 Returns @true if the streams supports seeking to arbitrary offsets.
878 virtual bool IsSeekable() const;
881 Internal function. It is called when the stream wants to read data of the
882 specified size. It should return the size that was actually read.
884 size_t OnSysRead(void* buffer
, size_t bufsize
);
889 size_t OnSysWrite(const void* buffer
, size_t bufsize
);
896 It is called when the stream needs to change the current position.
898 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
902 It is called when the stream needs to know the real position.
904 virtual wxFileOffset
OnSysTell() const;