StreamMemory.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORESTREAMMEMORY_H
  28. #define ROCKETCORESTREAMMEMORY_H
  29. #include "Header.h"
  30. #include "Stream.h"
  31. namespace Rocket {
  32. namespace Core {
  33. /**
  34. Memory Byte Stream Class
  35. @author Lloyd Weehuizen
  36. */
  37. class ROCKETCORE_API StreamMemory : public Stream
  38. {
  39. public:
  40. /// Empty memory stream with default size buffer
  41. StreamMemory();
  42. /// Empty memory stream with specified buffer size
  43. StreamMemory(size_t initial_size);
  44. /// Read only memory stream based on the existing buffer
  45. StreamMemory(const byte* buffer, size_t buffer_size);
  46. /// Copy a memory stream
  47. StreamMemory(const StreamMemory& copy);
  48. virtual ~StreamMemory();
  49. StreamMemory& operator=(const StreamMemory& copy);
  50. /// Close the stream
  51. virtual void Close();
  52. /// Are we at the end of the stream
  53. virtual bool IsEOS() const;
  54. /// Size of this stream ( in bytes )
  55. virtual size_t Length() const;
  56. /// Get Stream position ( in bytes )
  57. size_t Tell() const;
  58. /// Set Stream position ( in bytes )
  59. bool Seek(long offset, int origin) const;
  60. /// Read from the stream
  61. using Stream::Read;
  62. virtual size_t Read(void* buffer, size_t bytes) const;
  63. /// Peek into the stream
  64. virtual size_t Peek(void *buffer, size_t bytes) const;
  65. /// Write to the stream
  66. using Stream::Write;
  67. virtual size_t Write(const void* buffer, size_t bytes);
  68. /// Truncate the stream to the specified length
  69. virtual size_t Truncate(size_t bytes);
  70. /// Push onto the front of the stream
  71. virtual size_t PushFront(const void* buffer, size_t bytes);
  72. /// Pop from the front of the stream
  73. virtual size_t PopFront(size_t bytes);
  74. /// Raw access to the stream
  75. const byte* RawStream() const;
  76. /// Erase a section of the stream
  77. void Erase(size_t offset, size_t bytes);
  78. /// Does the stream have data available for reading
  79. virtual bool IsReadReady();
  80. /// Is the stream able to accept data now
  81. virtual bool IsWriteReady();
  82. /// Sets this streams source URL, useful data that is stored
  83. /// in memory streams that originated from files
  84. void SetSourceURL(const URL& url);
  85. private:
  86. byte* buffer;
  87. mutable byte* buffer_ptr;
  88. size_t buffer_size;
  89. size_t buffer_used;
  90. bool owns_buffer;
  91. bool Reallocate(size_t size);
  92. };
  93. }
  94. }
  95. #endif