List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java
License:Apache License
/** * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library * which is faster as it computes using hardware machine instruction else it computes using crc32c algo. * * @param payload/* w w w. j a va 2 s. c o m*/ * @return */ public static int computeChecksum(ByteBuf payload) { if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { return CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { return CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { return CRC32C_HASH.calculate(payload.nioBuffer()); } }
From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java
License:Apache License
/** * Computes incremental checksum with input previousChecksum and input payload * * @param previousChecksum : previously computed checksum * @param payload//from ww w .j a v a 2 s .c o m * @return */ public static int resumeChecksum(int previousChecksum, ByteBuf payload) { if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { return CRC32C_HASH.resume(previousChecksum, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { return CRC32C_HASH.resume(previousChecksum, payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { return CRC32C_HASH.resume(previousChecksum, payload.nioBuffer()); } }
From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java
License:Apache License
/** * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library * which is faster as it computes using hardware machine instruction else it computes using crc32c algo. * * @param payload//from w w w .j a v a2 s . c o m * @return */ public static long computeChecksum(ByteBuf payload) { int crc; if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { crc = CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { crc = CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { crc = CRC32C_HASH.calculate(payload.nioBuffer()); } return crc & 0xffffffffL; }
From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java
License:Apache License
/** * Computes incremental checksum with input previousChecksum and input payload * * @param previousChecksum : previously computed checksum * @param payload/* ww w . ja v a2 s .c o m*/ * @return */ public static long resumeChecksum(long previousChecksum, ByteBuf payload) { int crc = (int) previousChecksum; if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { crc = CRC32C_HASH.resume(crc, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { crc = CRC32C_HASH.resume(crc, payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { crc = CRC32C_HASH.resume(crc, payload.nioBuffer()); } return crc & 0xffffffffL; }
From source file:com.shelf.messagepack.MessagePackFrameDecoder.java
License:Apache License
public long decodeLength(ByteBuf in, int offset) throws Exception { if (discardingTooLongFrame) { long bytesToDiscard = this.bytesToDiscard; int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes()); in.skipBytes(localBytesToDiscard); bytesToDiscard -= localBytesToDiscard; this.bytesToDiscard = bytesToDiscard; failIfNecessary(false);/* w w w. j ava2s .c om*/ } int readerIndex = in.readerIndex() + offset; short b = in.getUnsignedByte(readerIndex); int ubyte = b & 0xff; LOGGER.trace("message: " + toHex(ubyte)); switch (ubyte) { case NIL: return 1L; case FALSE: return 1L; case TRUE: return 1L; case BIN8: { short length = in.getUnsignedByte(readerIndex + 1); return 2L + length; } case BIN16: { int length = in.getUnsignedShort(readerIndex + 1); return 3L + length; } case BIN32: { long length = in.getUnsignedInt(readerIndex + 1); return 5L + length; } case EXT8: { short length = in.getUnsignedByte(readerIndex + 1); return 3L + length; } case EXT16: { int length = in.getUnsignedShort(readerIndex + 1); return 4L + length; } case EXT32: { long length = in.getUnsignedInt(readerIndex + 1); return 6L + length; } case FLOAT32: return 5L; case FLOAT64: return 9L; case UINT8: return 2L; case UINT16: return 3L; case UINT32: return 5L; case UINT64: return 9L; case INT8: return 2L; case INT16: return 3L; case INT32: return 5L; case INT64: return 9L; case FIXEXT1: return 3L; case FIXEXT2: return 4L; case FIXEXT4: return 6L; case FIXEXT8: return 10L; case FIXEXT16: return 18L; case STR8: { short length = in.getUnsignedByte(readerIndex + 1); return 2L + length; } case STR16: { int length = in.getUnsignedShort(readerIndex + 1); return 3L + length; } case STR32: { long length = in.getUnsignedInt(readerIndex + 1); return 5L + length; } case ARRAY16: { int elemCount = in.getUnsignedShort(readerIndex + 1); return getArraySize(in, 3, offset, elemCount); } case ARRAY32: { long elemCount = in.getUnsignedInt(readerIndex + 1); return getArraySize(in, 5, offset, elemCount); } case MAP16: { int elemCount = in.getUnsignedShort(readerIndex + 1); return getArraySize(in, 3, offset, elemCount * 2); } case MAP32: { long elemCount = in.getUnsignedInt(readerIndex + 1); return getArraySize(in, 5, offset, elemCount * 2); } default: if ((ubyte >> 7) == 0) { //positive fixint return 1L; } else if ((ubyte >> 4) == 0b1000) { //fixmap int elemCount = ubyte & 0b00001111; return getArraySize(in, 1, offset, elemCount * 2); } else if ((ubyte >> 4) == 0b1001) { //fixarray int elemCount = ubyte & 0b00001111; return getArraySize(in, 1, offset, elemCount); } else if ((ubyte >> 5) == 0b101) { //fixstr int length = ubyte & 0b00011111; return 1L + length; } else if ((ubyte >> 5) == 0b111) { //negative fixint return 1L; } else { throw new CorruptedFrameException("Unknown header byte of message: " + toHex(ubyte)); } } }
From source file:com.shelf.messagepack.MessagePackFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { long frameLength = decodeLength(in, 0); if (frameLength > maxFrameLength) { long discard = frameLength - in.readableBytes(); tooLongFrameLength = frameLength; if (discard < 0) { // buffer contains more bytes then the frameLength so we can discard all now in.skipBytes((int) frameLength); } else {//from w w w . ja v a 2 s. co m // Enter the discard mode and discard everything received so far. discardingTooLongFrame = true; bytesToDiscard = discard; in.skipBytes(in.readableBytes()); } failIfNecessary(true); } else { int readerIndex = in.readerIndex(); int actualFrameLength = (int) frameLength; ByteBuf buffer = extractFrame(ctx, in, readerIndex, actualFrameLength); in.readerIndex(readerIndex + actualFrameLength); out.add(buffer); } }
From source file:com.spotify.folsom.client.binary.RequestTestTemplate.java
License:Apache License
protected void assertEOM(final ByteBuf b) { assertEquals(b.readerIndex(), b.writerIndex()); }
From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java
License:Apache License
/** * Create a string from binary data, keeping printable ascii and hex encoding everything else. * * @param data The data//from www . ja v a 2 s . c o m * @return A string representation of the data */ public static String toString(final ByteBuf data) { if (data == null) { return null; } final StringBuilder sb = new StringBuilder(); for (int i = data.readerIndex(); i < data.writerIndex(); i++) { final byte b = data.getByte(i); if (b > 31 && b < 127) { if (b == '%') { sb.append('%'); } sb.append((char) b); } else { sb.append('%'); sb.append(String.format("%02X", b)); } } return sb.toString(); }
From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java
License:Apache License
private static boolean asciiEquals(final AsciiString s, final ByteBuf data, final int size) { final int ix = data.readerIndex(); if (size != s.length()) { return false; }//from w ww . ja va2 s. c o m for (int i = 0; i < size; i++) { char c = (char) data.getByte(ix + i); if (c != s.charAt(i)) { return false; } } return true; }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPFramingDecoder.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws ZMTPParsingException { while (in.isReadable()) { if (!headerParsed) { final int mark = in.readerIndex(); headerParsed = header.read(in); if (!headerParsed) { // Wait for more data in.readerIndex(mark);//ww w . j a v a 2 s . co m return; } decoder.header(ctx, header.length(), header.more(), out); remaining = header.length(); } final int writerMark = in.writerIndex(); final int n = (int) min(remaining, in.readableBytes()); final int readerMark = in.readerIndex(); in.writerIndex(readerMark + n); decoder.content(ctx, in, out); in.writerIndex(writerMark); final int read = in.readerIndex() - readerMark; remaining -= read; if (remaining > 0) { // Wait for more data return; } if (!header.more()) { decoder.finish(ctx, out); } headerParsed = false; } }