Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vmemorystream_h 00009 #define vmemorystream_h 00010 00013 #include "vstream.h" 00014 00052 class VMemoryStream : public VStream { 00053 public: 00054 00055 static const Vs64 kIncrement2x = 0; 00056 static const Vs64 kDefaultBufferSize = 32768; 00057 typedef enum { kAllocatedByOperatorNew, kAllocatedByMalloc, kAllocatedOnStack, kAllocatedUnknown } BufferAllocationType; 00058 00064 VMemoryStream(Vs64 initialBufferSize = kDefaultBufferSize, Vs64 resizeIncrement = kIncrement2x); 00065 /* 00066 Copy construction and assignment semantics: If the other stream owns its buffer, then 00067 the copy gets a brand new copied buffer; if the other stream doesn't own its buffer, 00068 then the copy also refers to the same buffer which it also does not own. When making 00069 a copy of a buffer, we try to use the same allocation type, but if the original buffer 00070 is on the stack, the copy is made on the heap. 00071 @param other the other VMemoryStream that we are initialized from 00072 */ 00073 VMemoryStream(const VMemoryStream& other); 00085 VMemoryStream(Vu8* buffer, BufferAllocationType allocationType, bool adoptsBuffer, Vs64 suppliedBufferSize, Vs64 suppliedEOFOffset, Vs64 resizeIncrement = kIncrement2x); 00089 virtual ~VMemoryStream(); 00090 00097 VMemoryStream& operator=(const VMemoryStream& other); 00098 00099 // Required VStream method overrides: 00106 virtual Vs64 read(Vu8* targetBuffer, Vs64 numBytesToRead); 00114 virtual Vs64 write(const Vu8* buffer, Vs64 numBytesToWrite); 00120 virtual void flush(); 00125 virtual bool skip(Vs64 numBytesToSkip); 00132 virtual bool seek(Vs64 offset, int whence); 00138 virtual Vs64 getIOOffset() const; 00148 virtual Vs64 available() const; 00149 00150 // Methods we define for callers who know what kind of stream they have: 00162 void adoptBuffer(Vu8* buffer, BufferAllocationType allocationType, bool adoptsBuffer, Vs64 suppliedBufferSize, Vs64 suppliedEOFOffset); 00167 void orphanBuffer(); 00172 Vu8* getBuffer() const; 00177 Vs64 getBufferSize() const; 00182 Vs64 getEOFOffset() const; 00189 void setEOF(Vs64 eofOffset); 00198 friend bool operator==(const VMemoryStream& m1, const VMemoryStream& m2); 00199 00200 protected: 00201 00202 /* 00203 These methods are ONLY overridden by buffer-based subclasses, 00204 for example VMemoryStream. They are called by the friend function 00205 streamCopy() so that it can efficiently copy data directly to/from 00206 streams that have data buffers (namely, VMemoryStream). 00207 */ 00208 00215 virtual Vu8* _getReadIOPtr() const; 00222 virtual Vu8* _getWriteIOPtr() const; 00229 virtual Vs64 _prepareToRead(Vs64 numBytesToRead) const; 00236 virtual void _prepareToWrite(Vs64 numBytesToWrite); 00242 virtual void _finishRead(Vs64 numBytesRead); 00248 virtual void _finishWrite(Vs64 numBytesWritten); 00249 00251 void _assertInvariant() const; 00252 00253 Vs64 mBufferSize; 00254 Vs64 mIOOffset; 00255 Vs64 mEOFOffset; 00256 Vs64 mResizeIncrement; 00257 bool mOwnsBuffer; 00258 BufferAllocationType mAllocationType; 00259 Vu8* mBuffer; 00260 00261 private: 00262 00263 Vu8* _createNewBuffer(Vs64 bufferSize, BufferAllocationType& newAllocationType); 00264 void _releaseBuffer(); 00265 }; 00266 00278 class VReadOnlyMemoryStream : public VMemoryStream { 00279 public: 00280 00286 VReadOnlyMemoryStream(Vu8* buffer, Vs64 suppliedEOFOffset); 00290 VReadOnlyMemoryStream(const VReadOnlyMemoryStream& other); 00294 virtual ~VReadOnlyMemoryStream() {} 00295 00299 VReadOnlyMemoryStream& operator=(const VReadOnlyMemoryStream& other); 00300 00310 void adoptBuffer(Vu8* buffer, BufferAllocationType allocationType, Vs64 suppliedBufferSize, Vs64 suppliedEOFOffset); 00311 00315 virtual Vs64 write(const Vu8* buffer, Vs64 numBytesToWrite); 00319 virtual void flush(); 00320 }; 00321 00322 #endif /* vmemorystream_h */