proxygen
proxygen::ZlibStreamCompressor Class Reference

#include <ZlibStreamCompressor.h>

Public Member Functions

 ZlibStreamCompressor (ZlibCompressionType type, int level)
 
 ~ZlibStreamCompressor ()
 
void init (ZlibCompressionType type, int level)
 
std::unique_ptr< folly::IOBufcompress (const folly::IOBuf *in, bool trailer=true)
 
int getStatus ()
 
bool hasError ()
 
bool finished ()
 

Private Attributes

ZlibCompressionType type_ {ZlibCompressionType::NONE}
 
int level_ {Z_DEFAULT_COMPRESSION}
 
z_stream zlibStream_
 
int status_ {-1}
 

Detailed Description

Definition at line 25 of file ZlibStreamCompressor.h.

Constructor & Destructor Documentation

proxygen::ZlibStreamCompressor::ZlibStreamCompressor ( ZlibCompressionType  type,
int  level 
)
explicit

Definition at line 95 of file ZlibStreamCompressor.cpp.

References init().

96  : status_(Z_OK) {
97  init(type, level);
98 }
void init(ZlibCompressionType type, int level)
PskType type
proxygen::ZlibStreamCompressor::~ZlibStreamCompressor ( )

Member Function Documentation

std::unique_ptr< IOBuf > proxygen::ZlibStreamCompressor::compress ( const folly::IOBuf in,
bool  trailer = true 
)

Definition at line 109 of file ZlibStreamCompressor.cpp.

References folly::io::addOutputBuffer(), folly::FATAL, folly::IOBuf::prev(), folly::gen::range(), status_, folly::IOBuf::trimEnd(), uint32_t, uint64_t, uint8_t, and zlibStream_.

Referenced by compressThenDecompress().

110  {
111  auto bufferLength = FLAGS_zlib_compressor_buffer_growth;
112 
113  auto out = addOutputBuffer(&zlibStream_, bufferLength);
114 
115  for (auto& range : *in) {
116  uint64_t remaining = range.size();
117  uint64_t written = 0;
118  while (remaining) {
119  uint32_t step = remaining;
120  zlibStream_.next_in = const_cast<uint8_t*>(range.data() + written);
121  zlibStream_.avail_in = step;
122  remaining -= step;
123  written += step;
124 
125  while (zlibStream_.avail_in != 0) {
126  status_ = deflateHelper(&zlibStream_, out.get(), Z_NO_FLUSH);
127  if (status_ != Z_OK) {
128  DLOG(FATAL) << "Deflate failed: " << zlibStream_.msg;
129  return nullptr;
130  }
131  }
132  }
133  }
134 
135  if (trailer) {
136  do {
137  status_ = deflateHelper(&zlibStream_, out.get(), Z_FINISH);
138  } while (status_ == Z_OK);
139 
140  if (status_ != Z_STREAM_END) {
141  DLOG(FATAL) << "Deflate failed: " << zlibStream_.msg;
142  return nullptr;
143  }
144  } else {
145  do {
146  status_ = deflateHelper(&zlibStream_, out.get(), Z_SYNC_FLUSH);
147  } while (zlibStream_.avail_out == 0);
148 
149  if (status_ != Z_OK) {
150  DLOG(FATAL) << "Deflate failed: " << zlibStream_.msg;
151  return nullptr;
152  }
153  }
154 
155  out->prev()->trimEnd(zlibStream_.avail_out);
156 
157  zlibStream_.next_out = Z_NULL;
158  zlibStream_.avail_out = 0;
159 
160  return out;
161 }
static std::unique_ptr< IOBuf > addOutputBuffer(MutableByteRange &output, uint64_t size)
Gen range(Value begin, Value end)
Definition: Base.h:467
bool proxygen::ZlibStreamCompressor::finished ( )
inline

Definition at line 40 of file ZlibStreamCompressor.h.

40 { return status_ == Z_STREAM_END; }
int proxygen::ZlibStreamCompressor::getStatus ( )
inline

Definition at line 36 of file ZlibStreamCompressor.h.

Referenced by compressThenDecompress().

bool proxygen::ZlibStreamCompressor::hasError ( )
inline

Definition at line 38 of file ZlibStreamCompressor.h.

Referenced by compressThenDecompress().

38 { return status_ != Z_OK && status_ != Z_STREAM_END; }
void proxygen::ZlibStreamCompressor::init ( ZlibCompressionType  type,
int  level 
)

Definition at line 51 of file ZlibStreamCompressor.cpp.

References proxygen::ERROR, folly::NONE, type, and type_.

Referenced by ZlibStreamCompressor().

51  {
52 
54  << "Attempt to re-initialize compression stream";
55 
56  type_ = type;
57  level_ = level;
58  status_ = Z_OK;
59 
60  zlibStream_.zalloc = Z_NULL;
61  zlibStream_.zfree = Z_NULL;
62  zlibStream_.opaque = Z_NULL;
63  zlibStream_.total_in = 0;
64  zlibStream_.next_in = Z_NULL;
65  zlibStream_.avail_in = 0;
66  zlibStream_.avail_out = 0;
67  zlibStream_.next_out = Z_NULL;
68 
69  DCHECK(level_ == Z_DEFAULT_COMPRESSION ||
70  (level_ >= Z_NO_COMPRESSION && level_ <= Z_BEST_COMPRESSION))
71  << "Invalid Zlib compression level. level=" << level_;
72 
73  switch (type_) {
75  status_ = deflateInit2(&zlibStream_,
76  level_,
77  Z_DEFLATED,
78  static_cast<int32_t>(type),
79  MAX_MEM_LEVEL,
80  Z_DEFAULT_STRATEGY);
81  break;
83  status_ = deflateInit(&zlibStream_, level);
84  break;
85  default:
86  DCHECK(false) << "Unsupported zlib compression type.";
87  break;
88  }
89 
90  if (status_ != Z_OK) {
91  LOG(ERROR) << "error initializing zlib stream. r=" << status_;
92  }
93 }
PskType type

Member Data Documentation

int proxygen::ZlibStreamCompressor::level_ {Z_DEFAULT_COMPRESSION}
private

Definition at line 44 of file ZlibStreamCompressor.h.

int proxygen::ZlibStreamCompressor::status_ {-1}
private

Definition at line 46 of file ZlibStreamCompressor.h.

Referenced by compress(), and ~ZlibStreamCompressor().

ZlibCompressionType proxygen::ZlibStreamCompressor::type_ {ZlibCompressionType::NONE}
private

Definition at line 43 of file ZlibStreamCompressor.h.

Referenced by ~ZlibStreamCompressor().

z_stream proxygen::ZlibStreamCompressor::zlibStream_
private

Definition at line 45 of file ZlibStreamCompressor.h.

Referenced by compress(), and ~ZlibStreamCompressor().


The documentation for this class was generated from the following files: