Home | History | Annotate | Download | only in fxcrt
      1 // Copyright 2017 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #ifndef CORE_FXCRT_CFX_MEMORYSTREAM_H_
      8 #define CORE_FXCRT_CFX_MEMORYSTREAM_H_
      9 
     10 #include <vector>
     11 
     12 #include "core/fxcrt/fx_stream.h"
     13 #include "core/fxcrt/retain_ptr.h"
     14 
     15 class CFX_MemoryStream : public IFX_SeekableStream {
     16  public:
     17   enum Type { kConsecutive = 1 << 0, kTakeOver = 1 << 1 };
     18 
     19   template <typename T, typename... Args>
     20   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
     21 
     22   // IFX_SeekableStream
     23   FX_FILESIZE GetSize() override;
     24   FX_FILESIZE GetPosition() override;
     25   bool IsEOF() override;
     26   bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
     27   size_t ReadBlock(void* buffer, size_t size) override;
     28   bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
     29   bool Flush() override;
     30 
     31   bool IsConsecutive() const { return !!(m_dwFlags & Type::kConsecutive); }
     32 
     33   uint8_t* GetBuffer() const {
     34     return !m_Blocks.empty() ? m_Blocks.front() : nullptr;
     35   }
     36 
     37   void EstimateSize(size_t nInitSize, size_t nGrowSize);
     38   void AttachBuffer(uint8_t* pBuffer, size_t nSize);
     39   void DetachBuffer();
     40 
     41  private:
     42   explicit CFX_MemoryStream(bool bConsecutive);
     43   CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver);
     44   ~CFX_MemoryStream() override;
     45 
     46   bool ExpandBlocks(size_t size);
     47 
     48   std::vector<uint8_t*> m_Blocks;
     49   size_t m_nTotalSize;
     50   size_t m_nCurSize;
     51   size_t m_nCurPos;
     52   size_t m_nGrowSize;
     53   uint32_t m_dwFlags;
     54 };
     55 
     56 #endif  // CORE_FXCRT_CFX_MEMORYSTREAM_H_
     57