You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2008/05/20 13:57:32 UTC

svn commit: r658213 [1/2] - in /mina/branches/buffer/core/src: main/java/org/apache/mina/queue/ main/java/org/apache/mina/util/byteaccess/ test/java/org/apache/mina/common/

Author: trustin
Date: Tue May 20 04:57:32 2008
New Revision: 658213

URL: http://svn.apache.org/viewvc?rev=658213&view=rev
Log:
* Added org.apache.mina.queue package as the first step
** Added IoQueue
** Added IoQueueListener
** Added ByteBufferQueue (extends IoQueue<ByteBuffer>)
* Applied Rich's patch - his implementation will be reused when I implement the default IoQueue implementation.

Added:
    mina/branches/buffer/core/src/main/java/org/apache/mina/queue/
    mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java   (with props)
    mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java   (with props)
    mina/branches/buffer/core/src/test/java/org/apache/mina/common/ByteAccessTest.java   (with props)

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java Tue May 20 04:57:32 2008
@@ -0,0 +1,60 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.queue;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public interface ByteBufferQueue extends IoQueue<ByteBuffer> {
+    ByteOrder order();
+
+    int length();
+
+    boolean offerByte(byte value);
+    boolean offerShort(short value);
+    boolean offerInt(int value);
+    boolean offerLong(long value);
+    boolean offerFloat(float value);
+    boolean offerDouble(double value);
+
+    byte   removeByte();
+    short  removeShort();
+    int    removeInt();
+    long   removeLong();
+    float  removeFloat();
+    double removeDouble();
+
+    byte   elementAsByte  ();
+    short  elementAsShort ();
+    int    elementAsInt   ();
+    long   elementAsLong  ();
+    float  elementAsFloat ();
+    double elementAsDouble();
+
+    byte   elementAsByte  (int byteIndex);
+    short  elementAsShort (int byteIndex);
+    int    elementAsInt   (int byteIndex);
+    long   elementAsLong  (int byteIndex);
+    float  elementAsFloat (int byteIndex);
+    double elementAsDouble(int byteIndex);
+
+    ByteBuffer toByteBuffer();
+    ByteBuffer toByteBuffer(int byteIndex, int length);
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ByteBufferQueue.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java Tue May 20 04:57:32 2008
@@ -0,0 +1,27 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.queue;
+
+import java.util.Queue;
+
+public interface IoQueue<E> extends Queue<E> {
+    void addListener(IoQueueListener<? super E> listener);
+    void removeListener(IoQueueListener<? super E> listener);
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueue.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java Tue May 20 04:57:32 2008
@@ -0,0 +1,28 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.queue;
+
+import java.util.EventListener;
+
+public interface IoQueueListener<E> extends EventListener {
+    boolean accept(IoQueue<? extends E> buffer, E o);
+    void offered(IoQueue<? extends E> buffer, E o);
+    void polled(IoQueue<? extends E> buffer, E o);
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/IoQueueListener.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java Tue May 20 04:57:32 2008
@@ -0,0 +1,259 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Collections;
+
+/**
+ * A <code>ByteArray</code> backed by a <code>ByteBuffer</code>. This class
+ * is abstract. Subclasses need to override the <code>free()</code> method. An
+ * implementation backed by a heap <code>ByteBuffer</code> can be created with
+ * a <code>SimpleByteArrayFactory</code>.
+ *
+ */
+public abstract class BufferByteArray implements ByteArray {
+
+    /**
+     * The backing <code>ByteBuffer</code>.
+     */
+    protected ByteBuffer bb;
+
+    public BufferByteArray(ByteBuffer bb) {
+        this.bb = bb;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Iterable<ByteBuffer> getByteBuffers() {
+        return Collections.singletonList(bb);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public ByteBuffer getSingleByteBuffer() {
+        return bb;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public abstract void free();
+
+    /**
+     * @inheritDoc
+     */
+    public Cursor cursor() {
+        return new CursorImpl();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Cursor cursor(int index) {
+        return new CursorImpl(index);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int first() {
+        return 0;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int last() {
+        return bb.limit();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int length() {
+        return last() - first();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public ByteOrder order() {
+        return bb.order();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void order(ByteOrder order) {
+        bb.order(order);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public byte get(int index) {
+        return bb.get(index);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void put(int index, byte b) {
+        bb.put(index, b);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void get(int index, ByteBuffer other) {
+        bb.position(index);
+        other.put(bb);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void put(int index, ByteBuffer other) {
+        bb.position(index);
+        bb.put(other);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int getInt(int index) {
+        return bb.getInt(index);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void putInt(int index, int i) {
+        bb.putInt(index, i);
+    }
+
+    private class CursorImpl implements Cursor {
+
+        private int index;
+
+        public CursorImpl() {
+            // This space intentionally blank.
+        }
+
+        public CursorImpl(int index) {
+            setIndex(index);
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public int getRemaining() {
+            return last() - index;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public boolean hasRemaining() {
+            return getRemaining() > 0;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public int getIndex() {
+            return index;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void setIndex(int index) {
+            if (index < 0 || index > last()) {
+                throw new IndexOutOfBoundsException();
+            }
+            this.index = index;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public ByteOrder order() {
+            return BufferByteArray.this.order();
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public byte get() {
+            byte b = BufferByteArray.this.get(index);
+            index += 1;
+            return b;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void put(byte b) {
+            BufferByteArray.this.put(index, b);
+            index += 1;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void get(ByteBuffer bb) {
+            int size = bb.remaining();
+            BufferByteArray.this.get(index, bb);
+            index += size;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void put(ByteBuffer bb) {
+            int size = bb.remaining();
+            BufferByteArray.this.put(index, bb);
+            index += size;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public int getInt() {
+            int i = BufferByteArray.this.getInt(index);
+            index += 4;
+            return i;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void putInt(int i) {
+            BufferByteArray.this.putInt(index, i);
+            index += 4;
+        }
+
+    }
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java Tue May 20 04:57:32 2008
@@ -0,0 +1,144 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Represents a sequence of bytes that can be read or written directly or
+ * through a cursor.
+ */
+public interface ByteArray extends IoAbsoluteReader, IoAbsoluteWriter {
+
+    /**
+     * @inheritDoc
+     */
+    int first();
+
+    /**
+     * @inheritDoc
+     */
+    int last();
+
+    /**
+     * @inheritDoc
+     */
+    ByteOrder order();
+
+    /**
+     * Set the byte order of the array.
+     */
+    void order(ByteOrder order);
+
+    /**
+     * Remove any resources associated with this object. Using the object after
+     * this method is called may result in undefined behaviour.
+     */
+    void free();
+
+    /**
+     * Get the sequence of <code>ByteBuffer</code>s that back this array.
+     * Compared to <code>getSingleByteBuffer()</code>, this method should be
+     * relatively efficient for all implementations.
+     */
+    Iterable<ByteBuffer> getByteBuffers();
+
+    /**
+     * Gets a single <code>ByteBuffer</code> that backs this array. Some
+     * implementations may initially have data split across multiple buffers, so
+     * calling this method may require a new buffer to be allocated and
+     * populated.
+     */
+    ByteBuffer getSingleByteBuffer();
+
+    /**
+     * @inheritDoc
+     */
+    byte get(int index);
+
+    /**
+     * @inheritDoc
+     */
+    public void get(int index, ByteBuffer bb);
+
+    /**
+     * @inheritDoc
+     */
+    int getInt(int index);
+
+    /**
+     * Get a cursor starting at index 0 (which may not be the start of the array).
+     */
+    Cursor cursor();
+
+    /**
+     * Get a cursor starting at the given index.
+     */
+    Cursor cursor(int index);
+
+    /**
+     * Provides relocatable, relative access to the underlying array. Multiple
+     * cursors may be used simultaneously, and cursors will stay consistent with
+     * the underlying array, even across modifications.
+     *
+     * Should this be <code>Cloneable</code> to allow cheap mark/position
+     * emulation?
+     */
+    public interface Cursor extends IoRelativeReader, IoRelativeWriter {
+
+        /**
+         * Gets the current index of the cursor.
+         */
+        int getIndex();
+
+        /**
+         * Sets the current index of the cursor. No bounds checking will occur
+         * until an access occurs.
+         */
+        void setIndex(int index);
+
+        /**
+         * @inheritDoc
+         */
+        int getRemaining();
+
+        /**
+         * @inheritDoc
+         */
+        boolean hasRemaining();
+
+        /**
+         * @inheritDoc
+         */
+        byte get();
+
+        /**
+         * @inheritDoc
+         */
+        void get(ByteBuffer bb);
+
+        /**
+         * @inheritDoc
+         */
+        int getInt();
+    }
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java Tue May 20 04:57:32 2008
@@ -0,0 +1,27 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+/**
+ * A factory for <code>ByteArray</code>s.
+ */
+public interface ByteArrayFactory {
+    ByteArray create(int size);
+}
\ No newline at end of file

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java Tue May 20 04:57:32 2008
@@ -0,0 +1,197 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.util.NoSuchElementException;
+
+/**
+ * A linked list that stores <code>ByteArray</code>s and maintains several useful invariants.
+ *
+ * TODO: Explain invariants.
+ */
+class ByteArrayList {
+
+    /**
+     * A {@link Node} which indicates the start and end of the list and does not
+     * hold a value. The value of <code>next</code> is the first item in the
+     * list. The value of of <code>previous</code> is the last item in the list.
+     */
+    private final Node header;
+
+    private int firstByte;
+
+    private int lastByte;
+
+    protected ByteArrayList() {
+        header = new Node();
+    }
+
+    public int lastByte() {
+        return lastByte;
+    }
+
+    public int firstByte() {
+        return firstByte;
+    }
+
+    public boolean isEmpty() {
+        return header.next == header;
+    }
+
+    public Node getFirst() {
+        return header.getNextNode();
+    }
+
+    public Node getLast() {
+        return header.getPreviousNode();
+    }
+
+    public void addFirst(ByteArray ba) {
+        addNode(new Node(ba), header.next);
+        firstByte -= ba.last();
+    }
+
+    public void addLast(ByteArray ba) {
+        addNode(new Node(ba), header);
+        lastByte += ba.last();
+    }
+
+    public Node removeFirst() {
+        Node node = header.getNextNode();
+        firstByte += node.ba.last();
+        return removeNode(node);
+    }
+
+    public Node removeLast() {
+        Node node = header.getPreviousNode();
+        lastByte -= node.ba.last();
+        return removeNode(node);
+    }
+
+    //-----------------------------------------------------------------------
+
+    /**
+     * Inserts a new node into the list.
+     *
+     * @param nodeToInsert  new node to insert
+     * @param insertBeforeNode  node to insert before
+     * @throws NullPointerException if either node is null
+     */
+    protected void addNode(Node nodeToInsert, Node insertBeforeNode) {
+        // Insert node.
+        nodeToInsert.next = insertBeforeNode;
+        nodeToInsert.previous = insertBeforeNode.previous;
+        insertBeforeNode.previous.next = nodeToInsert;
+        insertBeforeNode.previous = nodeToInsert;
+    }
+
+    /**
+     * Removes the specified node from the list.
+     *
+     * @param node  the node to remove
+     * @throws NullPointerException if <code>node</code> is null
+     */
+    protected Node removeNode(Node node) {
+        // Remove node.
+        node.previous.next = node.next;
+        node.next.previous = node.previous;
+        node.removed = true;
+        return node;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * A node within the linked list.
+     * <p>
+     * From Commons Collections 3.1, all access to the <code>value</code> property
+     * is via the methods on this class.
+     */
+    public class Node {
+
+        /** A pointer to the node before this node */
+        private Node previous;
+        /** A pointer to the node after this node */
+        private Node next;
+        /** The ByteArray contained within this node */
+        private ByteArray ba;
+        private boolean removed;
+
+        /**
+         * Constructs a new header node.
+         */
+        private Node() {
+            super();
+            previous = this;
+            next = this;
+        }
+
+        /**
+         * Constructs a new node with a value.
+         */
+        private Node(ByteArray ba) {
+            super();
+            if (ba == null) {
+                throw new NullPointerException("ByteArray must not be null.");
+            }
+            this.ba = ba;
+        }
+
+        /**
+         * Gets the previous node.
+         *
+         * @return the previous node
+         */
+        public Node getPreviousNode() {
+            if (!hasPreviousNode()) {
+                throw new NoSuchElementException();
+            }
+            return previous;
+        }
+
+        /**
+         * Gets the next node.
+         *
+         * @return the next node
+         */
+        public Node getNextNode() {
+            if (!hasNextNode()) {
+                throw new NoSuchElementException();
+            }
+            return next;
+        }
+
+        public boolean hasPreviousNode() {
+            return previous != header;
+        }
+
+        public boolean hasNextNode() {
+            return next != header;
+        }
+
+        public ByteArray getByteArray() {
+            return ba;
+        }
+
+        public boolean isRemoved() {
+            return removed;
+        }
+    }
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java Tue May 20 04:57:32 2008
@@ -0,0 +1,132 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Stack;
+
+/**
+ * Creates <code>ByteArray</code>s, using a pool to reduce allocation where possible.
+ *
+ * WARNING: This code has never been run!
+ */
+public class ByteArrayPool implements ByteArrayFactory {
+
+    private final int MAX_BITS = 32;
+
+    private boolean freed;
+    private final boolean direct;
+    private ArrayList<Stack<DirectBufferByteArray>> freeBuffers;
+    private int freeBufferCount = 0;
+    private long freeMemory = 0;
+    private final int maxFreeBuffers;
+    private final int maxFreeMemory;
+
+    public ByteArrayPool(boolean direct, int maxFreeBuffers, int maxFreeMemory) {
+        this.direct = direct;
+        freeBuffers = new ArrayList<Stack<DirectBufferByteArray>>();
+        for (int i = 0; i < MAX_BITS; i++) {
+            freeBuffers.add(new Stack<DirectBufferByteArray>());
+        }
+        this.maxFreeBuffers = maxFreeBuffers;
+        this.maxFreeMemory = maxFreeMemory;
+    }
+
+    public ByteArray create(int size) {
+        if (size < 1) {
+            throw new IllegalArgumentException(
+                    "Buffer size must be at least 1: " + size);
+        }
+        int bits = bits(size);
+        synchronized (this) {
+            if (!freeBuffers.isEmpty()) {
+                DirectBufferByteArray ba = freeBuffers.get(bits).pop();
+                ba.setFreed(false);
+                ba.getSingleByteBuffer().limit(size);
+                return ba;
+            }
+        }
+        ByteBuffer bb;
+        int bbSize = 1 << bits;
+        if (direct) {
+            bb = ByteBuffer.allocateDirect(bbSize);
+        } else {
+            bb = ByteBuffer.allocate(bbSize);
+        }
+        bb.limit(size);
+        DirectBufferByteArray ba = new DirectBufferByteArray(bb);
+        ba.setFreed(false);
+        return ba;
+    }
+
+    private int bits(int index) {
+        int bits = 0;
+        while (1 << bits < index) {
+            bits++;
+        }
+        return bits;
+    }
+
+    public void free() {
+        synchronized (this) {
+            if (freed) {
+                throw new IllegalStateException("Already freed.");
+            }
+            freed = true;
+            freeBuffers = null;
+        }
+    }
+
+    private class DirectBufferByteArray extends BufferByteArray {
+
+        public boolean freed;
+
+        public DirectBufferByteArray(ByteBuffer bb) {
+            super(bb);
+        }
+
+        public void setFreed(boolean freed) {
+            this.freed = freed;
+        }
+
+        @Override
+        public void free() {
+            synchronized (this) {
+                if (freed) {
+                    throw new IllegalStateException("Already freed.");
+                }
+                freed = true;
+            }
+            int bits = bits(last());
+            synchronized (ByteArrayPool.this) {
+                if (freeBuffers != null && freeBufferCount < maxFreeBuffers
+                        && freeMemory + last() <= maxFreeMemory) {
+                    freeBuffers.get(bits).push(this);
+                    freeBufferCount++;
+                    freeMemory += last();
+                    return;
+                }
+            }
+        }
+
+    }
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayPool.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java Tue May 20 04:57:32 2008
@@ -0,0 +1,632 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.mina.util.byteaccess.ByteArrayList.Node;
+
+/**
+ * A ByteArray composed of other ByteArrays. Optimised for fast relative access
+ * via cursors. Absolute access methods are provided, but may perform poorly.
+ */
+public final class CompositeByteArray implements ByteArray {
+
+    /**
+     * Allows for efficient detection of component boundaries when using a cursor.
+     *
+     * TODO: Is this interface right?
+     */
+    public interface CursorListener {
+
+        /**
+         * Called when the first component in the composite is entered by the cursor.
+         */
+        public void enteredFirstComponent(int componentIndex, ByteArray component);
+
+        /**
+         * Called when the next component in the composite is entered by the cursor.
+         */
+        public void enteredNextComponent(int componentIndex, ByteArray component);
+
+        /**
+         * Called when the previous component in the composite is entered by the cursor.
+         */
+        public void enteredPreviousComponent(int componentIndex, ByteArray component);
+
+        /**
+         * Called when the last component in the composite is entered by the cursor.
+         */
+        public void enteredLastComponent(int componentIndex, ByteArray component);
+
+    }
+
+    /**
+     * Stores the underlying <code>ByteArray</code>s.
+     */
+    private final ByteArrayList bas = new ByteArrayList();
+
+    private ByteOrder order;
+
+    /**
+     * May be used in <code>getSingleByteBuffer</code>. Optional.
+     */
+    private final ByteArrayFactory byteArrayFactory;
+
+    public CompositeByteArray() {
+        this(null);
+    }
+
+    public CompositeByteArray(ByteArrayFactory byteArrayFactory) {
+        this.byteArrayFactory = byteArrayFactory;
+    }
+
+    public ByteArray getFirst() {
+        if (bas.isEmpty()) {
+            return null;
+        } else {
+            return bas.getFirst().getByteArray();
+        }
+    }
+
+    public void addFirst(ByteArray ba) {
+        handleAdd(ba);
+        bas.addFirst(ba);
+    }
+
+    public ByteArray removeFirst() {
+        Node node = bas.removeFirst();
+        return node == null ? null : node.getByteArray();
+    }
+
+    /**
+     * Remove component <code>ByteArray</code>s to the given index (splitting
+     * them if necessary) and returning them in a single <code>ByteArray</code>.
+     * The caller is responsible for freeing the returned object.
+     *
+     * TODO: Document free behaviour more thoroughly.
+     */
+    public ByteArray removeTo(int index) {
+        if (index < first() || index > last()) {
+            throw new IndexOutOfBoundsException();
+        }
+        // Optimisation when removing exactly one component.
+//        if (index == start() + getFirst().length()) {
+//            ByteArray component = getFirst();
+//            removeFirst();
+//            return component;
+//        }
+        // Removing
+        CompositeByteArray prefix = new CompositeByteArray(byteArrayFactory);
+        int remaining = index - first();
+        while (remaining > 0) {
+            ByteArray component = removeFirst();
+            if (component.last() <= remaining) {
+                // Remove entire component.
+                prefix.addLast(component);
+                remaining -= component.last();
+            } else {
+                // Remove part of component. Do this by removing entire
+                // component
+                // then readding remaining bytes.
+                // TODO: Consider using getByteBuffers(), as more generic.
+                ByteBuffer bb = component.getSingleByteBuffer();
+                int originalLimit = bb.limit();
+                bb.position(0);
+                bb.limit(remaining);
+                ByteBuffer bb1 = bb.slice();
+                bb.position(remaining);
+                bb.limit(originalLimit);
+                ByteBuffer bb2 = bb.slice();
+                ByteArray ba1 = new BufferByteArray(bb1) {
+                    @Override
+                    public void free() {
+                        // Do not free.
+                    }
+                };
+                prefix.addLast(ba1);
+                remaining -= ba1.last();
+                final ByteArray componentFinal = component; // final for
+                // anonymous inner
+                // class
+                ByteArray ba2 = new BufferByteArray(bb2) {
+                    @Override
+                    public void free() {
+                        componentFinal.free();
+                    }
+                };
+                addFirst(ba2);
+            }
+        }
+        return prefix;
+    }
+
+    public void addLast(ByteArray ba) {
+        handleAdd(ba);
+        bas.addLast(ba);
+    }
+
+    public ByteArray removeLast() {
+        Node node = bas.removeLast();
+        return node == null ? null : node.getByteArray();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void free() {
+        while (!bas.isEmpty()) {
+            Node node = bas.getLast();
+            node.getByteArray().free();
+            bas.removeLast();
+        }
+    }
+
+    private void checkBounds(int index, int accessSize) {
+        int lower = index;
+        int upper = index + accessSize;
+        if (lower < first()) {
+            throw new IndexOutOfBoundsException("Index " + lower
+                    + " less than start " + first() + ".");
+        }
+        if (upper > last()) {
+            throw new IndexOutOfBoundsException("Index " + upper
+                    + " greater than length " + last() + ".");
+        }
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Iterable<ByteBuffer> getByteBuffers() {
+        if (bas.isEmpty()) {
+            return Collections.emptyList();
+        }
+        Collection<ByteBuffer> result = new ArrayList<ByteBuffer>();
+        Node node = bas.getFirst();
+        for (ByteBuffer bb : node.getByteArray().getByteBuffers()) {
+            result.add(bb);
+        }
+        while (node.hasNextNode()) {
+            node = node.getNextNode();
+            for (ByteBuffer bb : node.getByteArray().getByteBuffers()) {
+                result.add(bb);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public ByteBuffer getSingleByteBuffer() {
+        if (byteArrayFactory == null) {
+            throw new IllegalStateException(
+                    "Can't get single buffer from CompositeByteArray unless it has a ByteArrayFactory.");
+        }
+        if (bas.isEmpty()) {
+            ByteArray ba = byteArrayFactory.create(1);
+            return ba.getSingleByteBuffer();
+        }
+        int actualLength = last() - first();
+        {
+            Node node = bas.getFirst();
+            ByteArray ba = node.getByteArray();
+            if (ba.last() == actualLength) {
+                return ba.getSingleByteBuffer();
+            }
+        }
+        // Replace all nodes with a single node.
+        ByteArray target = byteArrayFactory.create(actualLength);
+        ByteBuffer bb = target.getSingleByteBuffer();
+        Cursor cursor = cursor();
+        cursor.put(bb); // Copy all existing data into target ByteBuffer.
+        while (!bas.isEmpty()) {
+            Node node = bas.getLast();
+            ByteArray component = node.getByteArray();
+            bas.removeLast();
+            component.free();
+        }
+        bas.addLast(target);
+        return bb;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Cursor cursor() {
+        return new CursorImpl();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Cursor cursor(int index) {
+        return new CursorImpl(index);
+    }
+
+    /**
+     * Get a cursor starting at index 0 (which may not be the start of the
+     * array) and with the given listener.
+     */
+    public Cursor cursor(CursorListener listener) {
+        return new CursorImpl(listener);
+    }
+
+    /**
+     * Get a cursor starting at the given index and with the given listener.
+     */
+    public Cursor cursor(int index, CursorListener listener) {
+        return new CursorImpl(index, listener);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public byte get(int index) {
+        return cursor(index).get();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void put(int index, byte b) {
+        cursor(index).put(b);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void get(int index, ByteBuffer bb) {
+        cursor(index).get(bb);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void put(int index, ByteBuffer bb) {
+        cursor(index).put(bb);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int getInt(int index) {
+        return cursor(index).getInt();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void putInt(int index, int i) {
+        cursor(index).putInt(i);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int first() {
+        return bas.firstByte();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int last() {
+        return bas.lastByte();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public int length() {
+        return last() - first();
+    }
+
+    private void handleAdd(ByteArray ba) {
+        // Check first() is zero, otherwise cursor might not work.
+        // TODO: Remove this restriction?
+        if (ba.first() != 0) {
+            throw new IllegalArgumentException(
+                    "Cannot add byte array that doesn't start from 0: "
+                            + ba.first());
+        }
+        // Check order.
+        if (order == null) {
+            order = ba.order();
+        } else if (!order.equals(ba.order())) {
+            throw new IllegalArgumentException(
+                    "Cannot add byte array with different byte order: "
+                            + ba.order());
+        }
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public ByteOrder order() {
+        if (order == null) {
+            throw new IllegalStateException("Byte order not yet set.");
+        }
+        return order;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void order(ByteOrder order) {
+        if (order == null || !order.equals(this.order)) {
+            this.order = order;
+            if (!bas.isEmpty()) {
+                for (Node node = bas.getFirst(); node.hasNextNode(); node = node
+                        .getNextNode()) {
+                    node.getByteArray().order(order);
+                }
+            }
+        }
+    }
+
+    private class CursorImpl implements Cursor {
+
+        private int index;
+
+        private final CursorListener listener;
+
+        private Node componentNode;
+
+        // Index of start of current component.
+        private int componentIndex;
+
+        // Cursor within current component.
+        private ByteArray.Cursor componentCursor;
+
+        public CursorImpl() {
+            this(0, null);
+        }
+
+        public CursorImpl(int index) {
+            this(index, null);
+        }
+
+        public CursorImpl(CursorListener listener) {
+            this(0, listener);
+        }
+
+        public CursorImpl(int index, CursorListener listener) {
+            this.index = index;
+            this.listener = listener;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public int getIndex() {
+            return index;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void setIndex(int index) {
+            checkBounds(index, 0);
+            this.index = index;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public ByteOrder order() {
+            return CompositeByteArray.this.order();
+        }
+
+        private void prepareForAccess(int accessSize) {
+            // Handle removed node. Do this first so we can remove the reference
+            // even if bounds checking fails.
+            if (componentNode != null && componentNode.isRemoved()) {
+                componentNode = null;
+                componentCursor = null;
+            }
+
+            // Bounds checks
+            checkBounds(index, accessSize);
+
+            // Remember the current node so we can later tell whether or not we
+            // need to create a new cursor.
+            Node oldComponentNode = componentNode;
+
+            // Handle missing node.
+            if (componentNode == null) {
+                int basMidpoint = (last() - first()) / 2 + first();
+                if (index <= basMidpoint) {
+                    // Search from the start.
+                    componentNode = bas.getFirst();
+                    componentIndex = first();
+                    if (listener != null) {
+                        listener.enteredFirstComponent(componentIndex,
+                                componentNode.getByteArray());
+                    }
+                } else {
+                    // Search from the end.
+                    componentNode = bas.getLast();
+                    componentIndex = last()
+                            - componentNode.getByteArray().last();
+                    if (listener != null) {
+                        listener.enteredLastComponent(componentIndex,
+                                componentNode.getByteArray());
+                    }
+                }
+            }
+
+            // Go back, if necessary.
+            while (index < componentIndex) {
+                componentNode = componentNode.getPreviousNode();
+                componentIndex -= componentNode.getByteArray().last();
+                if (listener != null) {
+                    listener.enteredPreviousComponent(componentIndex,
+                            componentNode.getByteArray());
+                }
+            }
+
+            // Go forward, if necessary.
+            while (index >= componentIndex + componentNode.getByteArray()
+                    .last()) {
+                componentIndex += componentNode.getByteArray().last();
+                componentNode = componentNode.getNextNode();
+                if (listener != null) {
+                    listener.enteredNextComponent(componentIndex, componentNode
+                            .getByteArray());
+                }
+            }
+
+            // Update the cursor.
+            int internalComponentIndex = index - componentIndex;
+            if (componentNode == oldComponentNode) {
+                // Move existing cursor.
+                componentCursor.setIndex(internalComponentIndex);
+            } else {
+                // Create new cursor.
+                componentCursor = componentNode.getByteArray().cursor(
+                        internalComponentIndex);
+            }
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public int getRemaining() {
+            return last() - index + 1;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public boolean hasRemaining() {
+            return getRemaining() > 0;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public byte get() {
+            prepareForAccess(1);
+            byte b = componentCursor.get();
+            index += 1;
+            return b;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void put(byte b) {
+            prepareForAccess(1);
+            componentCursor.put(b);
+            index += 1;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void get(ByteBuffer bb) {
+            prepareForAccess(bb.remaining());
+            while (bb.hasRemaining()) {
+                int chunkSize = componentCursor.getRemaining();
+                prepareForAccess(chunkSize);
+                componentCursor.get(bb);
+                index += chunkSize;
+            }
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void put(ByteBuffer bb) {
+            prepareForAccess(bb.remaining());
+            while (bb.hasRemaining()) {
+                int chunkSize = componentCursor.getRemaining();
+                prepareForAccess(chunkSize);
+                componentCursor.put(bb);
+                index += chunkSize;
+            }
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public int getInt() {
+            prepareForAccess(4);
+            if (componentCursor.getRemaining() >= 4) {
+                int i = componentCursor.getInt();
+                index += 4;
+                return i;
+            } else {
+                byte b1 = get();
+                byte b2 = get();
+                byte b3 = get();
+                byte b4 = get();
+                if (order.equals(ByteOrder.BIG_ENDIAN)) {
+                    return b1 << 24 & b2 << 16 & b3 << 8 & b4;
+                } else {
+                    return b4 << 24 & b3 << 16 & b2 << 8 & b1;
+                }
+            }
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public void putInt(int i) {
+            prepareForAccess(4);
+            if (componentCursor.getRemaining() >= 4) {
+                componentCursor.putInt(i);
+                index += 4;
+            } else {
+                byte b1;
+                byte b2;
+                byte b3;
+                byte b4;
+                if (order.equals(ByteOrder.BIG_ENDIAN)) {
+                    b4 = (byte) (i & 0xff);
+                    i >>= 8;
+                    b3 = (byte) (i & 0xff);
+                    i >>= 8;
+                    b2 = (byte) (i & 0xff);
+                    i >>= 8;
+                    b1 = (byte) (i & 0xff);
+                } else {
+                    b1 = (byte) (i & 0xff);
+                    i >>= 8;
+                    b2 = (byte) (i & 0xff);
+                    i >>= 8;
+                    b3 = (byte) (i & 0xff);
+                    i >>= 8;
+                    b4 = (byte) (i & 0xff);
+                }
+                put(b1);
+                put(b2);
+                put(b3);
+                put(b4);
+            }
+        }
+    }
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java Tue May 20 04:57:32 2008
@@ -0,0 +1,121 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteOrder;
+
+import org.apache.mina.util.byteaccess.ByteArray.Cursor;
+import org.apache.mina.util.byteaccess.CompositeByteArray.CursorListener;
+
+/**
+ * Provides common functionality between the
+ * <code>CompositeByteArrayRelativeReader</code> and
+ * <code>CompositeByteArrayRelativeWriter</code>.
+ */
+abstract class CompositeByteArrayRelativeBase {
+
+    /**
+     * The underlying <code>CompositeByteArray</code>.
+     */
+    protected final CompositeByteArray cba;
+
+    /**
+     * A cursor of the underlying <code>CompositeByteArray</code>. This
+     * cursor is never moved directly; its position only changes through calls
+     * to relative read or write methods.
+     */
+    protected final Cursor cursor;
+
+    public CompositeByteArrayRelativeBase(CompositeByteArray cba) {
+        this.cba = cba;
+        cursor = cba.cursor(cba.first(), new CursorListener() {
+
+            public void enteredFirstComponent(int componentIndex,
+                    ByteArray component) {
+                // Do nothing.
+            }
+
+            public void enteredLastComponent(int componentIndex,
+                    ByteArray component) {
+                assert false;
+            }
+
+            public void enteredNextComponent(int componentIndex,
+                    ByteArray component) {
+                cursorPassedFirstComponent();
+            }
+
+            public void enteredPreviousComponent(int componentIndex,
+                    ByteArray component) {
+                assert false;
+            }
+
+        });
+    }
+
+    public final int getRemaining() {
+        return cursor.getRemaining();
+    }
+
+    public final boolean hasRemaining() {
+        return cursor.hasRemaining();
+    }
+
+    public ByteOrder order() {
+        return cba.order();
+    }
+
+    /**
+     * Make a <code>ByteArray</code> available for access at the end of this object.
+     */
+    public final void append(ByteArray ba) {
+        cba.addLast(ba);
+    }
+
+    /**
+     * Free all resources associated with this object.
+     */
+    public final void free() {
+        cba.free();
+    }
+
+    /**
+     * Get the index that will be used for the next access.
+     */
+    public final int getIndex() {
+        return cursor.getIndex();
+    }
+
+    /**
+     * Get the index after the last byte that can be accessed.
+     */
+    public final int last() {
+        return cba.last();
+    }
+
+    /**
+     * Called whenever the cursor has passed from the <code>cba</code>'s
+     * first component. As the first component is no longer used, this provides
+     * a good opportunity for subclasses to perform some action on it (such as
+     * freeing it).
+     */
+    protected abstract void cursorPassedFirstComponent();
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java Tue May 20 04:57:32 2008
@@ -0,0 +1,66 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Provides restricted, relative, read-only access to the bytes in a
+ * <code>CompositeByteArray</code>. Using this interface has the advantage
+ * that it can be automatically determined when a component
+ * <code>ByteArray</code> can no longer be read, and thus components can be
+ * automatically freed. This makes it easier to use pooling for underlying
+ * <code>ByteArray</code>s.
+ */
+public class CompositeByteArrayRelativeReader extends
+        CompositeByteArrayRelativeBase implements IoRelativeReader {
+
+    /**
+     * Whether or not to free component <code>CompositeByteArray</code>s when
+     * the cursor moves past them.
+     */
+    private final boolean autoFree;
+
+    public CompositeByteArrayRelativeReader(CompositeByteArray cba,
+            boolean autoFree) {
+        super(cba);
+        this.autoFree = autoFree;
+    }
+
+    @Override
+    protected void cursorPassedFirstComponent() {
+        if (autoFree) {
+            cba.removeFirst().free();
+        }
+    }
+
+    public byte get() {
+        return cursor.get();
+    }
+
+    public void get(ByteBuffer bb) {
+        cursor.get(bb);
+    }
+
+    public int getInt() {
+        return cursor.getInt();
+    }
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeReader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java Tue May 20 04:57:32 2008
@@ -0,0 +1,161 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Provides restricted, relative, write-only access to the bytes in a
+ * <code>CompositeByteArray</code>.
+ *
+ * Using this interface has the advantage that it can be automatically
+ * determined when a component <code>ByteArray</code> can no longer be written
+ * to, and thus components can be automatically flushed. This makes it easier to
+ * use pooling for underlying <code>ByteArray</code>s.
+ *
+ * By providing an appropriate <code>Expander</code> it is also possible to
+ * automatically add more backing storage as more data is written.
+ *
+ * TODO: Get flushing working.
+ */
+public class CompositeByteArrayRelativeWriter extends
+        CompositeByteArrayRelativeBase implements IoRelativeWriter {
+
+    /**
+     * An object that knows how to expand a <code>CompositeByteArray</code>.
+     */
+    public interface Expander {
+        void expand(CompositeByteArray cba, int minSize);
+    }
+
+    public static class NopExpander implements Expander {
+        public void expand(CompositeByteArray cba, int minSize) {
+            // Do nothing.
+        }
+    }
+
+    public static class ChunkedExpander implements Expander {
+
+        private final ByteArrayFactory baf;
+
+        private final int newComponentSize;
+
+        public ChunkedExpander(ByteArrayFactory baf, int newComponentSize) {
+            this.baf = baf;
+            this.newComponentSize = newComponentSize;
+        }
+
+        public void expand(CompositeByteArray cba, int minSize) {
+            int remaining = minSize;
+            while (remaining > 0) {
+                ByteArray component = baf.create(newComponentSize);
+                cba.addLast(component);
+                remaining -= newComponentSize;
+            }
+        }
+
+    }
+
+    /**
+     * An object that knows how to flush a <code>ByteArray</code>.
+     */
+    public interface Flusher {
+        // document free() behaviour
+        void flush(ByteArray ba);
+    }
+
+    /**
+     * The expander to use when the array underflows.
+     */
+    private final Expander expander;
+
+    /**
+     * The flusher to use when flushing component <code>ByteArray</code>s.
+     */
+    private final Flusher flusher;
+
+    /**
+     * Whether or not to automatically flush a component once the cursor moves
+     * past it.
+     */
+    private final boolean autoFlush;
+
+    public CompositeByteArrayRelativeWriter(CompositeByteArray cba,
+            Expander expander, Flusher flusher, boolean autoFlush) {
+        super(cba);
+        this.expander = expander;
+        this.flusher = flusher;
+        this.autoFlush = autoFlush;
+    }
+
+    private void prepareForAccess(int size) {
+        int underflow = cursor.getIndex() + size - last();
+        if (underflow > 0) {
+            expander.expand(cba, underflow);
+        }
+    }
+
+    /**
+     * Flush to the current index.
+     */
+    public void flush() {
+        flushTo(cursor.getIndex());
+    }
+
+    /**
+     * Flush to the given index.
+     */
+    public void flushTo(int index) {
+        ByteArray removed = cba.removeTo(index);
+        flusher.flush(removed);
+    }
+
+    @Override
+    protected void cursorPassedFirstComponent() {
+        if (autoFlush) {
+            flushTo(cba.first() + cba.getFirst().last());
+        }
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void put(byte b) {
+        prepareForAccess(1);
+        cursor.put(b);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void put(ByteBuffer bb) {
+        prepareForAccess(bb.remaining());
+        cursor.put(bb);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void putInt(int i) {
+        prepareForAccess(4);
+        cursor.putInt(i);
+    }
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeWriter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java Tue May 20 04:57:32 2008
@@ -0,0 +1,64 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Provides absolute read access to a sequence of bytes.
+ */
+public interface IoAbsoluteReader {
+
+    /**
+     * Get the index of the first byte that can be accessed.
+     */
+    int first();
+
+    /**
+     * Gets the index after the last byte that can be accessed.
+     */
+    int last();
+
+    /**
+     * Gets the total number of bytes that can be accessed.
+     */
+    int length();
+
+    /**
+     * Gets the order of the bytes.
+     */
+    ByteOrder order();
+
+    /**
+     * Gets a <code>byte</code> from the given index.
+     */
+    byte get(int index);
+
+    /**
+     * Gets enough bytes to fill the <code>ByteBuffer</code> from the given index.
+     */
+    public void get(int index, ByteBuffer bb);
+
+    /**
+     * Gets an <code>int</code> from the given index.
+     */
+    int getInt(int index);
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteReader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java Tue May 20 04:57:32 2008
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Provides absolute write access to a sequence of bytes.
+ */
+public interface IoAbsoluteWriter {
+
+    /**
+     * Get the index of the first byte that can be accessed.
+     */
+    int first();
+
+    /**
+     * Gets the index after the last byte that can be accessed.
+     */
+    int last();
+
+    /**
+     * Gets the order of the bytes.
+     */
+    ByteOrder order();
+
+    /**
+     * Puts a <code>byte</code> at the given index.
+     */
+    void put(int index, byte b);
+
+    /**
+     * Puts bytes from the <code>ByteBuffer</code> at the given index.
+     */
+    public void put(int index, ByteBuffer bb);
+
+    /**
+     * Puts an <code>int</code> at the given index.
+     */
+    void putInt(int index, int i);
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoAbsoluteWriter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java Tue May 20 04:57:32 2008
@@ -0,0 +1,60 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Provides relative read access to a sequence of bytes.
+ */
+public interface IoRelativeReader {
+
+    /**
+     * Gets the number of remaining bytes that can be read.
+     */
+    int getRemaining();
+
+    /**
+     * Checks if there are any remaining bytes that can be read.
+     */
+    boolean hasRemaining();
+
+    /**
+     * Gets the order of the bytes.
+     */
+    ByteOrder order();
+
+    /**
+     * Gets a <code>byte</code> and advances the reader.
+     */
+    byte get();
+
+    /**
+     * Gets enough bytes to fill the <code>ByteBuffer</code> and advances the reader.
+     */
+    void get(ByteBuffer bb);
+
+    /**
+     * Gets an <code>int</code> and advances the reader.
+     */
+    int getInt();
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeReader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java Tue May 20 04:57:32 2008
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Provides relative read access to a sequence of bytes.
+ */
+public interface IoRelativeWriter {
+
+    /**
+     * Gets the number of remaining bytes that can be read.
+     */
+    int getRemaining();
+
+    /**
+     * Checks if there are any remaining bytes that can be read.
+     */
+    boolean hasRemaining();
+
+    /**
+     * Gets the order of the bytes.
+     */
+    ByteOrder order();
+
+    /**
+     * Puts a <code>byte</code> and advances the reader.
+     */
+    void put(byte b);
+
+    /**
+     * Puts enough bytes to fill the <code>ByteBuffer</code> and advances the reader.
+     */
+    void put(ByteBuffer bb);
+
+    /**
+     * Puts an <code>int</code> and advances the reader.
+     */
+    void putInt(int i);
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/IoRelativeWriter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java?rev=658213&view=auto
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java (added)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java Tue May 20 04:57:32 2008
@@ -0,0 +1,51 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.util.byteaccess;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Creates <code>ByteArray</code> backed by a heap-allocated
+ * <code>ByteBuffer</code>. The free method on returned
+ * <code>ByteArray</code>s is a nop.
+ */
+public class SimpleByteArrayFactory implements ByteArrayFactory {
+
+    /**
+     * @inheritDoc
+     */
+    public ByteArray create(int size) {
+        if (size < 0) {
+            throw new IllegalArgumentException(
+                    "Buffer size must not be negative:" + size);
+        }
+        ByteBuffer bb = ByteBuffer.allocate(size);
+        ByteArray ba = new BufferByteArray(bb) {
+
+            @Override
+            public void free() {
+                // Nothing to do.
+            }
+
+        };
+        return ba;
+    }
+
+}

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/branches/buffer/core/src/main/java/org/apache/mina/util/byteaccess/SimpleByteArrayFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date