]> git.donarmstrong.com Git - bamtools.git/commitdiff
Fixed regression: missing EOF empty block on BgzfStream::Close()
authorderek <derekwbarnett@gmail.com>
Tue, 17 Jan 2012 16:47:13 +0000 (11:47 -0500)
committerderek <derekwbarnett@gmail.com>
Tue, 17 Jan 2012 16:47:13 +0000 (11:47 -0500)
src/api/internal/io/BgzfStream_p.cpp
src/api/internal/io/BgzfStream_p.h

index 7f73d67fea9c43fd387523fce4ade4c49ed3dc5b..1f4e0d8861197c2e94de38724d2e249d8516c171 100644 (file)
@@ -2,7 +2,7 @@
 // BgzfStream_p.cpp (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 25 October 2011(DB)
+// Last modified: 17 January 2012(DB)
 // ---------------------------------------------------------------------------
 // Based on BGZF routines developed at the Broad Institute.
 // Provides the basic functionality for reading & writing BGZF files
@@ -67,7 +67,7 @@ void BgzfStream::Close(void) {
     // then write an empty block (as EOF marker)
     if ( m_device->IsOpen() && (m_device->Mode() == IBamIODevice::WriteOnly) ) {
         FlushBlock();
-        const size_t blockLength = DeflateBlock();
+        const size_t blockLength = DeflateBlock(0);
         m_device->Write(m_compressedBlock.Buffer, blockLength);
     }
 
@@ -88,7 +88,7 @@ void BgzfStream::Close(void) {
 }
 
 // compresses the current block
-size_t BgzfStream::DeflateBlock(void) {
+size_t BgzfStream::DeflateBlock(int32_t blockLength) {
 
     // initialize the gzip header
     char* buffer = m_compressedBlock.Buffer;
@@ -107,7 +107,7 @@ size_t BgzfStream::DeflateBlock(void) {
     const int compressionLevel = ( m_isWriteCompressed ? Z_DEFAULT_COMPRESSION : 0 );
 
     // loop to retry for blocks that do not compress enough
-    int inputLength = m_blockOffset;
+    int inputLength = blockLength;
     size_t compressedLength = 0;
     const unsigned int bufferSize = Constants::BGZF_MAX_BLOCK_SIZE;
 
@@ -180,7 +180,7 @@ size_t BgzfStream::DeflateBlock(void) {
     BamTools::PackUnsignedInt(&buffer[compressedLength - 4], inputLength);
 
     // ensure that we have less than a block of data left
-    int remaining = m_blockOffset - inputLength;
+    int remaining = blockLength - inputLength;
     if ( remaining > 0 ) {
         if ( remaining > inputLength )
             throw BamException("BgzfStream::DeflateBlock", "after deflate, remainder too large");
@@ -203,7 +203,7 @@ void BgzfStream::FlushBlock(void) {
     while ( m_blockOffset > 0 ) {
 
         // compress the data block
-        const size_t blockLength = DeflateBlock();
+        const size_t blockLength = DeflateBlock(m_blockOffset);
 
         // flush the data to our output device
         const int64_t numBytesWritten = m_device->Write(m_compressedBlock.Buffer, blockLength);
@@ -328,7 +328,6 @@ size_t BgzfStream::Read(char* data, const size_t dataLength) {
         m_blockAddress = m_device->Tell();
         m_blockOffset  = 0;
         m_blockLength  = 0;
-
     }
 
     // return actual number of bytes read
@@ -341,7 +340,7 @@ void BgzfStream::ReadBlock(void) {
     BT_ASSERT_X( m_device, "BgzfStream::ReadBlock() - trying to read from null IO device");
 
     // store block's starting address
-    int64_t blockAddress = m_device->Tell();
+    const int64_t blockAddress = m_device->Tell();
 
     // read block header from file
     char header[Constants::BGZF_BLOCK_HEADER_LENGTH];
@@ -461,7 +460,7 @@ size_t BgzfStream::Write(const char* data, const size_t dataLength) {
         numBytesWritten += copyLength;
 
         // flush (& compress) output buffer when full
-        if ( m_blockOffset == blockLength )
+        if ( m_blockOffset == static_cast<int32_t>(blockLength) )
             FlushBlock();
     }
 
index 47b360904740420ff919748a07fdf09756c6e111..a386c1a3a8f7863cb4d40edb26276bcd4cd5a307 100644 (file)
@@ -2,7 +2,7 @@
 // BgzfStream_p.h (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 25 October 2011(DB)
+// Last modified: 17 January 2012(DB)
 // ---------------------------------------------------------------------------
 // Based on BGZF routines developed at the Broad Institute.
 // Provides the basic functionality for reading & writing BGZF files
@@ -61,7 +61,7 @@ class BgzfStream {
     // internal methods
     private:
         // compresses the current block
-        size_t DeflateBlock(void);
+        size_t DeflateBlock(int32_t blockLength);
         // flushes the data in the BGZF block
         void FlushBlock(void);
         // de-compresses the current block
@@ -76,9 +76,9 @@ class BgzfStream {
 
     // data members
     public:
-        unsigned int m_blockLength;
-        unsigned int m_blockOffset;
-        uint64_t     m_blockAddress;
+        int32_t m_blockLength;
+        int32_t m_blockOffset;
+        int64_t m_blockAddress;
 
         bool m_isWriteCompressed;
         IBamIODevice* m_device;