mstream.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: mstream.h
  3. // Purpose: interface of wxMemoryOutputStream, wxMemoryInputStream
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxMemoryOutputStream
  9. This class allows to use all methods taking a wxOutputStream reference to write
  10. to in-memory data.
  11. Example:
  12. @code
  13. wxMemoryOutputStream stream;
  14. if (!my_wxImage.SaveFile(stream))
  15. return;
  16. // now we can access the saved image bytes:
  17. wxStreamBuffer* theBuffer = stream.GetOutputStreamBuffer();
  18. unsigned char byte;
  19. if (theBuffer->Read(byte, 1) != 1)
  20. return;
  21. // ... do something with 'byte'...
  22. // remember that ~wxMemoryOutputStream will destroy the internal
  23. // buffer since we didn't provide our own when constructing it
  24. @endcode
  25. @library{wxbase}
  26. @category{streams}
  27. @see wxStreamBuffer
  28. */
  29. class wxMemoryOutputStream : public wxOutputStream
  30. {
  31. public:
  32. /**
  33. If @a data is @NULL, then it will initialize a new empty buffer which will
  34. grow if required.
  35. @warning
  36. If the buffer is created by wxMemoryOutputStream, it will be destroyed
  37. at the destruction of the stream.
  38. */
  39. wxMemoryOutputStream(void* data = NULL, size_t length = 0);
  40. /**
  41. Destructor.
  42. If the buffer wasn't provided by the user, it will be deleted here.
  43. */
  44. virtual ~wxMemoryOutputStream();
  45. /**
  46. Allows you to transfer data from the internal buffer of wxMemoryOutputStream
  47. to an external buffer. @a len specifies the size of the buffer.
  48. */
  49. size_t CopyTo(void* buffer, size_t len) const;
  50. /**
  51. Returns the pointer to the stream object used as an internal buffer
  52. for this stream.
  53. */
  54. wxStreamBuffer* GetOutputStreamBuffer() const;
  55. };
  56. /**
  57. @class wxMemoryInputStream
  58. This class allows to use all methods taking a wxInputStream reference to read
  59. in-memory data.
  60. Example:
  61. @code
  62. // we've got a block of memory containing a BMP image and we want
  63. // to use wxImage to load it:
  64. wxMemoryInputStream stream(my_memory_block, size);
  65. wxImage theBitmap;
  66. if (!theBitmap.LoadFile(stream, wxBITMAP_TYPE_BMP))
  67. return;
  68. // we can now safely delete the original memory buffer as the data
  69. // has been decoded by wxImage:
  70. delete [] my_memory_block;
  71. @endcode
  72. @library{wxbase}
  73. @category{streams}
  74. @see wxStreamBuffer, wxMemoryOutputStream
  75. */
  76. class wxMemoryInputStream : public wxInputStream
  77. {
  78. public:
  79. /**
  80. Initializes a new read-only memory stream which will use the specified
  81. buffer data of length len. The stream does not take ownership of the buffer,
  82. i.e. the buffer will not be deleted in its destructor.
  83. */
  84. wxMemoryInputStream(const void* data, size_t len);
  85. /**
  86. Creates a new read-only memory stream, initializing it with the data from
  87. the given output stream @a stream.
  88. */
  89. wxMemoryInputStream(const wxMemoryOutputStream& stream);
  90. /**
  91. Creates a new read-only memory stream, initializing it with the
  92. data from the given input stream @a stream.
  93. The @a len argument specifies the amount of data to read from the
  94. @a stream. Setting it to ::wxInvalidOffset means that the @a stream
  95. is to be read entirely (i.e. till the EOF is reached).
  96. */
  97. wxMemoryInputStream(wxInputStream& stream,
  98. wxFileOffset len = wxInvalidOffset);
  99. /**
  100. Destructor. Does NOT free the buffer provided in the ctor.
  101. */
  102. virtual ~wxMemoryInputStream();
  103. /**
  104. Returns the pointer to the stream object used as an internal buffer
  105. for that stream.
  106. */
  107. wxStreamBuffer* GetInputStreamBuffer() const;
  108. };