summaryrefslogtreecommitdiff
path: root/includes/ZHandle.h
blob: 30db407105c438277f5e353f8770b7b2a84fe313 (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
#pragma once

#include <zlib.h>
#include <Handle.h>
#include <Async.h>

namespace Balau {

class ZStream : public Filter {
  public:
    typedef enum {
        ZLIB,
        GZIP,
        RAW,
    } header_t;
      ZStream(IO<Handle> h, int level = Z_BEST_COMPRESSION, header_t header = ZLIB);
    virtual void close() throw (GeneralException) override;
    virtual ssize_t read(void * buf, size_t count) throw (GeneralException) override;
    virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) override;
    virtual bool isEOF() override { return m_phase == CLOSING || m_eof || Filter::isEOF(); }
    virtual bool isClosed() override { return m_phase == CLOSING || Filter::isClosed(); }
    virtual const char * getName() override { return m_name.to_charp(); }
    void flush() { doFlush(false); }
    void setUseAsyncOp(bool useAsyncOp) { m_useAsyncOp = useAsyncOp; }
    virtual bool isPendingComplete() override;
    void finish() { doFlush(true); }
    void doFlush(bool finish);
  private:
    z_stream m_zin, m_zout;
    String m_name;
    uint8_t * m_buf = NULL;
    uint8_t * m_wptr;
    enum {
        IDLE,
        READING,
        WRITING,
        COMPRESSING,
        COMPRESSING_IDLE,
        DECOMPRESSING,
        DECOMPRESSING_IDLE,
        WRITING_FINISH,
        COMPRESSING_FINISH,
        COMPRESSING_FINISH_IDLE,
        CLOSING,
    } m_phase = IDLE;
    size_t m_total, m_count, m_compressed;
    AsyncOperation * m_op = NULL;
    ssize_t m_status = IDLE;
    bool m_useAsyncOp = true, m_eof = false;
};

};