List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:ivorius.ivtoolkit.tools.IvNBTHelper.java
License:Apache License
public static void writeNBTLongs(String id, long[] longs, NBTTagCompound compound) { if (longs != null) { ByteBuf bytes = Unpooled.buffer(longs.length * 8); for (long aLong : longs) bytes.writeLong(aLong);//from ww w. jav a 2s . c o m compound.setByteArray(id, bytes.array()); } }
From source file:me.ferrybig.p2pnetwork.Peer.java
public Future<?> pingAddress(Address addr) { int packetNumber = pingPacketCounter.getAndIncrement(); byte[] data = new byte[4]; ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(data); wrappedBuffer.writerIndex(0);//www . ja va 2 s . c om wrappedBuffer.writeInt(packetNumber); assert wrappedBuffer.array() == data; Promise<PongPacket> promise = events.newPromise(); pingListeners.put(packetNumber, promise); promise.addListener(e -> pingListeners.remove(packetNumber)); boolean send = routePacket(addr, new PingPacket(data)); if (!send) { promise.setFailure(new IllegalArgumentException("Unknown address")); } return promise; }
From source file:minetweaker.mc1102.network.MineTweakerCopyClipboardPacket.java
@Override public void fromBytes(ByteBuf buf) { data = UTF_8.decode(ByteBuffer.wrap(buf.array())).toString().trim(); }
From source file:minetweaker.mc1102.network.MineTweakerOpenBrowserPacket.java
@Override public void fromBytes(ByteBuf buf) { url = UTF8.decode(ByteBuffer.wrap(buf.array())).toString().trim(); }
From source file:mysql.client.Session_Old.java
final String readString(ByteBuf buf, String encoding) throws Exception { int i = buf.readerIndex(); int len = 0;/*from w w w . j av a 2 s. c om*/ int maxLen = buf.writerIndex(); while ((i < maxLen) && (buf.getByte(i) != 0)) { len++; i++; } try { return StringUtils.toString(buf.array(), buf.readerIndex(), len, encoding); } catch (UnsupportedEncodingException uEE) { throw uEE; } finally { buf.readerIndex(buf.readerIndex() + (len + 1)); } }
From source file:mysql.client.Session_Old.java
String readString(ByteBuf buf, String encoding, int expectedLength) throws Exception { if (buf.readerIndex() + expectedLength > buf.writerIndex()) { throw new RuntimeException(); }// w w w.ja v a 2 s .c o m try { return StringUtils.toString(buf.array(), buf.readerIndex(), expectedLength, encoding); } catch (UnsupportedEncodingException uEE) { throw uEE; } finally { buf.readerIndex(buf.readerIndex() + expectedLength); } }
From source file:mysql.client.Session_Old.java
public void send(ByteBuf packet) throws IOException { this.packetSequence++; int position = packet.writerIndex(); packet.readerIndex(0);//from ww w . ja va2 s. c o m packet.writerIndex(0); int size = position - 4; packet.writeByte((byte) (size & 0xff)); packet.writeByte((byte) (size >>> 8)); packet.writeByte((byte) (size >>> 16)); packet.writeByte(packetSequence); socket.getOutputStream().write(packet.array()); socket.getOutputStream().flush(); }
From source file:nats.codec.ClientFrameDecoder.java
License:Open Source License
@Override protected ClientFrame decodeCommand(ChannelHandlerContext context, String command, ByteBuf in) { LOGGER.trace("Decoding '{}'", command); // CONNECT/* w ww . ja v a 2 s . c om*/ if (command.startsWith(CMD_CONNECT)) { final String body = command.substring(CMD_CONNECT.length()).trim(); final ConnectBody connectBody = ConnectBody.parse(body); return new ClientConnectFrame(connectBody); } // PING if (PING_PATTERN.matcher(command).matches()) { return ClientPingFrame.PING; } // PONG if (PONG_PATTERN.matcher(command).matches()) { return ClientPongFrame.PONG; } // PUB if (command.startsWith(CMD_PUBLISH)) { try { final String[] parts = command.substring(CMD_PUBLISH.length()).trim().split("\\s+"); if (parts.length < 2 || parts.length > 3) { throw new NatsDecodingException(command); } final String subject = parts[0]; final int length = Integer.parseInt(parts[parts.length - 1]); final String replyTo = (parts.length == 3) ? parts[1] : null; final ByteBuf bodyBytes = in.readBytes(length); in.skipBytes(ByteBufUtil.CRLF.length); final String body = new String(bodyBytes.array()); return new ClientPublishFrame(subject, body, replyTo); } catch (NumberFormatException e) { throw new NatsDecodingException(command); } } // SUB if (command.startsWith(CMD_SUBSCRIBE)) { final String[] parts = command.substring(CMD_SUBSCRIBE.length()).trim().split("\\s+"); if (parts.length < 2 || parts.length > 3) { throw new NatsDecodingException(command); } final String subject = parts[0]; final String id = parts[parts.length - 1]; final String queueGroup = (parts.length == 3) ? parts[1] : null; return new ClientSubscribeFrame(id, subject, queueGroup); } // UNSUB if (command.startsWith(CMD_UNSUBSCRIBE)) { final String[] parts = command.substring(CMD_UNSUBSCRIBE.length()).trim().split("\\s+"); if (parts.length < 1 || parts.length > 2) { throw new NatsDecodingException(command); } final String id = parts[0]; final Integer maxMessages = (parts.length == 2) ? Integer.valueOf(parts[1]) : null; return new ClientUnsubscribeFrame(id, maxMessages); } throw new NatsDecodingException(command); }
From source file:nats.codec.ServerFrameDecoder.java
License:Open Source License
@Override protected ServerFrame decodeCommand(ChannelHandlerContext context, String command, ByteBuf in) { LOGGER.trace("Decoding '{}'", command); Matcher matcher = MSG_PATTERN.matcher(command); if (matcher.matches()) { final String subject = matcher.group(1); final String id = matcher.group(2); final String replyTo = matcher.group(4); final int length = Integer.valueOf(matcher.group(5)); if (length > getMaxMessageSize()) { throwTooLongFrameException(context); }//w ww .j av a 2s .c o m final ByteBuf bodyBytes = in.readBytes(length); final String body = new String(bodyBytes.array()); in.skipBytes(ByteBufUtil.CRLF.length); return new ServerPublishFrame(id, subject, replyTo, body); } matcher = INFO_PATTERN.matcher(command); if (matcher.matches()) { return new ServerInfoFrame(matcher.group(1)); } matcher = OK_PATTERN.matcher(command); if (matcher.matches()) { return ServerOkFrame.OK_MESSAGE; } matcher = ERR_PATTERN.matcher(command); if (matcher.matches()) { return new ServerErrorFrame(matcher.group(1)); } if (PING_PATTERN.matcher(command).matches()) { return ServerPingFrame.PING; } if (PONG_PATTERN.matcher(command).matches()) { return ServerPongFrame.PONG; } throw new NatsDecodingException(command); }
From source file:net.malisis.switches.tileentity.SwitchTileEntity.java
License:Open Source License
@Override public void writeToNBT(NBTTagCompound tag) { super.writeToNBT(tag); ByteBuf bytes = Unpooled.buffer(linkedPos.size() * 8); for (BlockPos pos : linkedPos) bytes.writeLong(pos.toLong());//ww w. j av a 2s .c o m tag.setByteArray("linkedPos", bytes.array()); }