summaryrefslogtreecommitdiff
path: root/klm/util/read_compressed.hh
blob: 83ca9fb22eceb93f073192f1731dd43666d9c958 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef UTIL_READ_COMPRESSED__
#define UTIL_READ_COMPRESSED__

#include "util/exception.hh"
#include "util/scoped.hh"

#include <cstddef>

#include <stdint.h>

namespace util {

class CompressedException : public Exception {
  public:
    CompressedException() throw();
    virtual ~CompressedException() throw();
};

class GZException : public CompressedException {
  public:
    GZException() throw();
    ~GZException() throw();
};

class BZException : public CompressedException {
  public:
    BZException() throw();
    ~BZException() throw();
};

class XZException : public CompressedException {
  public:
    XZException() throw();
    ~XZException() throw();
};

class ReadBase;

class ReadCompressed {
  public:
    static const std::size_t kMagicSize = 6;
    // Must have at least kMagicSize bytes.  
    static bool DetectCompressedMagic(const void *from);

    // Takes ownership of fd.   
    explicit ReadCompressed(int fd);

    // Must call Reset later.
    ReadCompressed();

    ~ReadCompressed();

    // Takes ownership of fd.  
    void Reset(int fd);

    std::size_t Read(void *to, std::size_t amount);

    uint64_t RawAmount() const { return raw_amount_; }

  private:
    friend class ReadBase;

    scoped_ptr<ReadBase> internal_;

    uint64_t raw_amount_;

    // No copying.  
    ReadCompressed(const ReadCompressed &);
    void operator=(const ReadCompressed &);
};

} // namespace util

#endif // UTIL_READ_COMPRESSED__