本文整理了Java中java.nio.ByteBuffer.reset()
方法的一些代码示例,展示了ByteBuffer.reset()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.reset()
方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:reset
暂无
代码示例来源: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: 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: 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: 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: 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: 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: sannies/mp4parser
public static void main(String[] args) throws IOException {
Movie movie = MovieCreator.build(DumpAmf0TrackToPropertyFile.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/example.f4v");
for (Track track : movie.getTracks()) {
if (track.getHandler().equals("data") ) {
long time = 0;
Iterator<Sample> samples = track.getSamples().iterator();
Properties properties = new Properties();
File f = File.createTempFile(DumpAmf0TrackToPropertyFile.class.getSimpleName(), "" + track.getTrackMetaData().getTrackId());
for (long decodingTime : track.getSampleDurations()) {
ByteBuffer sample = samples.next().asByteBuffer();
byte[] sampleBytes = new byte[sample.limit()];
sample.reset();
sample.get(sampleBytes);
properties.put("" + time, new String(Base64.encodeBase64(sampleBytes, false, false)));
time += decodingTime;
}
FileOutputStream fos = new FileOutputStream(f);
System.err.println(properties);
properties.store(fos, "");
}
}
}
代码示例来源: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: apache/hbase
/**
* Similar to {@link ByteBuffer}.reset(), ensures that this MBB
* is reset back to last marked position.
* @return This MBB
*/
@Override
public MultiByteBuff reset() {
// when the buffer is moved to the next one.. the reset should happen on the previous marked
// item and the new one should be taken as the base
if (this.markedItemIndex < 0) throw new InvalidMarkException();
ByteBuffer markedItem = this.items[this.markedItemIndex];
markedItem.reset();
this.curItem = markedItem;
// All items after the marked position upto the current item should be reset to 0
for (int i = this.curItemIndex; i > this.markedItemIndex; i--) {
this.items[i].position(0);
}
this.curItemIndex = this.markedItemIndex;
return this;
}
代码示例来源:origin: killme2008/xmemcached
LABEL: switch (this.decodeStatus) {
case NONE:
if (buffer.remaining() < 24) {
return false;
} else {
buffer.reset();
return true;
代码示例来源: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/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/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:
代码示例来源:origin: apache/hbase
in.mark(); // mark beginning of key
ByteBufferUtils.skip(in, rowLength);
familyLength = in.get();
type = in.get();
in.reset();
代码示例来源:origin: hector-client/hector
private byte[] copyFromMark(ByteBuffer bb, int segStart, int segSize) {
byte[] newSeg = new byte[segSize];
bb.reset();
bb.get(newSeg, 0, segSize);
return newSeg;
}
代码示例来源:origin: apache/kafka
/**
* Write all records to the given channel (including partial records).
* @param channel The channel to write to
* @return The number of bytes written
* @throws IOException For any IO errors writing to the channel
*/
public int writeFullyTo(GatheringByteChannel channel) throws IOException {
buffer.mark();
int written = 0;
while (written < sizeInBytes())
written += channel.write(buffer);
buffer.reset();
return written;
}
代码示例来源:origin: JanusGraph/janusgraph
@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: rapidoid/rapidoid
public synchronized boolean unwrapInput() {
boolean success = false;
boolean shouldUnwrap = true;
while (!isClosed() && shouldUnwrap && netIn.position() > 0) {
debug("- UNWRAP");
netIn.flip(); // prepare for reading
netIn.mark(); // backup, to rollback in case of underflow
SSLEngineResult result = unwrap(netIn, appIn);
boolean underflow = result.getStatus().equals(SSLEngineResult.Status.BUFFER_UNDERFLOW);
if (!underflow) {
// prepare for writing
netIn.compact();
} else {
// rollback
netIn.reset();
netIn.compact();
}
appIn.flip();
conn.input.append(appIn);
appIn.clear();
reactToResult(result);
shouldUnwrap = !result.getStatus().equals(SSLEngineResult.Status.BUFFER_UNDERFLOW)
&& !result.getHandshakeStatus().equals(SSLEngineResult.HandshakeStatus.NEED_TASK)
&& !result.getHandshakeStatus().equals(SSLEngineResult.HandshakeStatus.NEED_WRAP);
success = true;
}
return success;
}
代码示例来源:origin: apache/ignite
raw1.mark();
raw2.mark();
int splits = Math.min(raw1.remaining(), raw2.remaining());
assertEquals(val, ((GridClientCacheRequest)msg).value2());
raw1.reset();
raw2.reset();
内容来源于网络,如有侵权,请联系作者删除!