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