summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_memorystream.h
blob: 2112280237731ffb48b15a3f85c454f375e7c323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef CORE_FXCRT_CFX_MEMORYSTREAM_H_
#define CORE_FXCRT_CFX_MEMORYSTREAM_H_

#include <vector>

#include "core/fxcrt/cfx_retain_ptr.h"
#include "core/fxcrt/fx_stream.h"

class CFX_MemoryStream : public IFX_SeekableStream {
 public:
  enum Type { kConsecutive = 1 << 0, kTakeOver = 1 << 1 };

  template <typename T, typename... Args>
  friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);

  // IFX_SeekableStream
  FX_FILESIZE GetSize() override;
  FX_FILESIZE GetPosition() override;
  bool IsEOF() override;
  bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
  size_t ReadBlock(void* buffer, size_t size) override;
  bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
  bool Flush() override;

  bool IsConsecutive() const { return !!(m_dwFlags & Type::kConsecutive); }

  uint8_t* GetBuffer() const {
    return !m_Blocks.empty() ? m_Blocks.front() : nullptr;
  }

  void EstimateSize(size_t nInitSize, size_t nGrowSize);
  void AttachBuffer(uint8_t* pBuffer, size_t nSize);
  void DetachBuffer();

 private:
  explicit CFX_MemoryStream(bool bConsecutive);
  CFX_MemoryStream(uint8_t* pBuffer, size_t nSize, bool bTakeOver);
  ~CFX_MemoryStream() override;

  bool ExpandBlocks(size_t size);

  std::vector<uint8_t*> m_Blocks;
  size_t m_nTotalSize;
  size_t m_nCurSize;
  size_t m_nCurPos;
  size_t m_nGrowSize;
  uint32_t m_dwFlags;
};

#endif  // CORE_FXCRT_CFX_MEMORYSTREAM_H_