List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
From source file:de.sanandrew.mods.turretmod.network.PacketSyncUpgradeInst.java
License:Creative Commons License
@Override public void fromBytes(ByteBuf buf) { this.turretId = buf.readInt(); this.upgradeId = new UUID(buf.readLong(), buf.readLong()); int lng = buf.readInt(); this.instData = new byte[lng]; if (lng > 0) { buf.readBytes(this.instData); }/* ww w.ja v a 2 s .co m*/ }
From source file:de.sanandrew.mods.turretmod.network.PacketUpdateTurretState.java
License:Creative Commons License
@Override public void fromBytes(ByteBuf buf) { this.turretId = buf.readInt(); this.entityToAttackId = buf.readInt(); this.currAmmoCap = buf.readInt(); this.isShooting = buf.readBoolean(); this.ammoStack = ByteBufUtils.readItemStack(buf); int lng = buf.readInt(); this.delegateData = new byte[lng]; if (lng > 0) { buf.readBytes(this.delegateData); }/* w ww. j a v a 2 s .c o m*/ }
From source file:de.saxsys.synchronizefx.netty.base.CommandToBinaryByteBuf.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf msg, final List<Object> out) throws Exception { byte[] data;//from ww w .ja v a 2 s .co m if (msg.hasArray()) { data = msg.array(); } else { data = new byte[msg.readableBytes()]; msg.readBytes(data); } out.add(serializer.deserialize(data)); }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureChecker.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try {/*from w ww .j a va 2s .c o m*/ if (msg instanceof ByteBuf) { final ByteBuf in = (ByteBuf) msg; final int dataLength = in.readInt(); final ByteBuf data = in.readSlice(dataLength); final int signatureLength = in.readInt(); final byte[] signature = new byte[signatureLength]; in.readBytes(signature); verifySignature.update(data.nioBuffer()); final boolean valid = verifySignature.verify(signature); //Log.v(TAG, "Read " + dataLength + "b of data with " + signatureLength + "b " + // (valid ? "valid" : "invalid") + " signature" + // (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : "")); if (valid) { data.retain(); ctx.fireChannelRead(data); } else { throw new SignatureException("Message has a broken signature, closing connection"); } } else { throw new SignatureException("Can't check signature of message of type " + (msg != null ? msg.getClass() : "null") + ", closing connection"); } } catch (SignatureException | RuntimeException e) { ctx.close(); throw e; } }
From source file:de.unipassau.isl.evs.ssh.core.sec.DeviceConnectInformation.java
License:Open Source License
/** * Read a DeviceConnectInformation from a Base64 encoded String, which was read from a QR Code. *//*from w w w .j ava 2 s . c o m*/ public static DeviceConnectInformation fromDataString(String data) throws IOException { final ByteBuf base64 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(data.length()); ByteBufUtil.writeAscii(base64, data); final ByteBuf byteBuf = decode(base64); if (byteBuf.readableBytes() != DATA_LENGTH) { throw new IOException("too many bytes encoded"); } final byte[] addressData = new byte[ADDRESS_LENGTH]; byteBuf.readBytes(addressData); final InetAddress address = InetAddress.getByAddress(addressData); final int port = byteBuf.readUnsignedShort(); final byte[] idData = new byte[DeviceID.ID_LENGTH]; byteBuf.readBytes(idData); final DeviceID id = new DeviceID(idData); final byte[] encodedToken = new byte[TOKEN_BASE64_LENGTH]; byteBuf.readBytes(encodedToken); final byte[] token = decodeToken(new String(encodedToken)); return new DeviceConnectInformation(address, port, id, token); }
From source file:deathcap.wsmc.web.WebSocketHandler.java
License:Apache License
@Override protected void messageReceived(final ChannelHandlerContext ctx, BinaryWebSocketFrame msg) throws Exception { // channelRead if (firstMessage) { firstMessage = false;/*from w w w . j ava 2 s . co m*/ this.webThread.getChannelGroup().add(ctx.channel()); } MinecraftThread minecraft = minecraftThreads.get(ctx.channel().remoteAddress().toString()); if (minecraft == null) { this.setupInitialConnection(ctx, msg); return; } final ByteBuf buf = msg.content(); if (verbose) logger.info("ws received " + buf.readableBytes() + " bytes: " + HexDumper.hexByteBuf(buf)); byte bytes[] = new byte[buf.readableBytes()]; buf.readBytes(bytes); // read packet id type for filtering int id = DefinedPacket.readVarInt(Unpooled.copiedBuffer(bytes)); // TODO: avoid copying (but need to reply with id in buffer) if (!this.filter.isAllowed(id)) { logger.info("FILTERED PACKET: " + id); return; } final ByteBuf reply = Unpooled.wrappedBuffer(bytes).retain(); if (verbose) logger.info( "id " + id + " stripped " + reply.readableBytes() + " reply=" + HexDumper.hexByteBuf(reply)); final MinecraftThread mc = minecraft; // forward MC to WS try { final ChannelFuture f = mc.clientHandler.minecraftClientHandler.ctx.writeAndFlush(reply); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { try { assert f == channelFuture; if (verbose) logger.info("forwarded WS -> MC, " + reply.readableBytes() + " bytes"); reply.release(); } catch (RejectedExecutionException ex) { // TODO } } }); } catch (RejectedExecutionException ex) { //TODO mc.clientHandler.minecraftClientHandler.close(ctx, ) } }
From source file:demo.netty.stickypacket.err.TimeClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8"); // String body = (String) msg; System.out.println("Now is : " + body + " ; the counter is : " + ++counter); }
From source file:demo.netty.stickypacket.err.TimeServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8").substring(0, req.length - System.getProperty("line.separator").length()); // String body = (String) msg; System.out.println("The time server receive order : " + body + " ; the counter is : " + ++counter); String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(System.currentTimeMillis()).toString() : "BAD ORDER"; currentTime = currentTime + System.getProperty("line.separator"); ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes()); ctx.writeAndFlush(resp);// ww w. j a v a2s. co m }
From source file:discord4j.gateway.ZlibDecompressor.java
License:Open Source License
public Flux<ByteBuf> completeMessages(Flux<ByteBuf> payloads) { return payloads.windowUntil(windowPredicate).flatMap(Flux::collectList).map(list -> { ByteBuf buf = Unpooled.wrappedBuffer(list.toArray(new ByteBuf[0])); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (InflaterOutputStream inflater = new InflaterOutputStream(out, context)) { inflater.write(bytes);// ww w. j a v a2 s . c om return Unpooled.wrappedBuffer(out.toByteArray()); } catch (IOException e) { throw Exceptions.propagate(e); } }); }
From source file:divconq.ctp.f.BlockCommand.java
License:Open Source License
@Override public boolean decode(ByteBuf in) { while (this.state != State.DONE) { switch (this.state) { case BLOCK_TYPE: { if (in.readableBytes() < 1) return false; this.blocktype = in.readUnsignedByte(); this.eof = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_EOF) != 0); this.skipHeaders = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_HEADER) == 0); this.skipPayload = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_CONTENT) == 0); // completely done, exit the loop and decode if (this.skipHeaders && this.skipPayload) { this.state = State.DONE; break; }//w w w .j a v a 2s.com // to skip headers, go back to loop if (this.skipHeaders) { this.state = State.STREAM_OFFSET; break; } this.state = State.HEADER_ATTR; // deliberate fall through } case HEADER_ATTR: { if (in.readableBytes() < 2) return false; this.currattr = in.readShort(); // done with headers, go back to loop to skip down to payload if (this.currattr == CtpConstants.CTP_F_ATTR_END) { if (this.skipPayload) this.state = State.DONE; else this.state = State.STREAM_OFFSET; break; } this.state = State.HEADER_SIZE; // deliberate fall through } case HEADER_SIZE: { if (in.readableBytes() < 2) return false; this.currasize = in.readShort(); // an empty attribute is like a flag - present but no data // go on to next header if (this.currasize == 0) { this.headers.put(this.currattr, new byte[0]); this.currattr = 0; this.state = State.HEADER_ATTR; break; } this.state = State.HEADER_VALUE; // deliberate fall through } case HEADER_VALUE: { if (in.readableBytes() < this.currasize) return false; byte[] val = new byte[this.currasize]; in.readBytes(val); this.headers.put(this.currattr, val); this.currattr = 0; this.currasize = 0; this.state = State.HEADER_ATTR; break; } case STREAM_OFFSET: { if (in.readableBytes() < 8) return false; this.streamOffset = in.readLong(); this.state = State.PAYLOAD_SIZE; // deliberate fall through } case PAYLOAD_SIZE: { if (in.readableBytes() < 3) return false; this.paysize = in.readMedium(); this.state = State.PAYLOAD; // deliberate fall through } case PAYLOAD: { // return here, without any state reset, means we need more before we can decide what to do if (in.readableBytes() < this.paysize) return false; // add Data only if there are some bytes, otherwise skip buffer allocation if (this.paysize > 0) { ByteBuf bb = in.readSlice(this.paysize); bb.retain(); this.data = bb; } this.state = State.DONE; // deliberate fall through } case DONE: { break; } } } return true; }