Index: src/xplcommon/BlockingStream.cc
===================================================================
--- src/xplcommon/BlockingStream.cc	(revision 29)
+++ src/xplcommon/BlockingStream.cc	(revision 30)
@@ -68,33 +68,12 @@
     ReadingBuffer& readingBuffer = stream.getReadingBuffer();
     while (length>0) {
-        size_t toCopy = min(readingBuffer.getLength() - readingOffset, length);
-        if (toCopy>0) {
-            memcpy(d, readingBuffer.getData() + readingOffset, toCopy);
-            readingOffset += toCopy;
-            length -= toCopy;
-            d += toCopy;
+        size_t copied = readingBuffer.extract(d, length, readingOffset);
+        readingOffset += copied;
+        length -= copied;
+        d += copied;
+
+        if (length!=0) {
+            if (!fill()) return false;
         }
-
-        if (length==0) break;
-
-        readingBuffer.reset();
-        readingOffset = 0;
-        while (!failed()) {
-            if (isInterrupted()) return false;
-
-            if (readingBuffer.read()) {
-                if (readingBuffer.isEmpty()) return false;
-                break;
-            } else if (readingBuffer.failed()) {
-                setErrorCode(readingBuffer.getErrorCode());
-            } else {
-                Waiter* waiter = stream.getWaiter();
-                waiter->wait();
-                if (waiter->failed()) {
-                    setErrorCode(waiter->getErrorCode());
-                }
-            }
-        }
-        if (failed()) return false;
     }
 
@@ -110,12 +89,7 @@
     WritingBuffer& writingBuffer = stream.getWritingBuffer();
     while(length>0) {
-        size_t toCopy = min(length, writingBuffer.getAvailable());
-        if (toCopy>0) {
-            memcpy(writingBuffer.getData() + writingBuffer.getLength(),
-                   s, toCopy);
-            writingBuffer.addLength(toCopy);
-            length -= toCopy;
-            s += toCopy;
-        }
+        size_t copied = writingBuffer.append(s, length);
+        length -= copied;
+        s += copied;
 
         if (length==0) break;
@@ -152,4 +126,31 @@
 //------------------------------------------------------------------------------
 
+bool BlockingStream::fill()
+{
+    ReadingBuffer& readingBuffer = stream.getReadingBuffer();
+
+    readingBuffer.reset();
+    readingOffset = 0;
+    while (true) {
+        if (isInterrupted()) return false;
+
+        if (readingBuffer.read()) {
+            return !readingBuffer.isEmpty();
+        } else if (readingBuffer.failed()) {
+            setErrorCode(readingBuffer.getErrorCode());
+            return false;
+        } else {
+            Waiter* waiter = stream.getWaiter();
+            waiter->wait();
+            if (waiter->failed()) {
+                setErrorCode(waiter->getErrorCode());
+                return false;
+            }
+        }
+    }
+}
+
+//------------------------------------------------------------------------------
+
 // Local Variables:
 // mode: C++
Index: src/xplcommon/BlockingStream.h
===================================================================
--- src/xplcommon/BlockingStream.h	(revision 29)
+++ src/xplcommon/BlockingStream.h	(revision 30)
@@ -119,4 +119,10 @@
      */
     bool flush();
+
+private:
+    /**
+     * Fill the buffer with data from the stream.
+     */
+    bool fill();
 };
 
Index: c/xplcommon/Buffer.cc
===================================================================
--- src/xplcommon/Buffer.cc	(revision 5)
+++ 	(revision )
@@ -1,67 +1,0 @@
-// Copyright (c) 2012 by István Váradi
-
-// This file is part of libxplcommon, a common utility library for
-// development related to X-Plane
-
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are met:
-
-// 1. Redistributions of source code must retain the above copyright notice, this
-//    list of conditions and the following disclaimer.
-// 2. Redistributions in binary form must reproduce the above copyright notice,
-//    this list of conditions and the following disclaimer in the documentation
-//    and/or other materials provided with the distribution.
-
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
-// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-// The views and conclusions contained in the software and documentation are those
-// of the authors and should not be interpreted as representing official policies,
-// either expressed or implied, of the FreeBSD Project.
-
-//------------------------------------------------------------------------------
-
-#include "Buffer.h"
-
-#include <cstring>
-
-//------------------------------------------------------------------------------
-
-using xplcommon::Buffer;
-
-using std::min;
-
-//------------------------------------------------------------------------------
-
-size_t Buffer::append(const void* src, size_t size)
-{
-    size_t toCopy = min(size, getAvailable());
-    memcpy(data + length, src, toCopy);
-    length += toCopy;
-    return toCopy;
-}
-
-//------------------------------------------------------------------------------
-
-size_t Buffer::extract(void* dest, size_t size, size_t offset)
-{
-    size_t toCopy = (offset>=length) ? 0 : min(size, length - offset);
-    memcpy(dest, data + offset, toCopy);
-    return toCopy;
-}
-
-//------------------------------------------------------------------------------
-
-// Local Variables:
-// mode: C++
-// c-basic-offset: 4
-// indent-tabs-mode: nil
-// End:
Index: src/xplcommon/Buffer.h
===================================================================
--- src/xplcommon/Buffer.h	(revision 29)
+++ src/xplcommon/Buffer.h	(revision 30)
@@ -35,4 +35,5 @@
 
 #include <cstdlib>
+#include <cstring>
 
 //------------------------------------------------------------------------------
@@ -130,5 +131,5 @@
      * @return the number of bytes actually extracted.
      */
-    size_t extract(void* dest, size_t size, size_t offset = 0);
+    size_t extract(void* dest, size_t size, size_t offset = 0) const;
 };
 
@@ -207,4 +208,23 @@
     length += toAdd;
     return toAdd;
+}
+
+//------------------------------------------------------------------------------
+
+inline size_t Buffer::append(const void* src, size_t size)
+{
+    size_t toCopy = std::min(size, getAvailable());
+    memcpy(data + length, src, toCopy);
+    length += toCopy;
+    return toCopy;
+}
+
+//------------------------------------------------------------------------------
+
+inline size_t Buffer::extract(void* dest, size_t size, size_t offset) const
+{
+    size_t toCopy = (offset>=length) ? 0 : std::min(size, length - offset);
+    memcpy(dest, data + offset, toCopy);
+    return toCopy;
 }
 
Index: src/xplcommon/Makefile.am
===================================================================
--- src/xplcommon/Makefile.am	(revision 29)
+++ src/xplcommon/Makefile.am	(revision 30)
@@ -11,5 +11,4 @@
 libxplcommon_la_SOURCES= \
 	PseudoRandom.cc		\
-	Buffer.cc		\
 	BlockingStream.cc
 
Index: test/testblkstream.cc
===================================================================
--- test/testblkstream.cc	(revision 29)
+++ test/testblkstream.cc	(revision 30)
@@ -274,6 +274,6 @@
     clientThread.start();
 
-    //Thread::sleep(60000);
-    Thread::sleep(5000);
+    Thread::sleep(60000);
+    //Thread::sleep(5000);
     printf("Signalling the server thread\n");
     serverThread.interrupt();
