List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private long parseLengthHeader(ByteBuf buffer) { int delimiterIndex = delimiterIndexOf(buffer, buffer.readerIndex(), buffer.readableBytes()); if (delimiterIndex == -1) { throw new DecoderException("Can't find tail delimiter"); }/* w ww .j a va 2s . co m*/ long len = parseLong(buffer, delimiterIndex); buffer.readerIndex(delimiterIndex + Packet.DELIMITER_BYTES.length); return len; }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private int delimiterIndexOf(ByteBuf buffer, int startIndex, int length) { ByteBuf buf = buffer.slice(startIndex, length); for (int i = 0; i < buf.readableBytes(); i++) { if (isCurrentDelimiter(buf, i)) { return startIndex + i; }/*from ww w.ja v a2 s .c o m*/ } return -1; }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private boolean hasLengthHeader(ByteBuf buffer) { for (int i = 0; i < Math.min(buffer.readableBytes(), 10); i++) { byte b = buffer.getByte(buffer.readerIndex() + i); if (b == (byte) ':' && i > 0) { return true; }/*from w w w. j a v a2 s . co m*/ if (b > 57 || b < 48) { return false; } } return false; }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private String readString(ByteBuf frame) { return readString(frame, frame.readableBytes()); }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private Packet parseBinary(ClientHead head, ByteBuf frame) throws IOException { if (frame.getByte(0) == 1) { frame.readByte();/*ww w. j a v a 2 s .c o m*/ int headEndIndex = frame.bytesBefore((byte) -1); int len = (int) readLong(frame, headEndIndex); ByteBuf oldFrame = frame; frame = frame.slice(oldFrame.readerIndex() + 1, len); oldFrame.readerIndex(oldFrame.readerIndex() + 1 + len); } if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') { frame.readShort(); } else if (frame.getByte(0) == 4) { frame.readByte(); } Packet binaryPacket = head.getLastBinaryPacket(); if (binaryPacket != null) { ByteBuf attachBuf; if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') { attachBuf = frame; } else { attachBuf = Base64.encode(frame); } binaryPacket.addAttachment(Unpooled.copiedBuffer(attachBuf)); frame.readerIndex(frame.readerIndex() + frame.readableBytes()); if (binaryPacket.isAttachmentsLoaded()) { LinkedList<ByteBuf> slices = new LinkedList<ByteBuf>(); ByteBuf source = binaryPacket.getDataSource(); for (int i = 0; i < binaryPacket.getAttachments().size(); i++) { ByteBuf attachment = binaryPacket.getAttachments().get(i); ByteBuf scanValue = Unpooled.copiedBuffer("{\"_placeholder\":true,\"num\":" + i + "}", CharsetUtil.UTF_8); int pos = PacketEncoder.find(source, scanValue); if (pos == -1) { throw new IllegalStateException( "Can't find attachment by index: " + i + " in packet source"); } ByteBuf prefixBuf = source.slice(source.readerIndex(), pos - source.readerIndex()); slices.add(prefixBuf); slices.add(QUOTES); slices.add(attachment); slices.add(QUOTES); source.readerIndex(pos + scanValue.readableBytes()); } slices.add(source.slice()); ByteBuf compositeBuf = Unpooled.wrappedBuffer(slices.toArray(new ByteBuf[slices.size()])); parseBody(head, compositeBuf, binaryPacket); head.setLastBinaryPacket(null); return binaryPacket; } } return new Packet(PacketType.MESSAGE); }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private void parseBody(ClientHead head, ByteBuf frame, Packet packet) throws IOException { if (packet.getType() == PacketType.MESSAGE) { if (packet.getSubType() == PacketType.CONNECT || packet.getSubType() == PacketType.DISCONNECT) { packet.setNsp(readString(frame)); }//from www .j av a 2 s . c o m if (packet.getSubType() == PacketType.ACK) { ByteBufInputStream in = new ByteBufInputStream(frame); AckCallback<?> callback = ackManager.getCallback(head.getSessionId(), packet.getAckId()); AckArgs args = jsonSupport.readAckArgs(in, callback); packet.setData(args.getArgs()); } if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.BINARY_EVENT) { if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) { packet.setDataSource(Unpooled.copiedBuffer(frame)); frame.readerIndex(frame.readableBytes()); head.setLastBinaryPacket(packet); } else { ByteBufInputStream in = new ByteBufInputStream(frame); Event event = jsonSupport.readValue(packet.getNsp(), in, Event.class); packet.setName(event.getName()); packet.setData(event.getArgs()); } } } }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodeJsonP(Integer jsonpIndex, Queue<Packet> packets, ByteBuf out, ByteBufAllocator allocator, int limit) throws IOException { boolean jsonpMode = jsonpIndex != null; ByteBuf buf = allocateBuffer(allocator); int i = 0;/*from w w w . j av a 2s. com*/ while (true) { Packet packet = packets.poll(); if (packet == null || i == limit) { break; } ByteBuf packetBuf = allocateBuffer(allocator); encodePacket(packet, packetBuf, allocator, true); int packetSize = packetBuf.writerIndex(); buf.writeBytes(toChars(packetSize)); buf.writeBytes(B64_DELIMITER); buf.writeBytes(packetBuf); packetBuf.release(); i++; for (ByteBuf attachment : packet.getAttachments()) { ByteBuf encodedBuf = Base64.encode(attachment, Base64Dialect.URL_SAFE); buf.writeBytes(toChars(encodedBuf.readableBytes() + 2)); buf.writeBytes(B64_DELIMITER); buf.writeBytes(BINARY_HEADER); buf.writeBytes(encodedBuf); } } if (jsonpMode) { out.writeBytes(JSONP_HEAD); out.writeBytes(toChars(jsonpIndex)); out.writeBytes(JSONP_START); } processUtf8(buf, out, jsonpMode); buf.release(); if (jsonpMode) { out.writeBytes(JSONP_END); } }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodePackets(Queue<Packet> packets, ByteBuf buffer, ByteBufAllocator allocator, int limit) throws IOException { int i = 0;// ww w. j a v a 2 s . c o m while (true) { Packet packet = packets.poll(); if (packet == null || i == limit) { break; } encodePacket(packet, buffer, allocator, false); i++; for (ByteBuf attachment : packet.getAttachments()) { buffer.writeByte(1); buffer.writeBytes(longToBytes(attachment.readableBytes() + 1)); buffer.writeByte(0xff); buffer.writeByte(4); buffer.writeBytes(attachment); } } }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public static int find(ByteBuf buffer, ByteBuf searchValue) { for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { if (isValueFound(buffer, i, searchValue)) { return i; }//from ww w .j av a 2s.co m } return -1; }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
private static boolean isValueFound(ByteBuf buffer, int index, ByteBuf search) { for (int i = 0; i < search.readableBytes(); i++) { if (buffer.getByte(index + i) != search.getByte(i)) { return false; }/*from ww w . j av a 2 s . com*/ } return true; }