]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BgzfStream_p.h
07aae52a195751281884e3b8c164663bcd2702bf
[bamtools.git] / src / api / internal / BgzfStream_p.h
1 // ***************************************************************************
2 // BgzfStream_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 7 October 2011(DB)
6 // ---------------------------------------------------------------------------
7 // Based on BGZF routines developed at the Broad Institute.
8 // Provides the basic functionality for reading & writing BGZF files
9 // Replaces the old BGZF.* files to avoid clashing with other toolkits
10 // ***************************************************************************
11
12 #ifndef BGZFSTREAM_P_H
13 #define BGZFSTREAM_P_H
14
15 //  -------------
16 //  W A R N I N G
17 //  -------------
18 //
19 // This file is not part of the BamTools API.  It exists purely as an
20 // implementation detail. This header file may change from version to version
21 // without notice, or even be removed.
22 //
23 // We mean it.
24
25 #include <api/BamAux.h>
26 #include <api/BamConstants.h>
27 #include <api/IBamIODevice.h>
28 #include "zlib.h"
29 #include <cstdio>
30 #include <memory>
31 #include <string>
32
33 namespace BamTools {
34 namespace Internal {
35
36 class BgzfStream {
37
38     // constructor & destructor
39     public:
40         BgzfStream(void);
41         ~BgzfStream(void);
42
43     // main interface methods
44     public:
45         // closes BGZF file
46         void Close(void);
47         // returns true if BgzfStream open for IO
48         bool IsOpen(void) const;
49         // opens the BGZF file (mode is either "rb" for reading, or "wb" for writing)
50         void Open(const std::string& filename, const char* mode);
51         void Open(const std::string& filename, const IBamIODevice::OpenMode mode);
52         // reads BGZF data into a byte buffer
53         size_t Read(char* data, const size_t dataLength);
54         // seek to position in BGZF file
55         void Seek(const int64_t& position);
56         // sets IO device (closes previous, if any, but does not attempt to open)
57         void SetIODevice(IBamIODevice* device);
58         // enable/disable compressed output
59         void SetWriteCompressed(bool ok);
60         // get file position in BGZF file
61         int64_t Tell(void) const;
62         // writes the supplied data into the BGZF buffer
63         size_t Write(const char* data, const size_t dataLength);
64
65     // internal methods
66     private:
67         // compresses the current block
68         size_t DeflateBlock(void);
69         // flushes the data in the BGZF block
70         void FlushBlock(void);
71         // de-compresses the current block
72         size_t InflateBlock(const size_t& blockLength);
73         // reads a BGZF block
74         void ReadBlock(void);
75
76     // static 'utility' methods
77     public:
78         // checks BGZF block header
79         static bool CheckBlockHeader(char* header);
80
81     // data members
82     public:
83         unsigned int m_blockLength;
84         unsigned int m_blockOffset;
85         uint64_t     m_blockAddress;
86
87         bool m_isOpen;
88         bool m_isWriteOnly;
89         bool m_isWriteCompressed;
90
91         IBamIODevice* m_device;
92
93         struct RaiiWrapper {
94             RaiiWrapper(void);
95             ~RaiiWrapper(void);
96             char* UncompressedBlock;
97             char* CompressedBlock;
98             FILE* Stream;
99         };
100         RaiiWrapper Resources;
101 };
102
103 } // namespace Internal
104 } // namespace BamTools
105
106 #endif // BGZFSTREAM_P_H