Added some missing Javadoc Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/9c95cd14 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/9c95cd14 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/9c95cd14
Branch: refs/heads/2.0 Commit: 9c95cd14e9c955ed78703a1f780923fbe6b8b961 Parents: 2405f81 Author: Emmanuel Lécharny <elecha...@symas.com> Authored: Sun Dec 4 17:23:06 2016 +0100 Committer: Emmanuel Lécharny <elecha...@symas.com> Committed: Sun Dec 4 17:23:06 2016 +0100 ---------------------------------------------------------------------- .../mina/core/buffer/IoBufferWrapper.java | 2 +- .../mina/util/byteaccess/AbstractByteArray.java | 18 +- .../mina/util/byteaccess/BufferByteArray.java | 74 +++++- .../apache/mina/util/byteaccess/ByteArray.java | 12 + .../mina/util/byteaccess/ByteArrayList.java | 8 +- .../util/byteaccess/CompositeByteArray.java | 255 ++++++++++++------- .../CompositeByteArrayRelativeBase.java | 25 +- 7 files changed, 286 insertions(+), 108 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java index 600db21..b10d74c 100644 --- a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java +++ b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java @@ -1498,10 +1498,10 @@ public class IoBufferWrapper extends IoBuffer { return this; } - @Override /** * {@inheritDoc} */ + @Override public IoBuffer putUnsigned(short value) { buf.putUnsigned(value); return this; http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/util/byteaccess/AbstractByteArray.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/util/byteaccess/AbstractByteArray.java b/mina-core/src/main/java/org/apache/mina/util/byteaccess/AbstractByteArray.java index 6da8cfb..0036c08 100644 --- a/mina-core/src/main/java/org/apache/mina/util/byteaccess/AbstractByteArray.java +++ b/mina-core/src/main/java/org/apache/mina/util/byteaccess/AbstractByteArray.java @@ -27,13 +27,20 @@ package org.apache.mina.util.byteaccess; * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ abstract class AbstractByteArray implements ByteArray { - /** * {@inheritDoc} */ + @Override public final int length() { return last() - first(); } + + + /** + * {@inheritDoc} + */ + @Override + public abstract int hashCode(); /** * {@inheritDoc} @@ -44,36 +51,43 @@ abstract class AbstractByteArray implements ByteArray { if (other == this) { return true; } + // Compare types. if (!(other instanceof ByteArray)) { return false; } + ByteArray otherByteArray = (ByteArray) other; + // Compare properties. if (first() != otherByteArray.first() || last() != otherByteArray.last() || !order().equals(otherByteArray.order())) { return false; } + // Compare bytes. Cursor cursor = cursor(); Cursor otherCursor = otherByteArray.cursor(); + for (int remaining = cursor.getRemaining(); remaining > 0;) { // Optimization: prefer int comparisons over byte comparisons if (remaining >= 4) { int i = cursor.getInt(); int otherI = otherCursor.getInt(); + if (i != otherI) { return false; } } else { byte b = cursor.get(); byte otherB = otherCursor.get(); + if (b != otherB) { return false; } } } + return true; } - } http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java b/mina-core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java index 851b4a6..64d3ab6 100644 --- a/mina-core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java +++ b/mina-core/src/main/java/org/apache/mina/util/byteaccess/BufferByteArray.java @@ -54,6 +54,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public Iterable<IoBuffer> getIoBuffers() { return Collections.singletonList(bb); } @@ -61,6 +62,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public IoBuffer getSingleIoBuffer() { return bb; } @@ -70,6 +72,7 @@ public abstract class BufferByteArray extends AbstractByteArray { * * Calling <code>free()</code> on the returned slice has no effect. */ + @Override public ByteArray slice(int index, int length) { int oldLimit = bb.limit(); bb.position(index); @@ -88,11 +91,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ - public abstract void free(); - - /** - * {@inheritDoc} - */ + @Override public Cursor cursor() { return new CursorImpl(); } @@ -100,6 +99,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public Cursor cursor(int index) { return new CursorImpl(index); } @@ -107,6 +107,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int first() { return 0; } @@ -114,6 +115,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int last() { return bb.limit(); } @@ -121,6 +123,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public ByteOrder order() { return bb.order(); } @@ -128,6 +131,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void order(ByteOrder order) { bb.order(order); } @@ -135,6 +139,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public byte get(int index) { return bb.get(index); } @@ -142,6 +147,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(int index, byte b) { bb.put(index, b); } @@ -149,6 +155,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void get(int index, IoBuffer other) { bb.position(index); other.put(bb); @@ -157,6 +164,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(int index, IoBuffer other) { bb.position(index); bb.put(other); @@ -165,6 +173,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public short getShort(int index) { return bb.getShort(index); } @@ -172,6 +181,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putShort(int index, short s) { bb.putShort(index, s); } @@ -179,6 +189,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getInt(int index) { return bb.getInt(index); } @@ -186,6 +197,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putInt(int index, int i) { bb.putInt(index, i); } @@ -193,6 +205,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public long getLong(int index) { return bb.getLong(index); } @@ -200,6 +213,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putLong(int index, long l) { bb.putLong(index, l); } @@ -207,6 +221,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public float getFloat(int index) { return bb.getFloat(index); } @@ -214,6 +229,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putFloat(int index, float f) { bb.putFloat(index, f); } @@ -221,6 +237,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public double getDouble(int index) { return bb.getDouble(index); } @@ -228,6 +245,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putDouble(int index, double d) { bb.putDouble(index, d); } @@ -235,6 +253,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public char getChar(int index) { return bb.getChar(index); } @@ -242,6 +261,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putChar(int index, char c) { bb.putChar(index, c); } @@ -261,6 +281,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getRemaining() { return last() - index; } @@ -268,6 +289,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public boolean hasRemaining() { return getRemaining() > 0; } @@ -275,6 +297,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getIndex() { return index; } @@ -282,6 +305,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void setIndex(int index) { if (index < 0 || index > last()) { throw new IndexOutOfBoundsException(); @@ -289,10 +313,18 @@ public abstract class BufferByteArray extends AbstractByteArray { this.index = index; } + /** + * {@inheritDoc} + */ + @Override public void skip(int length) { setIndex(index + length); } + /** + * {@inheritDoc} + */ + @Override public ByteArray slice(int length) { ByteArray slice = BufferByteArray.this.slice(index, length); index += length; @@ -302,6 +334,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public ByteOrder order() { return BufferByteArray.this.order(); } @@ -309,6 +342,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public byte get() { byte b = BufferByteArray.this.get(index); index += 1; @@ -318,6 +352,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(byte b) { BufferByteArray.this.put(index, b); index += 1; @@ -326,6 +361,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void get(IoBuffer bb) { int size = Math.min(getRemaining(), bb.remaining()); BufferByteArray.this.get(index, bb); @@ -335,6 +371,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(IoBuffer bb) { int size = bb.remaining(); BufferByteArray.this.put(index, bb); @@ -344,6 +381,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public short getShort() { short s = BufferByteArray.this.getShort(index); index += 2; @@ -353,6 +391,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putShort(short s) { BufferByteArray.this.putShort(index, s); index += 2; @@ -361,6 +400,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getInt() { int i = BufferByteArray.this.getInt(index); index += 4; @@ -370,6 +410,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putInt(int i) { BufferByteArray.this.putInt(index, i); index += 4; @@ -378,6 +419,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public long getLong() { long l = BufferByteArray.this.getLong(index); index += 8; @@ -387,6 +429,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putLong(long l) { BufferByteArray.this.putLong(index, l); index += 8; @@ -395,6 +438,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public float getFloat() { float f = BufferByteArray.this.getFloat(index); index += 4; @@ -404,6 +448,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putFloat(float f) { BufferByteArray.this.putFloat(index, f); index += 4; @@ -412,6 +457,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public double getDouble() { double d = BufferByteArray.this.getDouble(index); index += 8; @@ -421,6 +467,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putDouble(double d) { BufferByteArray.this.putDouble(index, d); index += 8; @@ -429,6 +476,7 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public char getChar() { char c = BufferByteArray.this.getChar(index); index += 2; @@ -438,9 +486,25 @@ public abstract class BufferByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putChar(char c) { BufferByteArray.this.putChar(index, c); index += 2; } } + + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + int h = 17; + + if (bb != null) { + h = h * 37 + bb.hashCode(); + } + + return h; + } } http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java b/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java index 78a0559..f813577 100644 --- a/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java +++ b/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArray.java @@ -34,16 +34,19 @@ public interface ByteArray extends IoAbsoluteReader, IoAbsoluteWriter { /** * {@inheritDoc} */ + @Override int first(); /** * {@inheritDoc} */ + @Override int last(); /** * {@inheritDoc} */ + @Override ByteOrder order(); /** @@ -82,21 +85,25 @@ public interface ByteArray extends IoAbsoluteReader, IoAbsoluteWriter { * @param other The ByteArray we want to compare with * @return <tt>true</tt> if both ByteArray are equals */ + @Override boolean equals(Object other); /** * {@inheritDoc} */ + @Override byte get(int index); /** * {@inheritDoc} */ + @Override void get(int index, IoBuffer bb); /** * {@inheritDoc} */ + @Override int getInt(int index); /** @@ -136,26 +143,31 @@ public interface ByteArray extends IoAbsoluteReader, IoAbsoluteWriter { /** * {@inheritDoc} */ + @Override int getRemaining(); /** * {@inheritDoc} */ + @Override boolean hasRemaining(); /** * {@inheritDoc} */ + @Override byte get(); /** * {@inheritDoc} */ + @Override void get(IoBuffer bb); /** * {@inheritDoc} */ + @Override int getInt(); } } http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java b/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java index 6d5e312..a897f88 100644 --- a/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java +++ b/mina-core/src/main/java/org/apache/mina/util/byteaccess/ByteArrayList.java @@ -23,6 +23,8 @@ import java.util.NoSuchElementException; /** * A linked list that stores <code>ByteArray</code>s and maintains several useful invariants. + * + * Note : this class is *not* thread safe. * * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ @@ -194,7 +196,6 @@ class ByteArrayList { * Constructs a new header node. */ private Node() { - super(); previous = this; next = this; } @@ -203,8 +204,6 @@ class ByteArrayList { * Constructs a new node with a value. */ private Node(ByteArray ba) { - super(); - if (ba == null) { throw new IllegalArgumentException("ByteArray must not be null."); } @@ -221,6 +220,7 @@ class ByteArrayList { if (!hasPreviousNode()) { throw new NoSuchElementException(); } + return previous; } @@ -233,6 +233,7 @@ class ByteArrayList { if (!hasNextNode()) { throw new NoSuchElementException(); } + return next; } @@ -252,5 +253,4 @@ class ByteArrayList { return removed; } } - } http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java b/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java index 4134e6e..baebd01 100644 --- a/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java +++ b/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArray.java @@ -28,7 +28,7 @@ import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.util.byteaccess.ByteArrayList.Node; /** - * A ByteArray composed of other ByteArrays. Optimised for fast relative access + * A ByteArray composed of other ByteArrays. Optimized for fast relative access * via cursors. Absolute access methods are provided, but may perform poorly. * * TODO: Write about laziness of cursor implementation - how movement doesn't @@ -125,8 +125,7 @@ public final class CompositeByteArray extends AbstractByteArray { * Adds the specified {@link ByteArray} to the first * position in the list * - * @param ba - * The ByteArray to add to the list + * @param ba The ByteArray to add to the list */ public void addFirst(ByteArray ba) { addHook(ba); @@ -136,8 +135,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * Remove the first {@link ByteArray} in the list * - * @return - * The first ByteArray in the list + * @return The first ByteArray in the list */ public ByteArray removeFirst() { Node node = bas.removeFirst(); @@ -152,7 +150,7 @@ public final class CompositeByteArray extends AbstractByteArray { * TODO: Document free behaviour more thoroughly. * * @param index The index from where we will remove bytes - * @return$ The resulting byte aaay + * @return The resulting byte aaay */ public ByteArray removeTo(int index) { if (index < first() || index > last()) { @@ -176,20 +174,28 @@ public final class CompositeByteArray extends AbstractByteArray { // TODO: Consider using getIoBuffers(), as would avoid // performance problems for nested ComponentByteArrays. IoBuffer bb = component.getSingleIoBuffer(); + // get the limit of the buffer int originalLimit = bb.limit(); + // set the position to the beginning of the buffer bb.position(0); + // set the limit of the buffer to what is remaining bb.limit(remaining); + // create a new IoBuffer, sharing the data with 'bb' IoBuffer bb1 = bb.slice(); + // set the position at the end of the buffer bb.position(remaining); + // gets the limit of the buffer bb.limit(originalLimit); + // create a new IoBuffer, sharing teh data with 'bb' IoBuffer bb2 = bb.slice(); + // create a new ByteArray with 'bb1' ByteArray ba1 = new BufferByteArray(bb1) { @Override @@ -210,6 +216,7 @@ public final class CompositeByteArray extends AbstractByteArray { componentFinal.free(); } }; + // add the new ByteArray to the CompositeByteArray addFirst(ba2); } @@ -222,8 +229,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * Adds the specified {@link ByteArray} to the end of the list * - * @param ba - * The ByteArray to add to the end of the list + * @param ba The ByteArray to add to the end of the list */ public void addLast(ByteArray ba) { addHook(ba); @@ -233,8 +239,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * Removes the last {@link ByteArray} in the list * - * @return - * The ByteArray that was removed + * @return The ByteArray that was removed */ public ByteArray removeLast() { Node node = bas.removeLast(); @@ -245,6 +250,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void free() { while (!bas.isEmpty()) { Node node = bas.getLast(); @@ -253,28 +259,16 @@ public final class CompositeByteArray extends AbstractByteArray { } } - 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} */ + @Override public Iterable<IoBuffer> getIoBuffers() { if (bas.isEmpty()) { return Collections.emptyList(); } - Collection<IoBuffer> result = new ArrayList<IoBuffer>(); + Collection<IoBuffer> result = new ArrayList<>(); Node node = bas.getFirst(); for (IoBuffer bb : node.getByteArray().getIoBuffers()) { @@ -295,6 +289,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public IoBuffer getSingleIoBuffer() { if (byteArrayFactory == null) { throw new IllegalStateException( @@ -308,13 +303,11 @@ public final class CompositeByteArray extends AbstractByteArray { int actualLength = last() - first(); - { - Node node = bas.getFirst(); - ByteArray ba = node.getByteArray(); + Node firstNode = bas.getFirst(); + ByteArray ba = firstNode.getByteArray(); - if (ba.last() == actualLength) { - return ba.getSingleIoBuffer(); - } + if (ba.last() == actualLength) { + return ba.getSingleIoBuffer(); } // Replace all nodes with a single node. @@ -331,12 +324,14 @@ public final class CompositeByteArray extends AbstractByteArray { } bas.addLast(target); + return bb; } /** * {@inheritDoc} */ + @Override public Cursor cursor() { return new CursorImpl(); } @@ -344,6 +339,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public Cursor cursor(int index) { return new CursorImpl(index); } @@ -373,6 +369,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public ByteArray slice(int index, int length) { return cursor(index).slice(length); } @@ -380,6 +377,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public byte get(int index) { return cursor(index).get(); } @@ -387,6 +385,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(int index, byte b) { cursor(index).put(b); } @@ -394,6 +393,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void get(int index, IoBuffer bb) { cursor(index).get(bb); } @@ -401,6 +401,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(int index, IoBuffer bb) { cursor(index).put(bb); } @@ -408,6 +409,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int first() { return bas.firstByte(); } @@ -415,6 +417,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int last() { return bas.lastByte(); } @@ -423,8 +426,7 @@ public final class CompositeByteArray extends AbstractByteArray { * This method should be called prior to adding any component * <code>ByteArray</code> to a composite. * - * @param ba - * The component to add. + * @param ba The component to add. */ private void addHook(ByteArray ba) { // Check first() is zero, otherwise cursor might not work. @@ -432,6 +434,7 @@ public final class CompositeByteArray extends AbstractByteArray { 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(); @@ -443,6 +446,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public ByteOrder order() { if (order == null) { throw new IllegalStateException("Byte order not yet set."); @@ -453,6 +457,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void order(ByteOrder order) { if (order == null || !order.equals(this.order)) { this.order = order; @@ -468,6 +473,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public short getShort(int index) { return cursor(index).getShort(); } @@ -475,6 +481,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putShort(int index, short s) { cursor(index).putShort(s); } @@ -482,6 +489,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getInt(int index) { return cursor(index).getInt(); } @@ -489,6 +497,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putInt(int index, int i) { cursor(index).putInt(i); } @@ -496,6 +505,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public long getLong(int index) { return cursor(index).getLong(); } @@ -503,6 +513,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putLong(int index, long l) { cursor(index).putLong(l); } @@ -510,6 +521,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public float getFloat(int index) { return cursor(index).getFloat(); } @@ -517,6 +529,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putFloat(int index, float f) { cursor(index).putFloat(f); } @@ -524,6 +537,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public double getDouble(int index) { return cursor(index).getDouble(); } @@ -531,6 +545,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putDouble(int index, double d) { cursor(index).putDouble(d); } @@ -538,6 +553,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public char getChar(int index) { return cursor(index).getChar(); } @@ -545,6 +561,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putChar(int index, char c) { cursor(index).putChar(c); } @@ -583,6 +600,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getIndex() { return index; } @@ -590,6 +608,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void setIndex(int index) { checkBounds(index, 0); this.index = index; @@ -598,6 +617,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void skip(int length) { setIndex(index + length); } @@ -605,9 +625,11 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public ByteArray slice(int length) { CompositeByteArray slice = new CompositeByteArray(byteArrayFactory); int remaining = length; + while (remaining > 0) { prepareForAccess(remaining); int componentSliceSize = Math.min(remaining, componentCursor.getRemaining()); @@ -616,12 +638,14 @@ public final class CompositeByteArray extends AbstractByteArray { index += componentSliceSize; remaining -= componentSliceSize; } + return slice; } /** * {@inheritDoc} */ + @Override public ByteOrder order() { return CompositeByteArray.this.order(); } @@ -644,10 +668,12 @@ public final class CompositeByteArray extends AbstractByteArray { // 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()); } @@ -655,6 +681,7 @@ public final class CompositeByteArray extends AbstractByteArray { // Search from the end. componentNode = bas.getLast(); componentIndex = last() - componentNode.getByteArray().last(); + if (listener != null) { listener.enteredLastComponent(componentIndex, componentNode.getByteArray()); } @@ -665,6 +692,7 @@ public final class CompositeByteArray extends AbstractByteArray { while (index < componentIndex) { componentNode = componentNode.getPreviousNode(); componentIndex -= componentNode.getByteArray().last(); + if (listener != null) { listener.enteredPreviousComponent(componentIndex, componentNode.getByteArray()); } @@ -674,6 +702,7 @@ public final class CompositeByteArray extends AbstractByteArray { while (index >= componentIndex + componentNode.getByteArray().length()) { componentIndex += componentNode.getByteArray().last(); componentNode = componentNode.getNextNode(); + if (listener != null) { listener.enteredNextComponent(componentIndex, componentNode.getByteArray()); } @@ -681,6 +710,7 @@ public final class CompositeByteArray extends AbstractByteArray { // Update the cursor. int internalComponentIndex = index - componentIndex; + if (componentNode == oldComponentNode) { // Move existing cursor. componentCursor.setIndex(internalComponentIndex); @@ -693,6 +723,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public int getRemaining() { return last() - index + 1; } @@ -700,6 +731,7 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public boolean hasRemaining() { return getRemaining() > 0; } @@ -707,16 +739,19 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public byte get() { prepareForAccess(1); byte b = componentCursor.get(); index += 1; + return b; } /** * {@inheritDoc} */ + @Override public void put(byte b) { prepareForAccess(1); componentCursor.put(b); @@ -726,12 +761,14 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void get(IoBuffer bb) { while (bb.hasRemaining()) { int remainingBefore = bb.remaining(); prepareForAccess(remainingBefore); componentCursor.get(bb); int remainingAfter = bb.remaining(); + // Advance index by actual amount got. int chunkSize = remainingBefore - remainingAfter; index += chunkSize; @@ -741,12 +778,14 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void put(IoBuffer bb) { while (bb.hasRemaining()) { int remainingBefore = bb.remaining(); prepareForAccess(remainingBefore); componentCursor.put(bb); int remainingAfter = bb.remaining(); + // Advance index by actual amount put. int chunkSize = remainingBefore - remainingAfter; index += chunkSize; @@ -756,15 +795,19 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public short getShort() { prepareForAccess(2); + if (componentCursor.getRemaining() >= 4) { short s = componentCursor.getShort(); index += 2; + return s; } else { byte b0 = get(); byte b1 = get(); + if (order.equals(ByteOrder.BIG_ENDIAN)) { return (short) ((b0 << 8) | (b1 & 0xFF)); } else { @@ -776,40 +819,42 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putShort(short s) { prepareForAccess(2); + if (componentCursor.getRemaining() >= 4) { componentCursor.putShort(s); index += 2; } else { - byte b0; - byte b1; if (order.equals(ByteOrder.BIG_ENDIAN)) { - b0 = (byte) ((s >> 8) & 0xff); - b1 = (byte) ((s >> 0) & 0xff); + put((byte) ((s >> 8) & 0xff)); + put((byte) (s & 0xff)); } else { - b0 = (byte) ((s >> 0) & 0xff); - b1 = (byte) ((s >> 8) & 0xff); + put((byte) (s & 0xff)); + put((byte) ((s >> 8) & 0xff)); } - put(b0); - put(b1); } } /** * {@inheritDoc} */ + @Override public int getInt() { prepareForAccess(4); + if (componentCursor.getRemaining() >= 4) { int i = componentCursor.getInt(); index += 4; + return i; } else { byte b0 = get(); byte b1 = get(); byte b2 = get(); byte b3 = get(); + if (order.equals(ByteOrder.BIG_ENDIAN)) { return (b0 << 24) | ((b1 & 0xFF) << 16) | ((b2 & 0xFF) << 8) | (b3 & 0xFF); } else { @@ -821,42 +866,39 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putInt(int i) { prepareForAccess(4); + if (componentCursor.getRemaining() >= 4) { componentCursor.putInt(i); index += 4; } else { - byte b0; - byte b1; - byte b2; - byte b3; if (order.equals(ByteOrder.BIG_ENDIAN)) { - b0 = (byte) ((i >> 24) & 0xff); - b1 = (byte) ((i >> 16) & 0xff); - b2 = (byte) ((i >> 8) & 0xff); - b3 = (byte) ((i >> 0) & 0xff); + put((byte) ((i >> 24) & 0xff)); + put((byte) ((i >> 16) & 0xff)); + put((byte) ((i >> 8) & 0xff)); + put((byte) (i & 0xff)); } else { - b0 = (byte) ((i >> 0) & 0xff); - b1 = (byte) ((i >> 8) & 0xff); - b2 = (byte) ((i >> 16) & 0xff); - b3 = (byte) ((i >> 24) & 0xff); + put((byte) (i & 0xff)); + put((byte) ((i >> 8) & 0xff)); + put((byte) ((i >> 16) & 0xff)); + put((byte) ((i >> 24) & 0xff)); } - put(b0); - put(b1); - put(b2); - put(b3); } } /** * {@inheritDoc} */ + @Override public long getLong() { prepareForAccess(8); + if (componentCursor.getRemaining() >= 4) { long l = componentCursor.getLong(); index += 8; + return l; } else { byte b0 = get(); @@ -867,6 +909,7 @@ public final class CompositeByteArray extends AbstractByteArray { byte b5 = get(); byte b6 = get(); byte b7 = get(); + if (order.equals(ByteOrder.BIG_ENDIAN)) { return ((b0 & 0xFFL) << 56) | ((b1 & 0xFFL) << 48) | ((b2 & 0xFFL) << 40) | ((b3 & 0xFFL) << 32) | ((b4 & 0xFFL) << 24) | ((b5 & 0xFFL) << 16) | ((b6 & 0xFFL) << 8) | (b7 & 0xFFL); @@ -880,62 +923,50 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putLong(long l) { - //TODO: see if there is some optimizing that can be done here prepareForAccess(8); + if (componentCursor.getRemaining() >= 4) { componentCursor.putLong(l); index += 8; } else { - byte b0; - byte b1; - byte b2; - byte b3; - byte b4; - byte b5; - byte b6; - byte b7; if (order.equals(ByteOrder.BIG_ENDIAN)) { - b0 = (byte) ((l >> 56) & 0xff); - b1 = (byte) ((l >> 48) & 0xff); - b2 = (byte) ((l >> 40) & 0xff); - b3 = (byte) ((l >> 32) & 0xff); - b4 = (byte) ((l >> 24) & 0xff); - b5 = (byte) ((l >> 16) & 0xff); - b6 = (byte) ((l >> 8) & 0xff); - b7 = (byte) ((l >> 0) & 0xff); + put((byte) ((l >> 56) & 0xff)); + put((byte) ((l >> 48) & 0xff)); + put((byte) ((l >> 40) & 0xff)); + put((byte) ((l >> 32) & 0xff)); + put((byte) ((l >> 24) & 0xff)); + put((byte) ((l >> 16) & 0xff)); + put((byte) ((l >> 8) & 0xff)); + put((byte) (l & 0xff)); } else { - b0 = (byte) ((l >> 0) & 0xff); - b1 = (byte) ((l >> 8) & 0xff); - b2 = (byte) ((l >> 16) & 0xff); - b3 = (byte) ((l >> 24) & 0xff); - b4 = (byte) ((l >> 32) & 0xff); - b5 = (byte) ((l >> 40) & 0xff); - b6 = (byte) ((l >> 48) & 0xff); - b7 = (byte) ((l >> 56) & 0xff); + put((byte) (l & 0xff)); + put((byte) ((l >> 8) & 0xff)); + put((byte) ((l >> 16) & 0xff)); + put((byte) ((l >> 24) & 0xff)); + put((byte) ((l >> 32) & 0xff)); + put((byte) ((l >> 40) & 0xff)); + put((byte) ((l >> 48) & 0xff)); + put((byte) ((l >> 56) & 0xff)); } - put(b0); - put(b1); - put(b2); - put(b3); - put(b4); - put(b5); - put(b6); - put(b7); } } /** * {@inheritDoc} */ + @Override public float getFloat() { prepareForAccess(4); + if (componentCursor.getRemaining() >= 4) { float f = componentCursor.getFloat(); index += 4; return f; } else { int i = getInt(); + return Float.intBitsToFloat(i); } } @@ -943,8 +974,10 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putFloat(float f) { prepareForAccess(4); + if (componentCursor.getRemaining() >= 4) { componentCursor.putFloat(f); index += 4; @@ -957,14 +990,18 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public double getDouble() { prepareForAccess(8); + if (componentCursor.getRemaining() >= 4) { double d = componentCursor.getDouble(); index += 8; + return d; } else { long l = getLong(); + return Double.longBitsToDouble(l); } } @@ -972,8 +1009,10 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putDouble(double d) { prepareForAccess(8); + if (componentCursor.getRemaining() >= 4) { componentCursor.putDouble(d); index += 8; @@ -986,15 +1025,19 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public char getChar() { prepareForAccess(2); + if (componentCursor.getRemaining() >= 4) { char c = componentCursor.getChar(); index += 2; + return c; } else { byte b0 = get(); byte b1 = get(); + if (order.equals(ByteOrder.BIG_ENDIAN)) { return (char)((b0 << 8) | (b1 & 0xFF)); } else { @@ -1006,25 +1049,55 @@ public final class CompositeByteArray extends AbstractByteArray { /** * {@inheritDoc} */ + @Override public void putChar(char c) { prepareForAccess(2); + + if (componentCursor.getRemaining() >= 4) { componentCursor.putChar(c); index += 2; } else { byte b0; byte b1; + if (order.equals(ByteOrder.BIG_ENDIAN)) { b0 = (byte) ((c >> 8) & 0xff); - b1 = (byte) ((c >> 0) & 0xff); + b1 = (byte) (c & 0xff); } else { - b0 = (byte) ((c >> 0) & 0xff); + b0 = (byte) (c & 0xff); b1 = (byte) ((c >> 8) & 0xff); } + put(b0); put(b1); } } + + 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} + */ + @Override + public int hashCode() { + int h = 17; + + h = h*37 + bas.hashCode(); + + return h; } } http://git-wip-us.apache.org/repos/asf/mina/blob/9c95cd14/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java b/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java index 2e1be05..370ab48 100644 --- a/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java +++ b/mina-core/src/main/java/org/apache/mina/util/byteaccess/CompositeByteArrayRelativeBase.java @@ -55,19 +55,35 @@ abstract class CompositeByteArrayRelativeBase { public CompositeByteArrayRelativeBase(CompositeByteArray cba) { this.cba = cba; cursor = cba.cursor(cba.first(), new CursorListener() { - + + /** + * {@inheritDoc} + */ + @Override public void enteredFirstComponent(int componentIndex, ByteArray component) { // Do nothing. } + /** + * {@inheritDoc} + */ + @Override public void enteredLastComponent(int componentIndex, ByteArray component) { assert false; } + /** + * {@inheritDoc} + */ + @Override public void enteredNextComponent(int componentIndex, ByteArray component) { cursorPassedFirstComponent(); } + /** + * {@inheritDoc} + */ + @Override public void enteredPreviousComponent(int componentIndex, ByteArray component) { assert false; } @@ -76,21 +92,21 @@ abstract class CompositeByteArrayRelativeBase { } /** - * {@inheritDoc} + * @return The number of remaining bytes */ public final int getRemaining() { return cursor.getRemaining(); } /** - * {@inheritDoc} + * @return <T>TRUE</T> if there are some more bytes */ public final boolean hasRemaining() { return cursor.hasRemaining(); } /** - * {@inheritDoc} + * @return The used byte order (little of big indian) */ public ByteOrder order() { return cba.order(); @@ -133,5 +149,4 @@ abstract class CompositeByteArrayRelativeBase { * freeing it). */ protected abstract void cursorPassedFirstComponent(); - }