gzip_stream.h

This section contains reference documentation for working with protocol buffer classes in C++.

#include <google/protobuf/io/gzip_stream.h>
namespace google::protobuf::io

This file contains the definition for classes GzipInputStream and GzipOutputStream.

GzipInputStream decompresses data from an underlying ZeroCopyInputStream and provides the decompressed data as a ZeroCopyInputStream.

GzipOutputStream is an ZeroCopyOutputStream that compresses data to an underlying ZeroCopyOutputStream.

Classes in this file

A ZeroCopyInputStream that reads compressed data through zlib.

class GzipInputStream: public ZeroCopyInputStream

#include <google/protobuf/io/gzip_stream.h>
namespace google::protobuf::io

A ZeroCopyInputStream that reads compressed data through zlib.

Members

enum
Format
Format key for constructor. more...
explicit
GzipInputStream(ZeroCopyInputStream * sub_stream, Format format = AUTO, int buffer_size = -1)
buffer_size and format may be -1 for default of 64kB and GZIP format
virtual
~GzipInputStream()
const char *
ZlibErrorMessage() const
Return last error message or NULL if no error.
int
ZlibErrorCode() const

implements ZeroCopyInputStream

virtual bool
Next(const void ** data, int * size)
Obtains a chunk of data from the stream. more...
virtual void
BackUp(int count)
Backs up a number of bytes, so that the next call to Next() returns data again that was already returned by the last call to Next(). more...
virtual bool
Skip(int count)
Skips a number of bytes. more...
virtual int64
ByteCount() const
Returns the total number of bytes read since this object was created.

enum GzipInputStream::Format {
  AUTO = = 0,
  GZIP = = 1,
  ZLIB = = 2
}

Format key for constructor.

AUTOzlib will autodetect gzip header or deflate stream
GZIPGZIP streams have some extra header data for file attributes.
ZLIBSimpler zlib stream format.

virtual bool GzipInputStream::Next(
        const void ** data,
        int * size)

Obtains a chunk of data from the stream.

Preconditions:

  • "size" and "data" are not NULL.

Postconditions:

  • If the returned value is false, there is no more data to return or an error occurred. All errors are permanent.
  • Otherwise, "size" points to the actual number of bytes read and "data" points to a pointer to a buffer containing these bytes.
  • Ownership of this buffer remains with the stream, and the buffer remains valid only until some other method of the stream is called or the stream is destroyed.
  • It is legal for the returned buffer to have zero size, as long as repeatedly calling Next() eventually yields a buffer with non-zero size.

virtual void GzipInputStream::BackUp(
        int count)

Backs up a number of bytes, so that the next call to Next() returns data again that was already returned by the last call to Next().

This is useful when writing procedures that are only supposed to read up to a certain point in the input, then return. If Next() returns a buffer that goes beyond what you wanted to read, you can use BackUp() to return to the point where you intended to finish.

Preconditions:

  • The last method called must have been Next().
  • count must be less than or equal to the size of the last buffer returned by Next().

Postconditions:

  • The last "count" bytes of the last buffer returned by Next() will be pushed back into the stream. Subsequent calls to Next() will return the same data again before producing new data.

virtual bool GzipInputStream::Skip(
        int count)

Skips a number of bytes.

Returns false if the end of the stream is reached or some input error occurred. In the end-of-stream case, the stream is advanced to the end of the stream (so ByteCount() will return the total size of the stream).

class GzipOutputStream: public ZeroCopyOutputStream

#include <google/protobuf/io/gzip_stream.h>
namespace google::protobuf::io

Members

enum
Format
Format key for constructor. more...
explicit
GzipOutputStream(ZeroCopyOutputStream * sub_stream)
Create a GzipOutputStream with default options.
GzipOutputStream(ZeroCopyOutputStream * sub_stream, const Options & options)
Create a GzipOutputStream with the given options.
virtual
~GzipOutputStream()
const char *
ZlibErrorMessage() const
Return last error message or NULL if no error.
int
ZlibErrorCode() const
bool
Flush()
Flushes data written so far to zipped data in the underlying stream. more...
bool
Close()
Writes out all data and closes the gzip stream. more...

implements ZeroCopyOutputStream

virtual bool
Next(void ** data, int * size)
Obtains a buffer into which data can be written. more...
virtual void
BackUp(int count)
Backs up a number of bytes, so that the end of the last buffer returned by Next() is not actually written. more...
virtual int64
ByteCount() const
Returns the total number of bytes written since this object was created.

enum GzipOutputStream::Format {
  GZIP = = 1,
  ZLIB = = 2
}

Format key for constructor.

GZIPGZIP streams have some extra header data for file attributes.
ZLIBSimpler zlib stream format.

bool GzipOutputStream::Flush()

Flushes data written so far to zipped data in the underlying stream.

It is the caller's responsibility to flush the underlying stream if necessary. Compression may be less efficient stopping and starting around flushes. Returns true if no error.

Please ensure that block size is > 6. Here is an excerpt from the zlib doc that explains why:

In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that avail_out is greater than six to avoid repeated flush markers due to avail_out == 0 on return.


bool GzipOutputStream::Close()

Writes out all data and closes the gzip stream.

It is the caller's responsibility to close the underlying stream if necessary. Returns true if no error.


virtual bool GzipOutputStream::Next(
        void ** data,
        int * size)

Obtains a buffer into which data can be written.

Any data written into this buffer will eventually (maybe instantly, maybe later on) be written to the output.

Preconditions:

  • "size" and "data" are not NULL.

Postconditions:

  • If the returned value is false, an error occurred. All errors are permanent.
  • Otherwise, "size" points to the actual number of bytes in the buffer and "data" points to the buffer.
  • Ownership of this buffer remains with the stream, and the buffer remains valid only until some other method of the stream is called or the stream is destroyed.
  • Any data which the caller stores in this buffer will eventually be written to the output (unless BackUp() is called).
  • It is legal for the returned buffer to have zero size, as long as repeatedly calling Next() eventually yields a buffer with non-zero size.

virtual void GzipOutputStream::BackUp(
        int count)

Backs up a number of bytes, so that the end of the last buffer returned by Next() is not actually written.

This is needed when you finish writing all the data you want to write, but the last buffer was bigger than you needed. You don't want to write a bunch of garbage after the end of your data, so you use BackUp() to back up.

Preconditions:

  • The last method called must have been Next().
  • count must be less than or equal to the size of the last buffer returned by Next().
  • The caller must not have written anything to the last "count" bytes of that buffer.

Postconditions:

  • The last "count" bytes of the last buffer returned by Next() will be ignored.

struct GzipOutputStream::Options

#include <google/protobuf/io/gzip_stream.h>
namespace google::protobuf::io

Members

Format
format
Defaults to GZIP.
int
buffer_size
What size buffer to use internally. Defaults to 64kB.
int
compression_level
A number between 0 and 9, where 0 is no compression and 9 is best compression. more...
int
compression_strategy
Defaults to Z_DEFAULT_STRATEGY. more...
Options()
Initializes with default values.

intOptions::compression_level

A number between 0 and 9, where 0 is no compression and 9 is best compression.

Defaults to Z_DEFAULT_COMPRESSION (see zlib.h).


intOptions::compression_strategy

Defaults to Z_DEFAULT_STRATEGY.

Can also be set to Z_FILTERED, Z_HUFFMAN_ONLY, or Z_RLE. See the documentation for deflateInit2 in zlib.h for definitions of these constants.