summaryrefslogtreecommitdiff
path: root/includes/ZHandle.h
blob: dfb73fbff8fd70e6f3907a582c28362a1dca3b75 (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
53
54
55
56
#pragma once

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

namespace Balau {

class ZStream : public Handle {
  public:
    typedef enum {
        ZLIB,
        GZIP,
        RAW,
    } header_t;
      ZStream(const IO<Handle> & h, int level = Z_BEST_COMPRESSION, header_t header = ZLIB);
    virtual void close() throw (GeneralException);
    virtual bool isClosed();
    virtual bool isEOF();
    virtual bool canRead();
    virtual bool canWrite();
    virtual const char * getName();
    virtual ssize_t read(void * buf, size_t count) throw (GeneralException);
    virtual ssize_t write(const void * buf, size_t count) throw (GeneralException);
    void detach() { m_detached = true; }
    void flush() { doFlush(false); }
    void setUseAsyncOp(bool useAsyncOp) { m_useAsyncOp = useAsyncOp; }
    virtual bool isPendingComplete();
    void finish() { doFlush(true); }
    void doFlush(bool finish);
  private:
    IO<Handle> m_h;
    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_detached = false, m_closed = false, m_eof = false, m_useAsyncOp = true;
};

};