Faster memory copy between direct buffer and byte array (part 2)
This commit is contained in:
parent
9a676bc7d5
commit
43af599d52
@ -102,13 +102,15 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (dst instanceof PooledUnsafeDirectByteBuf) {
|
if (length != 0) {
|
||||||
PooledUnsafeDirectByteBuf bbdst = (PooledUnsafeDirectByteBuf) dst;
|
if (dst instanceof PooledUnsafeDirectByteBuf) {
|
||||||
PlatformDependent.copyMemory(addr(index), bbdst.addr(dstIndex), length);
|
PooledUnsafeDirectByteBuf bbdst = (PooledUnsafeDirectByteBuf) dst;
|
||||||
} else if (dst.hasArray()) {
|
PlatformDependent.copyMemory(addr(index), bbdst.addr(dstIndex), length);
|
||||||
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
} else if (dst.hasArray()) {
|
||||||
} else {
|
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||||
dst.setBytes(dstIndex, this, index, length);
|
} else {
|
||||||
|
dst.setBytes(dstIndex, this, index, length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -116,10 +118,9 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
ByteBuffer tmpBuf = internalNioBuffer();
|
if (length != 0) {
|
||||||
index = idx(index);
|
PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
|
||||||
tmpBuf.clear().position(index).limit(index + length);
|
}
|
||||||
tmpBuf.get(dst, dstIndex, length);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,15 +138,11 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (length == 0) {
|
if (length != 0) {
|
||||||
return this;
|
byte[] tmp = new byte[length];
|
||||||
|
PlatformDependent.copyMemory(addr(index), tmp, 0, length);
|
||||||
|
out.write(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] tmp = new byte[length];
|
|
||||||
ByteBuffer tmpBuf = internalNioBuffer();
|
|
||||||
tmpBuf.clear().position(idx(index));
|
|
||||||
tmpBuf.get(tmp);
|
|
||||||
out.write(tmp);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,13 +200,15 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (src instanceof PooledUnsafeDirectByteBuf) {
|
if (length != 0) {
|
||||||
PooledUnsafeDirectByteBuf bbsrc = (PooledUnsafeDirectByteBuf) src;
|
if (src instanceof PooledUnsafeDirectByteBuf) {
|
||||||
PlatformDependent.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
|
PooledUnsafeDirectByteBuf bbsrc = (PooledUnsafeDirectByteBuf) src;
|
||||||
} else if (src.hasArray()) {
|
PlatformDependent.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
|
||||||
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
|
} else if (src.hasArray()) {
|
||||||
} else {
|
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
|
||||||
src.getBytes(srcIndex, this, index, length);
|
} else {
|
||||||
|
src.getBytes(srcIndex, this, index, length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -217,10 +216,9 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
ByteBuffer tmpBuf = internalNioBuffer();
|
if (length != 0) {
|
||||||
index = idx(index);
|
PlatformDependent.copyMemory(src, srcIndex, addr(index), length);
|
||||||
tmpBuf.clear().position(index).limit(index + length);
|
}
|
||||||
tmpBuf.put(src, srcIndex, length);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,12 +241,9 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
byte[] tmp = new byte[length];
|
byte[] tmp = new byte[length];
|
||||||
int readBytes = in.read(tmp);
|
int readBytes = in.read(tmp);
|
||||||
if (readBytes <= 0) {
|
if (readBytes > 0) {
|
||||||
return readBytes;
|
PlatformDependent.copyMemory(tmp, 0, addr(index), readBytes);
|
||||||
}
|
}
|
||||||
ByteBuffer tmpNioBuf = internalNioBuffer();
|
|
||||||
tmpNioBuf.clear().position(idx(index));
|
|
||||||
tmpNioBuf.put(tmp, 0, readBytes);
|
|
||||||
return readBytes;
|
return readBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,8 +264,10 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||||||
public ByteBuf copy(int index, int length) {
|
public ByteBuf copy(int index, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
PooledUnsafeDirectByteBuf copy = (PooledUnsafeDirectByteBuf) alloc().directBuffer(capacity(), maxCapacity());
|
PooledUnsafeDirectByteBuf copy = (PooledUnsafeDirectByteBuf) alloc().directBuffer(capacity(), maxCapacity());
|
||||||
PlatformDependent.copyMemory(addr(index), copy.addr(index), length);
|
if (length != 0) {
|
||||||
copy.setIndex(index, index + length);
|
PlatformDependent.copyMemory(addr(index), copy.addr(index), length);
|
||||||
|
copy.setIndex(index, index + length);
|
||||||
|
}
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user