Skip to content

Commit

Permalink
Add UnpooledUnsafeDirectByteBuf and use it when low-level access is a…
Browse files Browse the repository at this point in the history
…vailable

- Remove PooledUnsafeDirectByteBuf.setMedium() which is redundant
- Fix constructor visibility
  • Loading branch information
trustin committed Mar 6, 2013
1 parent 6c3d5ed commit 81ce055
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 14 deletions.
8 changes: 4 additions & 4 deletions buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ public ByteBuf setZero(int index, int length) {

@Override
public byte readByte() {
if (readerIndex == writerIndex) {
throw new IndexOutOfBoundsException("readerIndex(" + readerIndex + ") == writerIndex(" + writerIndex + ')');
}
return _getByte(readerIndex++);
int i = readerIndex;
byte b = getByte(i);
readerIndex = i + 1;
return b;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,6 @@ protected void _setShort(int index, int value) {
PlatformDependent.putShort(addr(index), NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
}

@Override
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
_setMedium(index, value);
return this;
}

@Override
protected void _setMedium(int index, int value) {
long addr = addr(index);
Expand Down
4 changes: 4 additions & 0 deletions buffer/src/main/java/io/netty/buffer/Unpooled.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.buffer;

import io.netty.util.internal.PlatformDependent;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
Expand Down Expand Up @@ -206,6 +208,8 @@ public static ByteBuf wrappedBuffer(ByteBuffer buffer) {
buffer.array(),
buffer.arrayOffset() + buffer.position(),
buffer.remaining()).order(buffer.order());
} else if (PlatformDependent.hasUnsafe()) {
return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining());
} else {
return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {

@Override
protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
return new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
if (PlatformDependent.hasUnsafe()) {
return new UnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity);
} else {
return new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
* @param initialCapacity the initial capacity of the underlying direct buffer
* @param maxCapacity the maximum capacity of the underlying direct buffer
*/
public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
super(maxCapacity);
if (alloc == null) {
throw new NullPointerException("alloc");
Expand All @@ -76,7 +76,7 @@ public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int ma
*
* @param maxCapacity the maximum capacity of the underlying direct buffer
*/
public UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
super(maxCapacity);
if (alloc == null) {
throw new NullPointerException("alloc");
Expand Down
Loading

0 comments on commit 81ce055

Please sign in to comment.