Index: src/xplcommon/Buffer.cc
===================================================================
--- src/xplcommon/Buffer.cc	(revision 5)
+++ src/xplcommon/Buffer.cc	(revision 5)
@@ -0,0 +1,67 @@
+// 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 5)
+++ src/xplcommon/Buffer.h	(revision 5)
@@ -0,0 +1,210 @@
+// 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.
+
+#ifndef XPLCOMMON_BUFFER_H
+#define XPLCOMMON_BUFFER_H
+//------------------------------------------------------------------------------
+
+#include <algorithm>
+
+#include <cstdlib>
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon {
+
+//------------------------------------------------------------------------------
+
+/**
+ * Base class for buffers. It maintains a memory area of a certain
+ * capacity and the length of the data in the buffer.
+ */
+class Buffer
+{
+private:
+    /**
+     * The data itself.
+     */
+    unsigned char* data;
+
+    /**
+     * The capacity of the buffer.
+     */
+    size_t capacity;
+
+    /**
+     * The amount of data in the buffer.
+     */
+    size_t length;
+
+public:
+    /**
+     * Construct the buffer with the given capacity.
+     */
+    Buffer(size_t capacity);
+
+    /**
+     * Destroy the buffer.
+     */
+    ~Buffer();
+
+    /**
+     * Get the capacity of the buffer.
+     */
+    size_t getCapacity() const;
+
+    /**
+     * Get the pointer to the data.
+     */
+    const unsigned char* getData() const;
+
+    /**
+     * Get the pointer to the data for modification.
+     */
+    unsigned char* getData();
+
+    /**
+     * Get the length of the data.
+     */
+    size_t getLength() const;
+
+    /**
+     * Get the available capacity in the buffer.
+     */
+    size_t getAvailable() const;
+
+    /**
+     * Reset the buffer.
+     */
+    void reset();
+
+    /**
+     * Add the given amount to the length. Should not exceed the
+     * capacity!
+     *
+     * @return the actual amount added.
+     */
+    size_t addLength(size_t n);
+
+    /**
+     * Append the given data to the contents of the given buffer.
+     *
+     * @return the number of bytes actually appended
+     */
+    size_t append(const void* src, size_t size);
+
+    /**
+     * Extract data into the given buffer from the given offset.
+     *
+     * @return the number of bytes actually extracted.
+     */
+    size_t extract(void* dest, size_t size, size_t offset = 0);
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Buffer::Buffer(size_t capacity) :
+    data(new unsigned char[capacity]),
+    capacity(capacity),
+    length(0)
+{
+}
+
+//------------------------------------------------------------------------------
+
+inline Buffer::~Buffer()
+{
+    delete[] data;
+}
+
+//------------------------------------------------------------------------------
+
+inline size_t Buffer::getCapacity() const
+{
+    return capacity;
+}
+
+//------------------------------------------------------------------------------
+
+inline const unsigned char* Buffer::getData() const
+{
+    return data;
+}
+
+//------------------------------------------------------------------------------
+
+inline unsigned char* Buffer::getData()
+{
+    return data;
+}
+
+//------------------------------------------------------------------------------
+
+inline size_t Buffer::getLength() const
+{
+    return length;
+}
+
+//------------------------------------------------------------------------------
+
+inline size_t Buffer::getAvailable() const
+{
+    return capacity - length;
+}
+
+//------------------------------------------------------------------------------
+
+inline void Buffer::reset()
+{
+    length = 0;
+}
+
+//------------------------------------------------------------------------------
+
+inline size_t Buffer::addLength(size_t n)
+{
+    size_t toAdd = std::min(n, getAvailable());
+    length += toAdd;
+    return toAdd;
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_BUFFER_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/Makefile.am
===================================================================
--- src/xplcommon/Makefile.am	(revision 4)
+++ src/xplcommon/Makefile.am	(revision 5)
@@ -9,5 +9,6 @@
 lib_LTLIBRARIES=libxplcommon.la
 
-libxplcommon_la_SOURCES=
+libxplcommon_la_SOURCES= \
+	Buffer.cc
 
 if TARGET_API_POSIX
@@ -24,3 +25,4 @@
 	Thread.h	\
 	Waiter.h	\
-	WaitableEvent.h
+	WaitableEvent.h	\
+	Buffer.h
