Related issue: NETTY-253 (Add several useful getters and setters to ChannelBuffer class)

* Added ChannelBuffer.hasArray/array/arrayOffset()
This commit is contained in:
Trustin Lee 2009-11-25 07:08:52 +00:00
parent 2c3ab480a2
commit 1688569758
10 changed files with 132 additions and 0 deletions

View File

@ -80,6 +80,18 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
return capacity; return capacity;
} }
public boolean hasArray() {
return buffer.hasArray();
}
public byte[] array() {
return buffer.array();
}
public int arrayOffset() {
return buffer.arrayOffset();
}
public byte getByte(int index) { public byte getByte(int index) {
return buffer.get(index); return buffer.get(index);
} }

View File

@ -1562,6 +1562,30 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/ */
ByteBuffer[] toByteBuffers(int index, int length); ByteBuffer[] toByteBuffers(int index, int length);
/**
* Returns {@code true} if and only if this buffer has a backing byte array.
* If this method returns true, you can safely call {@link #array()} and
* {@link #arrayOffset()}.
*/
boolean hasArray();
/**
* Returns the backing byte array of this buffer.
*
* @throws UnsupportedOperationException
* if there no accessible backing byte array
*/
byte[] array();
/**
* Returns the offset of the first byte within the backing byte array of
* this buffer.
*
* @throws UnsupportedOperationException
* if there no accessible backing byte array
*/
int arrayOffset();
/** /**
* Decodes this buffer's readable bytes into a string with the specified * Decodes this buffer's readable bytes into a string with the specified
* character set name. This method is identical to * character set name. This method is identical to

View File

@ -150,6 +150,18 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
return order; return order;
} }
public boolean hasArray() {
return false;
}
public byte[] array() {
throw new UnsupportedOperationException();
}
public int arrayOffset() {
throw new UnsupportedOperationException();
}
public int capacity() { public int capacity() {
return indices[components.length]; return indices[components.length];
} }

View File

@ -68,6 +68,18 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr
return buffer.capacity(); return buffer.capacity();
} }
public boolean hasArray() {
return buffer.hasArray();
}
public byte[] array() {
return buffer.array();
}
public int arrayOffset() {
return buffer.arrayOffset();
}
public byte getByte(int index) { public byte getByte(int index) {
return buffer.getByte(index); return buffer.getByte(index);
} }

View File

@ -76,6 +76,18 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
return buffer.capacity(); return buffer.capacity();
} }
public boolean hasArray() {
return buffer.hasArray();
}
public byte[] array() {
return buffer.array();
}
public int arrayOffset() {
return buffer.arrayOffset();
}
public byte getByte(int index) { public byte getByte(int index) {
return buffer.getByte(index); return buffer.getByte(index);
} }

View File

@ -77,6 +77,18 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer {
return array.length; return array.length;
} }
public boolean hasArray() {
return true;
}
public byte[] array() {
return array;
}
public int arrayOffset() {
return 0;
}
public byte getByte(int index) { public byte getByte(int index) {
return array[index]; return array[index];
} }

View File

@ -64,6 +64,18 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap
return buffer.order(); return buffer.order();
} }
public boolean hasArray() {
return false;
}
public byte[] array() {
throw new ReadOnlyBufferException();
}
public int arrayOffset() {
throw new ReadOnlyBufferException();
}
@Override @Override
public void discardReadBytes() { public void discardReadBytes() {
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();

View File

@ -73,6 +73,18 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
return length; return length;
} }
public boolean hasArray() {
return buffer.hasArray();
}
public byte[] array() {
return buffer.array();
}
public int arrayOffset() {
return buffer.arrayOffset() + adjustment;
}
public byte getByte(int index) { public byte getByte(int index) {
checkIndex(index); checkIndex(index);
return buffer.getByte(index + adjustment); return buffer.getByte(index + adjustment);

View File

@ -67,6 +67,18 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
return length; return length;
} }
public boolean hasArray() {
return buffer.hasArray();
}
public byte[] array() {
return buffer.array();
}
public int arrayOffset() {
return buffer.arrayOffset();
}
public byte getByte(int index) { public byte getByte(int index) {
checkIndex(index); checkIndex(index);
return buffer.getByte(index); return buffer.getByte(index);

View File

@ -57,6 +57,18 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
} }
} }
public boolean hasArray() {
return false;
}
public byte[] array() {
throw new UnsupportedOperationException();
}
public int arrayOffset() {
throw new UnsupportedOperationException();
}
public void clear() { public void clear() {
throw new UnreplayableOperationException(); throw new UnreplayableOperationException();
} }