]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/internal/BgzfStream_p.cpp
Removed some debugging 'error string' messages that snuck into last
[bamtools.git] / src / api / internal / BgzfStream_p.cpp
index f70b97eac1eb692035040ebf26a23bdc3686f75b..5891067a8ab17f154d2a37ad73e42ba8c9609eec 100644 (file)
@@ -2,19 +2,23 @@
 // BgzfStream_p.cpp (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 7 October 2011(DB)
+// Last modified: 11 October 2011(DB)
 // ---------------------------------------------------------------------------
 // Based on BGZF routines developed at the Broad Institute.
 // Provides the basic functionality for reading & writing BGZF files
 // Replaces the old BGZF.* files to avoid clashing with other toolkits
 // ***************************************************************************
 
-#include <api/internal/BamDeviceFactory_p.h>
-#include <api/internal/BamException_p.h>
-#include <api/internal/BgzfStream_p.h>
+#include "api/BamAux.h"
+#include "api/BamConstants.h"
+#include "api/internal/BamDeviceFactory_p.h"
+#include "api/internal/BamException_p.h"
+#include "api/internal/BgzfStream_p.h"
 using namespace BamTools;
 using namespace BamTools::Internal;
 
+#include "zlib.h"
+
 #include <cstring>
 #include <algorithm>
 #include <iostream>
@@ -72,12 +76,6 @@ bool BgzfStream::CheckBlockHeader(char* header) {
 // closes BGZF file
 void BgzfStream::Close(void) {
 
-    // reset state
-    m_blockLength = 0;
-    m_blockOffset = 0;
-    m_blockAddress = 0;
-    m_isWriteCompressed = true;
-
     // skip if no device open
     if ( m_device == 0 ) return;
 
@@ -93,6 +91,12 @@ void BgzfStream::Close(void) {
     m_device->Close();
     delete m_device;
     m_device = 0;
+
+    // reset state
+    m_blockLength = 0;
+    m_blockOffset = 0;
+    m_blockAddress = 0;
+    m_isWriteCompressed = true;
 }
 
 // compresses the current block
@@ -150,16 +154,16 @@ size_t BgzfStream::DeflateBlock(void) {
 
             deflateEnd(&zs);
 
-            // if error status
-            if ( status != Z_OK )
-                throw BamException("BgzfStream::DeflateBlock", "zlib deflate failed");
-
-            // not enough space available in buffer
+            // there was not enough space available in buffer
             // try to reduce the input length & re-start loop
-            inputLength -= 1024;
-            if ( inputLength <= 0 )
-                throw BamException("BgzfStream::DeflateBlock", "input reduction failed");
-            continue;
+            if ( status == Z_OK ) {
+                inputLength -= 1024;
+                if ( inputLength < 0 )
+                    throw BamException("BgzfStream::DeflateBlock", "input reduction failed");
+                continue;
+            }
+
+            throw BamException("BgzfStream::DeflateBlock", "zlib deflate failed");
         }
 
         // finalize the compression routine
@@ -298,6 +302,7 @@ size_t BgzfStream::Read(char* data, const size_t dataLength) {
         return 0;
 
     // read blocks as needed until desired data length is retrieved
+    char* output = data;
     size_t numBytesRead = 0;
     while ( numBytesRead < dataLength ) {
 
@@ -314,12 +319,12 @@ size_t BgzfStream::Read(char* data, const size_t dataLength) {
 
         // copy data from uncompressed source buffer into data destination buffer
         const size_t copyLength = min( (dataLength-numBytesRead), (size_t)bytesAvailable );
-        memcpy(data, Resources.UncompressedBlock + m_blockOffset, copyLength);
+        memcpy(output, Resources.UncompressedBlock + m_blockOffset, copyLength);
 
         // update counters
-        m_blockOffset  += copyLength;
-        data         += copyLength;
-        numBytesRead += copyLength;
+        m_blockOffset += copyLength;
+        output        += copyLength;
+        numBytesRead  += copyLength;
     }
 
     // update block data