本文整理了Java中java.nio.ByteBuffer.duplicate()
方法的一些代码示例,展示了ByteBuffer.duplicate()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.duplicate()
方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:duplicate
[英]Returns a duplicated buffer that shares its content with this buffer.
The duplicated buffer's position, limit, capacity and mark are the same as this buffer's. The duplicated buffer's read-only property and byte order are the same as this buffer's too.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
[中]返回与此缓冲区共享其内容的重复缓冲区。
重复缓冲区的位置、限制、容量和标记与此缓冲区相同。重复缓冲区的只读属性和字节顺序也与此缓冲区相同。
新缓冲区与此缓冲区共享其内容,这意味着任何一个缓冲区的内容更改都将对另一个缓冲区可见。两个缓冲器的位置、极限和标记是独立的。
代码示例来源:origin: apache/incubator-dubbo
@Override
public void setBytes(int index, ByteBuffer src) {
ByteBuffer data = buffer.duplicate();
data.limit(index + src.remaining()).position(index);
data.put(src);
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public void setBytes(int index, byte[] src, int srcIndex, int length) {
ByteBuffer data = buffer.duplicate();
data.limit(index + length).position(index);
data.put(src, srcIndex, length);
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
ByteBuffer data = buffer.duplicate();
try {
data.limit(index + length).position(index);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
}
data.get(dst, dstIndex, length);
}
代码示例来源:origin: apache/incubator-druid
@Override
public void init(ByteBuffer buf, int position)
{
final ByteBuffer mutationBuffer = buf.duplicate();
mutationBuffer.position(position);
mutationBuffer.put(EMPTY_BYTES);
}
代码示例来源:origin: spring-projects/spring-framework
private void write(ByteBuffer source) {
int length = source.remaining();
ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.writePosition + source.remaining();
((Buffer) tmp).clear().position(this.writePosition).limit(limit);
tmp.put(source);
this.writePosition += length;
}
代码示例来源:origin: Alluxio/alluxio
/**
* Creates a byte array from the given ByteBuffer, the position property of input
* {@link ByteBuffer} remains unchanged.
*
* @param buf source ByteBuffer
* @return a newly created byte array
*/
public static byte[] newByteArrayFromByteBuffer(ByteBuffer buf) {
final int length = buf.remaining();
byte[] bytes = new byte[length];
// transfer bytes from this buffer into the given destination array
buf.duplicate().get(bytes, 0, length);
return bytes;
}
代码示例来源:origin: Tencent/tinker
/**
* Returns a copy of the the bytes of this dex.
*/
public byte[] getBytes() {
ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe
byte[] result = new byte[data.capacity()];
data.position(0);
data.get(result);
return result;
}
代码示例来源:origin: apache/incubator-druid
public static ByteBuffer getSlice(ByteBuffer buffer, int sliceSize, int i)
{
final ByteBuffer slice = buffer.duplicate();
slice.position(sliceSize * i);
slice.limit(slice.position() + sliceSize);
return slice.slice();
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public void aggregate(ByteBuffer buf, int position)
{
SerializablePairLongString pair = (SerializablePairLongString) selector.getObject();
if (pair != null && pair.lhs != null) {
ByteBuffer mutationBuffer = buf.duplicate();
mutationBuffer.position(position);
long lastTime = mutationBuffer.getLong(position);
if (pair.lhs < lastTime) {
mutationBuffer.putLong(position, pair.lhs);
if (pair.rhs != null) {
byte[] valueBytes = StringUtils.toUtf8(pair.rhs);
mutationBuffer.putInt(position + Long.BYTES, valueBytes.length);
mutationBuffer.position(position + Long.BYTES + Integer.BYTES);
mutationBuffer.put(valueBytes);
} else {
mutationBuffer.putInt(position + Long.BYTES, 0);
}
}
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public Object get(ByteBuffer buf, int position)
{
ByteBuffer mutationBuffer = buf.duplicate();
mutationBuffer.position(position);
Long timeValue = mutationBuffer.getLong(position);
Integer stringSizeBytes = mutationBuffer.getInt(position + Long.BYTES);
SerializablePairLongString serializablePair;
if (stringSizeBytes > 0) {
byte[] valueBytes = new byte[stringSizeBytes];
mutationBuffer.position(position + Long.BYTES + Integer.BYTES);
mutationBuffer.get(valueBytes, 0, stringSizeBytes);
serializablePair = new SerializablePairLongString(timeValue, StringUtils.fromUtf8(valueBytes));
} else {
serializablePair = new SerializablePairLongString(timeValue, null);
}
return serializablePair;
}
代码示例来源:origin: thinkaurelius/titan
@Override
public StaticBuffer getStaticBufferFlipBytes(int from, int to) {
ByteBuffer b = buffer.duplicate();
b.flip();
Preconditions.checkArgument(from>=0 && from<=to);
Preconditions.checkArgument(to<=b.limit());
for (int i=from;i<to;i++) b.put(i,(byte)~b.get(i));
return StaticArrayBuffer.of(b);
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public Object get(ByteBuffer buf, int position)
{
ByteBuffer mutationBuffer = buf.duplicate();
mutationBuffer.position(position);
// | k (byte) | numLongs (int) | bitset (long[numLongs]) |
int sizeBytes = 1 + Integer.BYTES + (buf.getInt(position + 1) * Long.BYTES);
mutationBuffer.limit(position + sizeBytes);
return mutationBuffer.slice();
}
代码示例来源:origin: netty/netty
@Override
public ByteBuffer nioBuffer(int index, int length) {
return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public DefaultDataBuffer read(byte[] destination, int offset, int length) {
Assert.notNull(destination, "Byte array must not be null");
assertIndex(this.readPosition <= this.writePosition - length,
"readPosition %d and length %d should be smaller than writePosition %d",
this.readPosition, length, this.writePosition);
ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.readPosition + length;
((Buffer) tmp).clear().position(this.readPosition).limit(limit);
tmp.get(destination, offset, length);
this.readPosition += length;
return this;
}
代码示例来源:origin: sannies/mp4parser
@Override
public void _parseDetails(ByteBuffer content) {
parseVersionAndFlags(content);
data = content.slice();
content.position(content.position() + content.remaining());
try {
data.rewind();
descriptor = ObjectDescriptorFactory.createFrom(-1, data.duplicate());
} catch (IOException e) {
LOG.warn("Error parsing ObjectDescriptor", e);
//that's why we copied it ;)
} catch (IndexOutOfBoundsException e) {
LOG.warn("Error parsing ObjectDescriptor", e);
//that's why we copied it ;)
}
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
if (dst instanceof ByteBufferBackedChannelBuffer) {
ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst;
ByteBuffer data = bbdst.buffer.duplicate();
data.limit(dstIndex + length).position(dstIndex);
getBytes(index, data);
} else if (buffer.hasArray()) {
dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length);
} else {
dst.setBytes(dstIndex, this, index, length);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public DefaultDataBuffer write(byte[] source, int offset, int length) {
Assert.notNull(source, "Byte array must not be null");
ensureCapacity(length);
ByteBuffer tmp = this.byteBuffer.duplicate();
int limit = this.writePosition + length;
((Buffer) tmp).clear().position(this.writePosition).limit(limit);
tmp.put(source, offset, length);
this.writePosition += length;
return this;
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public void setBytes(int index, ByteBuffer src) {
ByteBuffer data = buffer.duplicate();
data.limit(index + src.remaining()).position(index);
data.put(src);
}
代码示例来源:origin: apache/incubator-druid
@Override
public void init(ByteBuffer buf, int position)
{
final ByteBuffer mutationBuffer = buf.duplicate();
mutationBuffer.position(position);
mutationBuffer.put(EMPTY_BYTES);
}
代码示例来源:origin: netty/netty
private void getBytes(int index, ByteBuffer dst, boolean internal) {
checkIndex(index, dst.remaining());
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = buffer.duplicate();
}
tmpBuf.clear().position(index).limit(index + dst.remaining());
dst.put(tmpBuf);
}
内容来源于网络,如有侵权,请联系作者删除!