]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BgzfStream_p.h
0ad7a793f0a8d8086ae3dcc54b943fc821166e7c
[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
50         void Open(const std::string& filename, const IBamIODevice::OpenMode mode);
51         // reads BGZF data into a byte buffer
52         size_t Read(char* data, const size_t dataLength);
53         // seek to position in BGZF file
54         void Seek(const int64_t& position);
55         // sets IO device (closes previous, if any, but does not attempt to open)
56         void SetIODevice(IBamIODevice* device);
57         // enable/disable compressed output
58         void SetWriteCompressed(bool ok);
59         // get file position in BGZF file
60         int64_t Tell(void) const;
61         // writes the supplied data into the BGZF buffer
62         size_t Write(const char* data, const size_t dataLength);
63
64     // internal methods
65     private:
66         // compresses the current block
67         size_t DeflateBlock(void);
68         // flushes the data in the BGZF block
69         void FlushBlock(void);
70         // de-compresses the current block
71         size_t InflateBlock(const size_t& blockLength);
72         // reads a BGZF block
73         void ReadBlock(void);
74
75     // static 'utility' methods
76     public:
77         // checks BGZF block header
78         static bool CheckBlockHeader(char* header);
79
80     // data members
81     public:
82         unsigned int m_blockLength;
83         unsigned int m_blockOffset;
84         uint64_t     m_blockAddress;
85
86         bool m_isWriteCompressed;
87         IBamIODevice* m_device;
88
89         struct RaiiWrapper {
90             RaiiWrapper(void);
91             ~RaiiWrapper(void);
92             char* UncompressedBlock;
93             char* CompressedBlock;
94         };
95         RaiiWrapper Resources;
96 };
97
98 } // namespace Internal
99 } // namespace BamTools
100
101 #endif // BGZFSTREAM_P_H