List of usage examples for io.netty.buffer ByteBuf getUnsignedByte
public abstract short getUnsignedByte(int index);
From source file:io.jsync.dns.impl.netty.decoder.TextDecoder.java
License:Open Source License
/** * Returns a decoded TXT (text) resource record, stored as an * {@link ArrayList} of {@code String}s. * * @param response the DNS response that contains the resource record being * decoded/* ww w. j a v a 2 s.com*/ * @param resource the resource record being decoded */ @Override public List<String> decode(DnsResponse response, DnsResource resource) { List<String> list = new ArrayList<String>(); ByteBuf data = resource.content().readerIndex(response.originalIndex()); int index = data.readerIndex(); while (index < data.writerIndex()) { int len = data.getUnsignedByte(index++); list.add(data.toString(index, len, CharsetUtil.UTF_8)); index += len; } return list; }
From source file:io.jsync.http.impl.cgbystrom.FlashPolicyHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buffer = (ByteBuf) msg; int index = buffer.readerIndex(); switch (state) { case MAGIC1:/*w w w . j a va 2s. c o m*/ if (!buffer.isReadable()) { return; } final int magic1 = buffer.getUnsignedByte(index++); state = ParseState.MAGIC2; if (magic1 != '<') { ctx.fireChannelRead(buffer); ctx.pipeline().remove(this); return; } // fall through case MAGIC2: if (!buffer.isReadable()) { return; } final int magic2 = buffer.getUnsignedByte(index); if (magic2 != 'p') { ctx.fireChannelRead(buffer); ctx.pipeline().remove(this); } else { ctx.writeAndFlush(Unpooled.copiedBuffer(XML, CharsetUtil.UTF_8)) .addListener(ChannelFutureListener.CLOSE); } } }
From source file:io.nebo.thrift.DefaultThriftFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { Channel channel = ctx.channel(); if (buffer.readableBytes() <= 0) { return;/*from w w w.j a v a 2 s .c om*/ } short firstByte = buffer.getUnsignedByte(0); if (firstByte >= 0x80) { ByteBuf messageBuffer = tryDecodeUnframedMessage(ctx, channel, buffer, inputProtocolFactory); if (messageBuffer == null) { return; } // A non-zero MSB for the first byte of the message implies the message starts with a // protocol id (and thus it is unframed). out.add(new ThriftMessage(messageBuffer, ThriftTransportType.UNFRAMED)); } else if (buffer.readableBytes() < MESSAGE_FRAME_SIZE) { // Expecting a framed message, but not enough bytes available to read the frame size return; } else { ByteBuf messageBuffer = tryDecodeFramedMessage(ctx, channel, buffer, true); if (messageBuffer == null) { return; } // Messages with a zero MSB in the first byte are framed messages out.add(new ThriftMessage(messageBuffer, ThriftTransportType.FRAMED)); } }
From source file:io.scalecube.socketio.serialization.PacketFramer.java
License:Apache License
private static int getUtf8CharCountByByteCount(final ByteBuf buffer, final int startByteIndex, final int bytesCount) { int charCount = 0; int endByteIndex = startByteIndex + bytesCount; int byteIndex = startByteIndex; while (byteIndex < endByteIndex) { charCount++;/*from w w w . j ava 2 s. com*/ // Define next char first byte short charFirstByte = buffer.getUnsignedByte(byteIndex); // Scan first byte of UTF-8 character according to: // http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 if ((charFirstByte >= 0x20) && (charFirstByte <= 0x7F)) { // characters U-00000000 - U-0000007F (same as ASCII) byteIndex++; } else if ((charFirstByte & 0xE0) == 0xC0) { // characters U-00000080 - U-000007FF, mask 110XXXXX byteIndex += 2; } else if ((charFirstByte & 0xF0) == 0xE0) { // characters U-00000800 - U-0000FFFF, mask 1110XXXX byteIndex += 3; } else if ((charFirstByte & 0xF8) == 0xF0) { // characters U-00010000 - U-001FFFFF, mask 11110XXX byteIndex += 4; } else if ((charFirstByte & 0xFC) == 0xF8) { // characters U-00200000 - U-03FFFFFF, mask 111110XX byteIndex += 5; } else if ((charFirstByte & 0xFE) == 0xFC) { // characters U-04000000 - U-7FFFFFFF, mask 1111110X byteIndex += 6; } else { byteIndex++; } } return charCount; }
From source file:io.scalecube.socketio.serialization.PacketFramer.java
License:Apache License
private static int getUtf8ByteCountByCharCount(final ByteBuf buffer, final int startIndex, final int charCount) { int bytesCount = 0; for (int charIndex = 0; charIndex < charCount; charIndex++) { // Define next char first byte int charFirstByteIndex = startIndex + bytesCount; short charFirstByte = buffer.getUnsignedByte(charFirstByteIndex); // Scan first byte of UTF-8 character according to: // http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 if ((charFirstByte >= 0x20) && (charFirstByte <= 0x7F)) { // characters U-00000000 - U-0000007F (same as ASCII) bytesCount++;//from w ww . j a va 2 s.c o m } else if ((charFirstByte & 0xE0) == 0xC0) { // characters U-00000080 - U-000007FF, mask 110XXXXX bytesCount += 2; } else if ((charFirstByte & 0xF0) == 0xE0) { // characters U-00000800 - U-0000FFFF, mask 1110XXXX bytesCount += 3; } else if ((charFirstByte & 0xF8) == 0xF0) { // characters U-00010000 - U-001FFFFF, mask 11110XXX bytesCount += 4; } else if ((charFirstByte & 0xFC) == 0xF8) { // characters U-00200000 - U-03FFFFFF, mask 111110XX bytesCount += 5; } else if ((charFirstByte & 0xFE) == 0xFC) { // characters U-04000000 - U-7FFFFFFF, mask 1111110X bytesCount += 6; } else { bytesCount++; } } return bytesCount; }
From source file:io.scalecube.socketio.serialization.PacketFramer.java
License:Apache License
private static CharSequence decodePacketLength(final ByteBuf buffer) { StringBuilder length = new StringBuilder(); final int scanStartIndex = buffer.readerIndex() + DELIMITER_BYTES_SIZE; final int scanEndIndex = buffer.readerIndex() + buffer.readableBytes(); for (int charIndex = scanStartIndex; charIndex < scanEndIndex; charIndex++) { if (isDelimiter(buffer, charIndex)) { break; } else {//from w ww. ja v a2 s.c om length.append((char) buffer.getUnsignedByte(charIndex)); } } return length; }
From source file:io.vertx.core.dns.impl.decoder.RecordDecoder.java
License:Open Source License
/** * Retrieves a domain name given a buffer containing a DNS packet without * advancing the readerIndex for the buffer. * * @param buf the byte buffer containing the DNS packet * @param offset the position at which the name begins * @return the domain name for an entry//from w w w. j a va2s . c o m */ static String getName(ByteBuf buf, int offset) { StringBuilder name = new StringBuilder(); for (int len = buf.getUnsignedByte(offset++); buf.writerIndex() > offset && len != 0; len = buf.getUnsignedByte(offset++)) { boolean pointer = (len & 0xc0) == 0xc0; if (pointer) { offset = (len & 0x3f) << 8 | buf.getUnsignedByte(offset++); } else { name.append(buf.toString(offset, len, CharsetUtil.UTF_8)).append("."); offset += len; } } if (name.length() == 0) { return null; } return name.substring(0, name.length() - 1); }
From source file:jss.proto.ResultsetPacketTest.java
/** * ??// ww w . j a va 2s.c o m */ @Test @Ignore public void testResultset() { ByteBuf buf = fromDumpBytes(BASIC_DUMP); System.out.println(); int flags = CapabilityFlags.CLIENT_BASIC_FLAGS; PacketData data = new PacketData(); readPacket(buf, data, flags); ByteBuf payload = data.payload; Dumper.hexDumpOut(payload); long fieldCount = int_lenenc(payload); readPacket(buf, data, flags); payload = data.payload; Dumper.hexDumpOut(payload); List<ColumnDefinition41> columns = Lists.newArrayList(); for (long i = 0; i < fieldCount; i++) { ColumnDefinition41 packet = readPacket(payload, new ColumnDefinition41(), 0); System.out.println(packet); // System.out.println("\n" + Dumper.hexDumpReadable(payload)); // System.out.println("\n" + Dumper.hexDumpReadable(buf)); readPacket(buf, data, 0); payload = data.payload; columns.add(packet); } readPacket(buf, data, flags); payload = data.payload; System.out.println(readPacket(payload, new EOF_Packet(), flags)); readPacket(buf, data, flags); payload = data.payload; // System.out.println("\n" + Dumper.hexDumpReadable(buf)); while (true) { readPacket(buf, data, flags); payload = data.payload; if (payload.getUnsignedByte(payload.readerIndex()) == 0xfe) { break; } System.out.printf("| "); for (long i = 0; i < fieldCount; i++) { System.out.printf(string_lenenc(payload).toString(UTF_8) + " | "); } System.out.println(); } System.out.println(readPacket(payload, new EOF_Packet(), flags)); System.out.println("\n" + Dumper.hexDumpReadable(buf)); }
From source file:log.server.handler.EightLengthFieldDecoder.java
License:Apache License
/** * Create a frame out of the {@link ByteBuf} and return it. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param in the {@link ByteBuf} from which to read data * @return frame the {@link ByteBuf} which represent the frame or {@code null} if no frame could * be created./*from www .ja v a2s. co m*/ */ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) 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); } int m = in.readableBytes(), i = 0; while (i < m) { short digit = in.getUnsignedByte(i); if (digit == ' ') break; i++; } if (i == m) { byte[] tests = new byte[in.readableBytes()]; in.readBytes(tests); String log = new String(tests); logger.info("readablebytes======konggechangdu==" + log); return null; } lengthFieldLength = i; if (in.readableBytes() < lengthFieldLength) { logger.info("readablebytes<<<<<lengthFieldLength"); return null; } initialBytesToStrip = i + lengthAdjustment; lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength; int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset; long frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder); if (frameLength < 0) { in.skipBytes(lengthFieldEndOffset); byte[] tests = new byte[in.readableBytes()]; in.readBytes(tests); String log = new String(tests); throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength + "log" + log); } frameLength += lengthAdjustment + lengthFieldEndOffset; if (frameLength < lengthFieldEndOffset) { in.skipBytes(lengthFieldEndOffset); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than lengthFieldEndOffset: " + lengthFieldEndOffset); } 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 { // Enter the discard mode and discard everything received so far. discardingTooLongFrame = true; bytesToDiscard = discard; in.skipBytes(in.readableBytes()); } failIfNecessary(true); return null; } // never overflows because it's less than maxFrameLength int frameLengthInt = (int) frameLength; if (in.readableBytes() < frameLengthInt) { return null; } if (initialBytesToStrip > frameLengthInt) { in.skipBytes(frameLengthInt); throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less " + "than initialBytesToStrip: " + initialBytesToStrip); } in.skipBytes(initialBytesToStrip); // extract frame int readerIndex = in.readerIndex(); int actualFrameLength = frameLengthInt - initialBytesToStrip; ByteBuf frame = extractFrame(ctx, in, readerIndex, actualFrameLength); in.readerIndex(readerIndex + actualFrameLength); return frame; }
From source file:log.server.handler.EightLengthFieldDecoder.java
License:Apache License
/** * Decodes the specified region of the buffer into an unadjusted frame length. The default implementation is * capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer. Override this method to * decode the length field encoded differently. Note that this method must not modify the state of the specified * buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.) * * @throws DecoderException if failed to decode the specified region *//*from w w w .j a v a 2 s.c om*/ protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) { buf = buf.order(order); long frameLength = 0; if (length > 8) { byte[] tests = new byte[buf.readableBytes()]; buf.readBytes(tests); String log = new String(tests); throw new DecoderException( "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: <= 8)" + log); } for (int i = 0; i < length; i++) { frameLength = frameLength * 10 + buf.getUnsignedByte(offset + i) - '0'; } return frameLength; }