activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r559882 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io: BufferedInputStream.cpp BufferedInputStream.h
Date Thu, 26 Jul 2007 17:00:30 GMT
Author: tabish
Date: Thu Jul 26 10:00:27 2007
New Revision: 559882

URL: http://svn.apache.org/viewvc?view=rev&rev=559882
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103

Attempting to fix the code so that we meet the Contract of InputStream

Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.h

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.cpp?view=diff&rev=559882&r1=559881&r2=559882
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.cpp Thu Jul
26 10:00:27 2007
@@ -34,7 +34,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 BufferedInputStream::BufferedInputStream( InputStream* stream,
     std::size_t bufferSize,
-    bool own  )
+    bool own )
 : FilterInputStream( stream, own )
 {
     init( bufferSize );
@@ -72,7 +72,13 @@
         // If we don't have any data buffered yet - read as much as
         // we can.
         if( isEmpty() ){
-            bufferData();
+
+            // If we hit EOF without getting any Data, then throw IOException
+            if( bufferData() == -1 ){
+                throw IOException(
+                    __FILE__, __LINE__,
+                    "BufferedInputStream::read - EOF has been Reached");
+            }
         }
 
         // Get the next character.
@@ -89,13 +95,18 @@
                                std::size_t targetBufferSize ) throw ( IOException ){
 
     try{
+
+        // For zero, do nothing
+        if( targetBufferSize == 0 ) {
+            return 0;
+        }
+
         // If there's no data left, reset to pointers to the beginning of the
         // buffer.
         normalizeBuffer();
 
         // If we still haven't filled the output buffer AND there is data
-        // on the input stream to be read, read a buffer's
-        // worth from the stream.
+        // on the input stream to be read, read a buffer's worth from the stream.
         std::size_t totalRead = 0;
         while( totalRead < targetBufferSize ){
 
@@ -119,8 +130,10 @@
             // read more data.
             if( totalRead < targetBufferSize ){
 
-                // Buffer as much data as we can.
-                bufferData();
+                // Buffer as much data as we can, return EOF if we hit it.
+                if( bufferData() == -1 ) {
+                    return -1;
+                }
             }
         }
 
@@ -171,7 +184,7 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void BufferedInputStream::bufferData() throw ( IOException ){
+int BufferedInputStream::bufferData() throw ( IOException ){
 
     try{
         if( getUnusedBytes() == 0 ){
@@ -187,14 +200,21 @@
         std::size_t bytesToRead = max( (std::size_t)1, min( available, getUnusedBytes() )
);
 
         // Read the bytes from the input stream.
-        std::size_t bytesRead = inputStream->read( getTail(), bytesToRead );
+        int bytesRead = inputStream->read( getTail(), bytesToRead );
         if( bytesRead == 0 ){
             throw IOException( __FILE__, __LINE__,
                 "BufferedInputStream::read() - failed reading bytes from stream");
         }
 
+        // Dont add -1 to tail if we hit EOF
+        if( bytesRead == -1 ) {
+            return bytesRead;
+        }
+
         // Increment the tail to the new end position.
         tail += bytesRead;
+
+        return bytesRead;
     }
     DECAF_CATCH_RETHROW( IOException )
     DECAF_CATCHALL_THROW( IOException )

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.h?view=diff&rev=559882&r1=559881&r2=559882
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/io/BufferedInputStream.h Thu Jul
26 10:00:27 2007
@@ -98,7 +98,7 @@
          * until the requested number of bytes are available.
          * @param buffer (out) the target buffer.
          * @param bufferSize the size of the output buffer.
-         * @return The number of bytes read.
+         * @return The number of bytes read or -1 if EOF
          * @throws IOException thrown if an error occurs.
          */
         virtual int read( unsigned char* buffer, std::size_t bufferSize )
@@ -134,9 +134,10 @@
         /**
          * Populates the buffer with as much data as possible
          * from the target input stream.
+         * @returns total bytes read, or -1 if EOF.
          * @throws CMSException
          */
-        void bufferData() throw ( IOException );
+        int bufferData() throw ( IOException );
 
         /**
          * Returns the number of bytes that are currently unused



Mime
View raw message