]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BgzfStream_p.h
Cleaned up intra-API includes & moved version numbers to 2.0.0
[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: 10 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/api_global.h"
26 #include "api/IBamIODevice.h"
27 #include <string>
28
29 namespace BamTools {
30 namespace Internal {
31
32 class BgzfStream {
33
34     // constructor & destructor
35     public:
36         BgzfStream(void);
37         ~BgzfStream(void);
38
39     // main interface methods
40     public:
41         // closes BGZF file
42         void Close(void);
43         // returns true if BgzfStream open for IO
44         bool IsOpen(void) const;
45         // opens the BGZF file
46         void Open(const std::string& filename, const IBamIODevice::OpenMode mode);
47         // reads BGZF data into a byte buffer
48         size_t Read(char* data, const size_t dataLength);
49         // seek to position in BGZF file
50         void Seek(const int64_t& position);
51         // sets IO device (closes previous, if any, but does not attempt to open)
52         void SetIODevice(IBamIODevice* device);
53         // enable/disable compressed output
54         void SetWriteCompressed(bool ok);
55         // get file position in BGZF file
56         int64_t Tell(void) const;
57         // writes the supplied data into the BGZF buffer
58         size_t Write(const char* data, const size_t dataLength);
59
60     // internal methods
61     private:
62         // compresses the current block
63         size_t DeflateBlock(void);
64         // flushes the data in the BGZF block
65         void FlushBlock(void);
66         // de-compresses the current block
67         size_t InflateBlock(const size_t& blockLength);
68         // reads a BGZF block
69         void ReadBlock(void);
70
71     // static 'utility' methods
72     public:
73         // checks BGZF block header
74         static bool CheckBlockHeader(char* header);
75
76     // data members
77     public:
78         unsigned int m_blockLength;
79         unsigned int m_blockOffset;
80         uint64_t     m_blockAddress;
81
82         bool m_isWriteCompressed;
83         IBamIODevice* m_device;
84
85         struct RaiiWrapper {
86             RaiiWrapper(void);
87             ~RaiiWrapper(void);
88             char* UncompressedBlock;
89             char* CompressedBlock;
90         };
91         RaiiWrapper Resources;
92 };
93
94 } // namespace Internal
95 } // namespace BamTools
96
97 #endif // BGZFSTREAM_P_H