File: GzipFileWriter.hpp

package info (click to toggle)
r-bioc-alabaster.base 1.6.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,652 kB
  • sloc: cpp: 11,377; sh: 29; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,975 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef BYTEME_GZIP_FILE_WRITER_HPP
#define BYTEME_GZIP_FILE_WRITER_HPP

#include <stdexcept>
#include <vector>
#include <string>
#include "Writer.hpp"

/**
 * @file GzipFileWriter.hpp
 *
 * @brief Write a Gzip-compressed file.
 */

namespace byteme {

/**
 * @brief Write uncompressed bytes to a Gzip-compressed file.
 *
 * This is basically a wrapper around Zlib's `gzFile` with correct closing and error checking.
 */
class GzipFileWriter : public Writer {
public:
    /**
     * @param path Path to the file.
     * @param compression_level Gzip compression level. 
     * @param buffer_size Size of the internal buffer. 
     */
    GzipFileWriter(const char* path, int compression_level = 6, size_t buffer_size = 65536) : gz(path, "wb") {
        if (gzbuffer(gz.handle, buffer_size)) {
            throw std::runtime_error("failed to set the Gzip compression buffer");
        }
        if (gzsetparams(gz.handle, compression_level, Z_DEFAULT_STRATEGY) != Z_OK) {
            throw std::runtime_error("failed to set the Gzip compression parameters");
        }
    }

    /**
     * @param path Path to the file.
     * @param compression_level Gzip compression level. 
     * @param buffer_size Size of the buffer to use for reading.
     */
    GzipFileWriter(const std::string& path, int compression_level = 6, size_t buffer_size = 65536) : GzipFileWriter(path.c_str(), compression_level, buffer_size) {}

public:
    using Writer::write;

    void write(const unsigned char* buffer, size_t n) {
        if (n) {
            size_t ok = gzwrite(gz.handle, buffer, n);
            if (ok != n) {
                throw std::runtime_error("failed to write to the Gzip-compressed file");
            }
        }
    }

    void finish() {
        gz.closed = true;
        if (gzclose(gz.handle) != Z_OK) {
            throw std::runtime_error("failed to close the Gzip-compressed file after writing");
        }
    }

private:
    SelfClosingGzFile gz;
};

}

#endif