/** * 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; }
@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(); }
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();
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 );
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);
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);
/** * 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; }
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; }
@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); } }
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; }
/** * {@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 }