nlib
Lz4OutputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_OSS_LZ4INPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_OSS_LZ4INPUTSTREAM_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/OutputStream.h"
9 
10 #include "lz4frame.h"
11 
12 #if defined(_MSC_VER) && defined(nx_oss_lz4_EXPORTS)
13 #undef NLIB_VIS_PUBLIC
14 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
15 #endif
16 
17 NLIB_NAMESPACE_BEGIN
18 namespace oss {
19 
20 class Lz4OutputStreamSettings;
21 
22 namespace detail {
23 
24 class NLIB_VIS_PUBLIC Lz4OutputTransform NLIB_FINAL : public OutputTransform {
25  public:
26  Lz4OutputTransform() NLIB_NOEXCEPT;
27  virtual ~Lz4OutputTransform() NLIB_NOEXCEPT NLIB_OVERRIDE;
28  virtual errno_t Transform(OutputStream* os, const void* p, size_t nBytes,
29  bool doFlush) NLIB_NOEXCEPT NLIB_OVERRIDE;
30  virtual void* GetWorkBuffer(size_t* n) NLIB_NOEXCEPT NLIB_OVERRIDE;
31  virtual errno_t OnSetStream(OutputStream* os) NLIB_NOEXCEPT NLIB_OVERRIDE;
32  virtual errno_t OnClose(OutputStream* os) NLIB_NOEXCEPT NLIB_OVERRIDE;
33 
34  errno_t Init(const Lz4OutputStreamSettings& settings) NLIB_NOEXCEPT;
35 
36  private:
37  LZ4F_compressionContext_t m_Ctx;
38  unsigned char* m_BufOut;
39  size_t m_BufOutSize;
40  size_t m_BlockSize;
41  size_t m_HeaderSize;
42  unsigned char m_Header[16]; // LZ4F_MAXHEADERFRAME_SIZE <= sizeof(m_Header)
43  unsigned char m_BaseBuf[256];
44  NLIB_DISALLOW_COPY_AND_ASSIGN(Lz4OutputTransform);
45 };
46 
47 } // namespace detail
48 
50  public:
51  Lz4OutputStreamSettings() NLIB_NOEXCEPT {
52  nlib_memset(&m_Pref, 0, sizeof(m_Pref));
53  m_Pref.autoFlush = 1;
54  m_Pref.frameInfo.blockMode = LZ4F_blockLinked;
55  m_Pref.compressionLevel = 9; // hc
56  }
57  ~Lz4OutputStreamSettings() NLIB_NOEXCEPT {}
58  void SetContentSize(uint64_t size) NLIB_NOEXCEPT {
59  m_Pref.frameInfo.contentSize = size;
60  }
61  uint64_t GetContentSize() const NLIB_NOEXCEPT {
62  return m_Pref.frameInfo.contentSize;
63  }
64  void SetCompressionLevel(int level) NLIB_NOEXCEPT {
65  if (level < 0)
66  level = 0;
67  else if (level > 16)
68  level = 16;
69  m_Pref.compressionLevel = level;
70  }
71  int GetCompressionLevel() NLIB_NOEXCEPT {
72  return m_Pref.compressionLevel;
73  }
74  void SetMaxBlockSize(LZ4F_blockSizeID_t max_block_size) NLIB_NOEXCEPT {
75  m_Pref.frameInfo.blockSizeID = max_block_size;
76  }
77  LZ4F_blockSizeID_t GetMaxBlockSize() const NLIB_NOEXCEPT {
78  return m_Pref.frameInfo.blockSizeID;
79  }
80  void SetBlockIndependence(bool independence) NLIB_NOEXCEPT {
81  m_Pref.frameInfo.blockMode = independence ? LZ4F_blockIndependent : LZ4F_blockLinked;
82  }
83  bool GetBlockIndependence() const NLIB_NOEXCEPT {
84  return (m_Pref.frameInfo.blockMode != LZ4F_blockLinked);
85  }
86 
87  private:
88  LZ4F_preferences_t m_Pref;
89  friend class detail::Lz4OutputTransform;
90 };
91 
93  : public OutputConverterStreamTempl<detail::Lz4OutputTransform> {
94  public:
98  Lz4OutputStreamSettings settings;
99  return this->Init(settings);
100  }
102  return m_Transform.Init(settings);
103  }
104 
105  private:
107 };
108 
109 } // namespace oss
110 NLIB_NAMESPACE_END
111 
112 #if defined(_MSC_VER) && defined(nx_oss_lz4_EXPORTS)
113 #undef NLIB_VIS_PUBLIC
114 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
115 #endif
116 
117 #endif // INCLUDE_NN_NLIB_OSS_LZ4INPUTSTREAM_H_
bool GetBlockIndependence() const noexcept
Gets the value specified with SetBlockIndependence(). The default is false.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
Lz4OutputStreamSettings() noexcept
Instantiates the object with default parameters (default constructor).
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
~Lz4OutputStreamSettings() noexcept
Destructor.
LZ4F_blockSizeID_t GetMaxBlockSize() const noexcept
Gets the value specified with SetMaxBlockSize(). The default is LZ4F_default.
void SetMaxBlockSize(LZ4F_blockSizeID_t max_block_size) noexcept
Sets the maximum data block size. The larger the size, the higher the speed, though more memory is re...
void SetContentSize(uint64_t size) noexcept
Data with the size before compression can be included in data compressed with LZ4.
The class for holding the OutputTransform derived class.
errno_t Init(const Lz4OutputStreamSettings &settings) noexcept
Initializes a stream.
The class to obtain and configure the Lz4OutputStream settings.
errno_t Init() noexcept
Initializes the stream with the default settings.
errno_t nlib_memset(void *buf, int ch, size_t n)
Makes a function call corresponding to memset(buf, ch, n).
Definition: Platform.h:2117
uint64_t GetContentSize() const noexcept
Gets the value specified with SetContentSize(). The default is 0.
A file that contains the configuration information for each development environment.
void SetBlockIndependence(bool independence) noexcept
Sets whether a compressed data block can be uncompressed independently.
The stream class to write data according to the LZ4 frame format.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
Defines the base class for output streams.
int GetCompressionLevel() noexcept
Gets the value specified with SetCompressionLevel(). The default is 9.
void SetCompressionLevel(int level) noexcept
Sets a compression level. The compression rate is an integer in a range from 0 to 16...
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24