1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxCountingOutputStream
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxCountingOutputStream
12 wxCountingOutputStream is a specialized output stream which does not write any
14 instead it counts how many bytes would get written if this were a normal
16 can sometimes be useful or required if some data gets serialized to a stream or
18 by using stream compression and thus the final size of the stream cannot be
20 than pretending to write the stream. One case where the resulting size would
22 known is if the data has to be written to a piece of memory and the memory has
24 allocated before writing to it (which is probably always the case when writing
31 class wxCountingOutputStream
: public wxOutputStream
35 Creates a wxCountingOutputStream object.
37 wxCountingOutputStream();
42 ~wxCountingOutputStream();
45 Returns the current size of the stream.
47 size_t GetSize() const;
53 @class wxBufferedInputStream
55 This stream acts as a cache. It caches the bytes read from the specified
56 input stream (See wxFilterInputStream).
57 It uses wxStreamBuffer and sets the default in-buffer size to 1024 bytes.
58 This class may not be used without some other stream to read the data
59 from (such as a file stream or a memory stream).
64 @see wxStreamBuffer, wxInputStream, wxBufferedOutputStream
66 class wxBufferedInputStream
: public wxFilterInputStream
88 Constructor. It initializes the stream buffer with the data of the specified
89 stream buffer. The new stream buffer has the same attributes, size, position
90 and they share the same buffer. This will cause problems if the stream to
91 which the stream buffer belong is destroyed and the newly cloned stream
92 buffer continues to be used, trying to call functions in the (destroyed)
93 stream. It is advised to use this feature only in very local area of the
96 @see @ref setbufferio() wxStreamBuffer:SetBufferIO
98 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
99 wxStreamBuffer(BufMode mode
);
100 wxStreamBuffer(const wxStreamBuffer
& buffer
);
104 Destructor. It finalizes all IO calls and frees all internal buffers if
115 Toggles the fixed flag. Usually this flag is toggled at the same time as
116 @e flushable. This flag allows (when it has the @false value) or forbids
117 (when it has the @true value) the stream buffer to resize dynamically the IO
122 void Fixed(bool fixed
);
125 Flushes the IO buffer.
130 Toggles the flushable flag. If @a flushable is disabled, no data are sent
131 to the parent stream.
133 void Flushable(bool flushable
);
136 Returns a pointer on the end of the stream buffer.
138 void* GetBufferEnd() const;
141 Returns a pointer on the current position of the stream buffer.
143 void* GetBufferPos() const;
146 Returns the size of the buffer.
148 size_t GetBufferSize() const;
151 Returns a pointer on the start of the stream buffer.
153 void* GetBufferStart() const;
156 Gets a single char from the stream buffer. It acts like the Read call.
163 Returns the amount of available data in the buffer.
165 size_t GetDataLeft();
168 Returns the current position (counted in bytes) in the stream buffer.
170 off_t
GetIntPosition() const;
173 Returns the amount of bytes read during the last IO call to the parent stream.
175 size_t GetLastAccess() const;
178 Puts a single char to the stream buffer.
182 void PutChar(char c
);
186 Copies data to @e buffer. The function returns when @a buffer is full or when
188 any more data in the current buffer.
192 size_t Read(void* buffer
, size_t size
);
193 Return value
size_t Read(wxStreamBuffer
* buffer
);
197 Resets to the initial state variables concerning the buffer.
202 Changes the current position.
203 @a mode may be one of the following:
207 The position is counted from the start of the stream.
211 The position is counted from the current position of the stream.
215 The position is counted from the end of the stream.
217 @return Upon successful completion, it returns the new offset as
218 measured in bytes from the beginning of the stream.
219 Otherwise, it returns wxInvalidOffset.
221 off_t
Seek(off_t pos
, wxSeekMode mode
);
225 Destroys or invalidates the previous IO buffer and allocates a new one of the
228 @see Fixed(), Flushable()
230 void SetBufferIO(char* buffer_start
, char* buffer_end
);
232 wxStreamBuffer constructor
234 wxStreamBuffer::Fixed
236 wxStreamBuffer::Flushable
237 void SetBufferIO(size_t bufsize
);
241 Sets the current position (in bytes) in the stream buffer.
243 void SetIntPosition(size_t pos
);
246 Returns the parent stream of the stream buffer.
248 wxStreamBase
* Stream();
251 Gets the current position in the stream. This position is calculated from
252 the @e real position in the stream and from the internal buffer position: so
253 it gives you the position in the @e real stream counted from the start of
256 @return Returns the current position in the stream if possible,
257 wxInvalidOffset in the other case.
262 Truncates the buffer to the current position.
263 Note: Truncate() cannot be used to enlarge the buffer. This is
264 usually not needed since the buffer expands automatically.
272 size_t Write(const void* buffer
, size_t size
);
273 size_t Write(wxStreamBuffer
* buffer
);
280 @class wxOutputStream
282 wxOutputStream is an abstract base class which may not be used directly.
287 class wxOutputStream
: public wxStreamBase
291 Creates a dummy wxOutputStream object.
301 Closes the stream, returning @false if an error occurs. The
302 stream is closed implicitly in the destructor if Close() is not
304 If this stream wraps another stream or some other resource such
305 as a file, then the underlying resource is closed too if it is owned
306 by this stream, or left open otherwise.
311 Returns the number of bytes written during the last
312 Write(). It may return 0 even if there is no
313 error on the stream if it is only temporarily impossible to write to it.
315 size_t LastWrite() const;
318 Puts the specified character in the output queue and increments the
324 Changes the stream current position.
329 One of wxFromStart, wxFromEnd, wxFromCurrent.
331 @return The new stream position or wxInvalidOffset on error.
333 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
336 Returns the current stream position.
342 Reads data from the specified input stream and stores them
343 in the current stream. The data is read until an error is raised
344 by one of the two streams.
346 wxOutputStream
Write(const void* buffer
, size_t size
);
347 wxOutputStream
Write(wxInputStream
& stream_in
);
354 @class wxFilterClassFactory
356 Allows the creation of filter streams to handle compression formats such
359 For example, given a filename you can search for a factory that will
360 handle it and create a stream to decompress it:
363 factory = wxFilterClassFactory::Find(filename, wxSTREAM_FILEEXT);
365 stream = factory-NewStream(new wxFFileInputStream(filename));
368 wxFilterClassFactory::Find can also search
369 for a factory by MIME type, HTTP encoding or by wxFileSystem protocol.
370 The available factories can be enumerated
371 using @ref wxFilterClassFactory::getfirst "GetFirst() and GetNext".
376 @see wxFilterInputStream, wxFilterOutputStream, wxArchiveClassFactory, @ref
377 overview_wxarc "Archive formats such as zip"
379 class wxFilterClassFactory
: public wxObject
383 Returns @true if this factory can handle the given protocol, MIME type, HTTP
384 encoding or file extension.
385 When using wxSTREAM_FILEEXT for the second parameter, the first parameter
386 can be a complete filename rather than just an extension.
388 bool CanHandle(const wxString
& protocol
,
389 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const;
392 A static member that finds a factory that can handle a given protocol, MIME
393 type, HTTP encoding or file extension. Returns a pointer to the class
394 factory if found, or @NULL otherwise. It does not give away ownership of the
396 When using wxSTREAM_FILEEXT for the second parameter, the first parameter
397 can be a complete filename rather than just an extension.
399 static const wxFilterClassFactory
* Find(const wxString
& protocol
,
400 wxStreamProtocolType type
= wxSTREAM_PROTOCOL
);
404 GetFirst and GetNext can be used to enumerate the available factories.
405 For example, to list them:
407 GetFirst()/GetNext() return a pointer to a factory or @NULL if no more
408 are available. They do not give away ownership of the factory.
410 static const wxFilterClassFactory
* GetFirst() const;
411 const wxFilterClassFactory
* GetNext() const;
415 Returns the wxFileSystem protocol supported by this factory. Equivalent
416 to wxString(*GetProtcols()).
418 wxString
GetProtocol() const;
421 Returns the protocols, MIME types, HTTP encodings or file extensions
422 supported by this factory, as an array of null terminated strings. It does
423 not give away ownership of the array or strings.
424 For example, to list the file extensions a factory supports:
426 const wxChar
* const* GetProtocols(wxStreamProtocolType type
= wxSTREAM_PROTOCOL
) const;
430 Create a new input or output stream to decompress or compress a given stream.
431 If the parent stream is passed as a pointer then the new filter stream
432 takes ownership of it. If it is passed by reference then it does not.
434 wxFilterInputStream
* NewStream(wxInputStream
& stream
) const;
435 const wxFilterOutputStream
* NewStream(wxOutputStream
& stream
) const;
436 const wxFilterInputStream
* NewStream(wxInputStream
* stream
) const;
437 const wxFilterOutputStream
* NewStream(wxOutputStream
* stream
) const;
441 Remove the file extension of @a location if it is one of the file
442 extensions handled by this factory.
444 wxString
PopExtension(const wxString
& location
) const;
447 Adds this class factory to the list returned
448 by @ref getfirst() GetFirst()/GetNext.
449 It is not necessary to do this to use the filter streams. It is usually
450 used when implementing streams, typically the implementation will
451 add a static instance of its factory class.
452 It can also be used to change the order of a factory already in the list,
453 bringing it to the front. This isn't a thread safe operation
454 so can't be done when other threads are running that will be using the list.
455 The list does not take ownership of the factory.
460 Removes this class factory from the list returned
461 by @ref getfirst() GetFirst()/GetNext.
462 Removing from the list isn't a thread safe operation
463 so can't be done when other threads are running that will be using the list.
464 The list does not own the factories, so removing a factory does not delete it.
472 @class wxFilterOutputStream
474 A filter stream has the capability of a normal
475 stream but it can be placed on top of another stream. So, for example, it
476 can compress, encrypt the data which are passed to it and write them to another
482 @see wxFilterClassFactory, wxFilterInputStream
484 class wxFilterOutputStream
: public wxOutputStream
489 Initializes a "filter" stream.
490 If the parent stream is passed as a pointer then the new filter stream
491 takes ownership of it. If it is passed by reference then it does not.
493 wxFilterOutputStream(wxOutputStream
& stream
);
494 wxFilterOutputStream(wxOutputStream
* stream
);
501 @class wxFilterInputStream
503 A filter stream has the capability of a normal stream but it can be placed on
505 of another stream. So, for example, it can uncompress or decrypt the data which
507 from another stream and pass it to the requester.
512 @see wxFilterClassFactory, wxFilterOutputStream
514 class wxFilterInputStream
: public wxInputStream
519 Initializes a "filter" stream.
520 If the parent stream is passed as a pointer then the new filter stream
521 takes ownership of it. If it is passed by reference then it does not.
523 wxFilterInputStream(wxInputStream
& stream
);
524 wxFilterInputStream(wxInputStream
* stream
);
531 @class wxBufferedOutputStream
533 This stream acts as a cache. It caches the bytes to be written to the specified
534 output stream (See wxFilterOutputStream). The
535 data is only written when the cache is full, when the buffered stream is
536 destroyed or when calling SeekO().
538 This class may not be used without some other stream to write the data
539 to (such as a file stream or a memory stream).
544 @see wxStreamBuffer, wxOutputStream
546 class wxBufferedOutputStream
: public wxFilterOutputStream
550 Creates a buffered stream using a buffer of a default size of 1024 bytes for
552 the stream @e parent.
554 wxBufferedOutputStream(const wxOutputStream
& parent
);
557 Destructor. Calls Sync() and destroys the internal buffer.
559 ~wxBufferedOutputStream();
562 Calls Sync() and changes the stream position.
564 off_t
SeekO(off_t pos
, wxSeekMode mode
);
567 Flushes the buffer and calls Sync() on the parent stream.
577 wxInputStream is an abstract base class which may not be used directly.
582 class wxInputStream
: public wxStreamBase
586 Creates a dummy input stream.
596 Returns @true if some data is available in the stream right now, so that
597 calling Read() wouldn't block.
599 bool CanRead() const;
602 Returns @true after an attempt has been made to read past the end of the
608 Returns the first character in the input queue and removes it,
609 blocking until it appears if necessary.
614 Returns the last number of bytes read.
616 size_t LastRead() const;
619 Returns the first character in the input queue without removing it.
625 Reads data from the input queue and stores it in the specified output stream.
626 The data is read until an error is raised by one of the two streams.
628 @return This function returns a reference on the current object, so the
629 user can test any states of the stream right away.
631 wxInputStream
Read(void* buffer
, size_t size
);
633 This function returns a reference on the current object
, so the user can test
634 any states of the stream right away
.
635 wxInputStream
& Read(wxOutputStream
& stream_out
);
639 Changes the stream current position.
644 One of wxFromStart, wxFromEnd, wxFromCurrent.
646 @return The new stream position or wxInvalidOffset on error.
648 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
651 Returns the current stream position.
657 This function acts like the previous one except that it takes only one
658 character: it is sometimes shorter to use than the generic function.
660 size_t Ungetch(const char* buffer
, size_t size
);
661 Return value
bool Ungetch(char c
);
670 This class is the base class of most stream related classes in wxWidgets. It
672 not be used directly.
683 Creates a dummy stream object. It doesn't do anything.
693 This function returns the last error.
701 An End-Of-File occurred.
703 @b wxSTREAM_WRITE_ERROR
705 A generic error occurred on the last write call.
707 @b wxSTREAM_READ_ERROR
709 A generic error occurred on the last read call.
711 wxStreamError
GetLastError() const;
714 Returns the length of the stream in bytes. If the length cannot be determined
715 (this is always the case for socket streams for example), returns
720 wxFileOffset
GetLength() const;
724 This function returns the size of the stream. For example, for a file it is the
727 size_t GetSize() const;
730 Returns @true if no error occurred on the stream.
734 virtual bool IsOk() const;
737 Returns @true if the streams supports seeking to arbitrary offsets.
739 bool IsSeekable() const;
742 Internal function. It is called when the stream wants to read data of the
743 specified size. It should return the size that was actually read.
745 size_t OnSysRead(void* buffer
, size_t bufsize
);
748 Internal function. It is called when the stream needs to change the
751 off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
754 Internal function. Is is called when the stream needs to know the
757 off_t
OnSysTell() const;
762 size_t OnSysWrite(const void* buffer
, size_t bufsize
);