本文整理了Java中java.nio.ByteBuffer.mark()
方法的一些代码示例,展示了ByteBuffer.mark()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.mark()
方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:mark
暂无
代码示例来源:origin: AsyncHttpClient/async-http-client
public NettyByteBufferBody(ByteBuffer bb, CharSequence contentTypeOverride) {
this.bb = bb;
length = bb.remaining();
bb.mark();
this.contentTypeOverride = contentTypeOverride;
}
代码示例来源:origin: redisson/redisson
@Override
public synchronized void mark(int readlimit) {
buffer.mark();
int pos = buffer.position();
int max = buffer.capacity() - pos;
int newLimit = Math.min(max, pos + readlimit);
buffer.limit(newLimit);
}
代码示例来源:origin: apache/kafka
/**
* Read a buffer into a Byte array for the given offset and length
*/
public static byte[] readBytes(ByteBuffer buffer, int offset, int length) {
byte[] dest = new byte[length];
if (buffer.hasArray()) {
System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, length);
} else {
buffer.mark();
buffer.position(offset);
buffer.get(dest, 0, length);
buffer.reset();
}
return dest;
}
代码示例来源:origin: apache/nifi
final int total = socketBuffer.remaining();
final InetAddress sender = socketChannel.socket().getInetAddress();
byte currByte = socketBuffer.get();
socketBuffer.mark();
代码示例来源:origin: ehcache/ehcache3
@Override
public ByteBuffer readBinaryValue(long chain) {
// first get total element size and allocate buffer
long element = chain + this.totalChainHeaderSize;
int totalLength = DETACHED_CONTIGUOUS_CHAIN_HEADER_SIZE;
do {
totalLength += ELEMENT_HEADER_SIZE + readElementLength(element);
element = storage.readLong(element + ELEMENT_HEADER_NEXT_OFFSET);
} while (element != chain);
final ByteBuffer detachedContiguousBuffer = ByteBuffer.allocate(totalLength);
// one way for layers above to extract encoding is to put the encoding of the chain address in the value
detachedContiguousBuffer.putLong(chain);
// now add the elements to the buffer
element = chain + this.totalChainHeaderSize;
do {
final int startPosition = detachedContiguousBuffer.position();
detachedContiguousBuffer.put(storage.readBuffer(element, ELEMENT_HEADER_SIZE + readElementLength(element)));
detachedContiguousBuffer.mark();
detachedContiguousBuffer.putLong(startPosition + ELEMENT_HEADER_NEXT_OFFSET, -1L);
detachedContiguousBuffer.reset();
element = storage.readLong(element + ELEMENT_HEADER_NEXT_OFFSET);
} while (element != chain);
return (ByteBuffer)detachedContiguousBuffer.flip();
}
代码示例来源:origin: apache/nifi
internalFrameBuilder.version = currentData.get();
internalFrameBuilder.frameType = currentData.get();
switch (internalFrameBuilder.frameType) {
case FRAME_JSON:
currentData.mark();
currentData.mark();
byte[] jsonBytes = new byte[payloadLength];
currentData.get(jsonBytes, 0, payloadLength);
currentData.mark();
代码示例来源:origin: mpetazzoni/ttorrent
private static ByteBuffer prepareDataFromBuffer(ByteBuffer buffer) {
final ByteBuffer data = ByteBuffer.allocate(buffer.remaining());
buffer.mark();
data.put(buffer);
data.clear();
buffer.reset();
return data;
}
代码示例来源:origin: neo4j/neo4j
assertThat( a.capacity(), is( sizeInBytes ) );
assertThat( a.limit(), is( sizeInBytes ) );
assertThat( a.position(), is( 0 ) );
assertThat( a.remaining(), is( sizeInBytes ) );
assertThat( getByte( address ), is( (byte) 0 ) );
a.put( (byte) -1 );
assertThat( getByte( address ), is( (byte) -1 ) );
a.position( 101 );
a.mark();
a.limit( 202 );
assertThat( a.capacity(), is( sizeInBytes2 ) );
assertThat( a.limit(), is( sizeInBytes2 ) );
assertThat( a.position(), is( 0 ) );
assertThat( a.remaining(), is( sizeInBytes2 ) );
assertThat( getByte( address2 ), is( (byte) 0 ) );
a.put( (byte) -1 );
代码示例来源:origin: qunarcorp/qmq
static boolean match(SegmentBuffer result, List<byte[]> requestTags, int tagTypeCode) {
ByteBuffer message = result.getBuffer();
message.mark();
byte flag = message.get();
if (!Flags.hasTags(flag)) {
message.reset();
return false;
final byte tagsSize = message.get();
if (tagsSize == 1) {
final short len = message.getShort();
final byte[] tag = new byte[len];
message.get(tag);
message.reset();
return matchOneTag(tag, requestTags, tagTypeCode);
tags.add(bs);
message.reset();
return matchTags(tags, requestTags, tagTypeCode);
代码示例来源:origin: didi/DDMQ
private AppendMessageResult doAppendMultiTopic(long fileFromOffset, ByteBuffer byteBuffer, int maxBlank,
MessageExtBatch messageExtBatch) {
long wroteOffset = fileFromOffset + byteBuffer.position();
0L, CommitLog.this.defaultMessageStore.now() - beginTimeMills);
byteBuffer.mark();
messagesByteBuff.mark();
while (messagesByteBuff.hasRemaining()) {
final int msgPos = messagesByteBuff.position();
final int msgLen = messagesByteBuff.getInt();
totalMsgLen += msgLen;
messagesByteBuff.position(msgPos + 12);
final int queueId = messagesByteBuff.getInt();
messagesByteBuff.position(msgPos + 20);
代码示例来源:origin: org.apache.hadoop/hadoop-common
/**
* Returns the next code point at the current position in
* the buffer. The buffer's position will be incremented.
* Any mark set on this buffer will be changed by this method!
*/
public static int bytesToCodePoint(ByteBuffer bytes) {
bytes.mark();
byte b = bytes.get();
bytes.reset();
int extraBytesToRead = bytesFromUTF8[(b & 0xFF)];
if (extraBytesToRead < 0) return -1; // trailing byte!
int ch = 0;
switch (extraBytesToRead) {
case 5: ch += (bytes.get() & 0xFF); ch <<= 6; /* remember, illegal UTF-8 */
case 4: ch += (bytes.get() & 0xFF); ch <<= 6; /* remember, illegal UTF-8 */
case 3: ch += (bytes.get() & 0xFF); ch <<= 6;
case 2: ch += (bytes.get() & 0xFF); ch <<= 6;
case 1: ch += (bytes.get() & 0xFF); ch <<= 6;
case 0: ch += (bytes.get() & 0xFF);
}
ch -= offsetsFromUTF8[extraBytesToRead];
return ch;
}
代码示例来源:origin: openmessaging/openmessaging-storage-dledger
public static long getPos(ByteBuffer byteBuffer) {
long pos;
byteBuffer.mark();
byteBuffer.position(byteBuffer.position() + DLedgerEntry.POS_OFFSET);
pos = byteBuffer.getLong();
byteBuffer.reset();
return pos;
}
代码示例来源:origin: TooTallNate/Java-WebSocket
public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException {
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
decode.onMalformedInput( codingErrorAction );
decode.onUnmappableCharacter( codingErrorAction );
String s;
try {
bytes.mark();
s = decode.decode( bytes ).toString();
bytes.reset();
} catch ( CharacterCodingException e ) {
throw new InvalidDataException( CloseFrame.NO_UTF8, e );
}
return s;
}
代码示例来源:origin: thinkaurelius/titan
@Override
public void copy(ByteBuffer data, byte[] dest, int destOffset) {
if (data.hasArray()) {
System.arraycopy(data.array(),data.arrayOffset()+data.position(),dest,destOffset,data.remaining());
} else {
data.mark();
data.get(dest,destOffset,data.remaining());
data.reset();
}
}
}
代码示例来源:origin: apache/nifi
@Override
protected void processBuffer(final SocketChannel socketChannel, final ByteBuffer socketBuffer)
throws InterruptedException, IOException {
// get total bytes in buffer
final int total = socketBuffer.remaining();
final InetAddress sender = socketChannel.socket().getInetAddress();
try {
// go through the buffer parsing the RELP command
for (int i = 0; i < total; i++) {
byte currByte = socketBuffer.get();
// if we found the end of a frame, handle the frame and mark the buffer
if (decoder.process(currByte)) {
final RELPFrame frame = decoder.getFrame();
logger.debug("Received RELP frame with transaction {} and command {}",
new Object[] {frame.getTxnr(), frame.getCommand()});
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel);
frameHandler.handle(frame, responder, sender.toString());
socketBuffer.mark();
}
}
logger.debug("Done processing buffer");
} catch (final RELPFrameException rfe) {
logger.error("Error reading RELP frames due to {}", new Object[] {rfe.getMessage()}, rfe);
// if an invalid frame or bad data was sent then the decoder will be left in a
// corrupted state, so lets close the connection and cause the client to re-establish
dispatcher.completeConnection(key);
}
}
代码示例来源:origin: apache/incubator-druid
private ByteBuffer allocateEmptyHLLBuffer(boolean direct, boolean aligned, int offset)
{
final int size = HyperLogLogCollector.getLatestNumBytesForDenseStorage();
final byte[] EMPTY_BYTES = HyperLogLogCollector.makeEmptyVersionedByteArray();
final ByteBuffer buf;
if (direct) {
if (aligned) {
buf = ByteBuffers.allocateAlignedByteBuffer(size + offset, CACHE_LINE);
buf.position(offset);
buf.mark();
buf.limit(size + offset);
} else {
buf = ByteBuffer.allocateDirect(size);
buf.mark();
buf.limit(size);
}
buf.put(EMPTY_BYTES);
buf.reset();
} else {
buf = ByteBuffer.allocate(size);
buf.limit(size);
buf.put(EMPTY_BYTES);
buf.rewind();
}
return buf;
}
代码示例来源:origin: lettuce-io/lettuce-core
@Override
public void set(ByteBuffer bytes) {
if (firstElement == null && bytes != null && bytes.remaining() > 0) {
bytes.mark();
firstElement = StringCodec.ASCII.decodeKey(bytes);
bytes.reset();
}
super.set(bytes);
}
代码示例来源:origin: apache/hbase
in.mark(); // mark beginning of key
ByteBufferUtils.skip(in, rowLength);
familyLength = in.get();
type = in.get();
in.reset();
代码示例来源:origin: commons-io/commons-io
/**
* {@inheritDoc}
* @param readlimit max read limit (ignored)
*/
@Override
public synchronized void mark(final int readlimit) {
this.mark_cbuf = this.cbuf.position();
this.mark_bbuf = this.bbuf.position();
this.cbuf.mark();
this.bbuf.mark();
// It would be nice to be able to use mark & reset on the cbuf and bbuf;
// however the bbuf is re-used so that won't work
}
代码示例来源:origin: apache/drill
bytes.mark();
byte b = bytes.get();
bytes.reset();
int extraBytesToRead = bytesFromUTF8[(b & 0xFF)];
if (extraBytesToRead < 0)
ch += (bytes.get() & 0xFF);
ch <<= 6; /* remember, illegal UTF-8 */
case 4:
ch += (bytes.get() & 0xFF);
ch <<= 6; /* remember, illegal UTF-8 */
case 3:
内容来源于网络,如有侵权,请联系作者删除!