List of usage examples for io.netty.buffer ByteBuf capacity
public abstract int capacity();
From source file:com.moshi.receptionist.remoting.netty.NettyDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { try {//from ww w . j a v a 2 s . c o m ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; } byte[] tmpBuf = new byte[frame.capacity()]; frame.getBytes(0, tmpBuf); frame.release(); return RemotingCommand.decode(tmpBuf); } catch (Exception e) { log.error("decode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); // ? pipelineclose??? RemotingUtil.closeChannel(ctx.channel()); } return null; }
From source file:com.ogarproject.ogar.server.net.PacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception { ByteBuf buf = frame.content().order(ByteOrder.LITTLE_ENDIAN); if (buf.capacity() < 1) { // Discard empty messages return;/*from w w w . ja v a 2 s .co m*/ } buf.resetReaderIndex(); int packetId = buf.readUnsignedByte(); Packet packet = PacketRegistry.SERVERBOUND.constructPacket(packetId); if (packet == null) { throw new UnknownPacketException("Unknown packet ID: " + packetId); } OgarServer.log.finest("Received packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") from " + ctx.channel().remoteAddress()); packet.readData(buf); out.add(packet); }
From source file:com.shouxun.server.netty.tcp.EchoServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf buffer = (ByteBuf) msg; for (int i = 0; i < buffer.capacity(); i++) { byte b = buffer.getByte(i); sb.append((char) b); }//from ww w .j a v a2 s. co m buffer.release(); }
From source file:com.spotify.netty.handler.codec.zmtp.ZMTPFrame.java
License:Apache License
/** * Create a new frame from a channel buffer. *//* www . j a v a 2 s . c om*/ public static ZMTPFrame create(ByteBuf buf) { if (!buf.isReadable()) { return EMPTY_FRAME; } else { byte[] dst = new byte[buf.capacity()]; buf.readBytes(dst); return new ZMTPFrame(dst); } }
From source file:com.spotify.netty.handler.queue.AutoFlushingWriteBatcher.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; int size = bufferSize.get() + buf.capacity(); if (size >= maxBufferSize) { ctx.writeAndFlush(buf);//from w ww . jav a 2s .co m } } else { ctx.write(msg, promise); } }
From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java
License:Apache License
private boolean verifySignature(int offset, int length, ByteBuf buf) throws OnRecordErrorException { boolean isVerified = false; if (securityLevel == SecurityLevel.NONE) { return true; }//from ww w. j a v a2 s. c o m if (length < 33) { LOG.warn("No username"); } else if (length < 32) { LOG.warn("invalid signature"); } byte[] signature = new byte[32]; buf.getBytes(offset, signature, 0, 32); int userLength = length - offset - 32; byte[] userBytes = new byte[userLength]; buf.getBytes(offset + 32, userBytes, 0, userLength); String username = new String(userBytes); if (!authKeys.containsKey(username)) { throw new OnRecordErrorException(Errors.COLLECTD_03, "Auth File doesn't contain requested user: " + username); } String key = authKeys.get(username); try { Mac sha256HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(charset), "HmacSHA256"); sha256HMAC.init(secretKey); int userPayloadLength = buf.capacity() - length + username.length(); byte[] userPayload = new byte[userPayloadLength]; buf.getBytes(offset + 32, userPayload, 0, userPayloadLength); isVerified = Arrays.equals(sha256HMAC.doFinal(userPayload), signature); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new OnRecordErrorException(Errors.COLLECTD_02, e.toString()); } return isVerified; }
From source file:com.streamsets.pipeline.lib.parser.net.DelimitedLengthFieldBasedFrameDecoder.java
License:Apache License
/** * Returns the number of bytes between the readerIndex of the haystack and * the first needle found in the haystack. -1 is returned if no needle is * found in the haystack./* ww w . j a va 2 s . c o m*/ */ private static int indexOf(ByteBuf haystack, ByteBuf needle) { for (int i = haystack.readerIndex(); i < haystack.writerIndex(); i++) { int haystackIndex = i; int needleIndex; for (needleIndex = 0; needleIndex < needle.capacity(); needleIndex++) { if (haystack.getByte(haystackIndex) != needle.getByte(needleIndex)) { break; } else { haystackIndex++; if (haystackIndex == haystack.writerIndex() && needleIndex != needle.capacity() - 1) { return -1; } } } if (needleIndex == needle.capacity()) { // Found the needle from the haystack! return i - haystack.readerIndex(); } } return -1; }
From source file:com.streamsets.pipeline.lib.parser.udp.collectd.CollectdParser.java
License:Apache License
private boolean verifySignature(int offset, int length, ByteBuf buf) throws OnRecordErrorException { boolean isVerified = false; if (securityLevel == SecurityLevel.NONE) { return true; }/*from w ww . j a va2 s. c om*/ if (length < 33) { LOG.warn("No username"); } if (length < 32) { LOG.warn("invalid signature"); } byte[] signature = new byte[32]; buf.getBytes(offset, signature, 0, 32); int userLength = length - offset - 32; byte[] userBytes = new byte[userLength]; buf.getBytes(offset + 32, userBytes, 0, userLength); String username = new String(userBytes, StandardCharsets.UTF_8); if (!authKeys.containsKey(username)) { throw new OnRecordErrorException(Errors.COLLECTD_03, "Auth File doesn't contain requested user: " + username); } String key = authKeys.get(username); try { Mac sha256HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(charset), "HmacSHA256"); sha256HMAC.init(secretKey); int userPayloadLength = buf.capacity() - length + username.length(); byte[] userPayload = new byte[userPayloadLength]; buf.getBytes(offset + 32, userPayload, 0, userPayloadLength); isVerified = Arrays.equals(sha256HMAC.doFinal(userPayload), signature); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new OnRecordErrorException(Errors.COLLECTD_02, e.toString()); } return isVerified; }
From source file:com.talent.balance.backend.decode.BackendResponseDecoder.java
License:Open Source License
@Override public PacketWithMeta<Packet> decode(ByteBuf buffer, ChannelContext channelContext) throws DecodeException { BalancePacket backendResponsePacket = new BalancePacket(); if (MysqlExt.getHandshakePacket(channelContext) == null && BackendExt.PROTOCOL_MYSQL.equals(channelContext.getProtocol())) { HandshakePacket handshakePacket = new HandshakePacket(); PacketWithMeta<Packet> pwm = handshakePacket.decode(buffer); return pwm; }//from w ww . jav a2 s. c o m backendResponsePacket.setBuffer(Unpooled.copiedBuffer(buffer)); List<Packet> packets = new ArrayList<Packet>(); packets.add(backendResponsePacket); PacketWithMeta<Packet> packetWithMeta = new PacketWithMeta<Packet>(); buffer.readerIndex(buffer.capacity()); packetWithMeta.setPacketLenght(buffer.capacity()); packetWithMeta.setPackets(packets); return packetWithMeta; }
From source file:com.talent.balance.common.ParseUtils.java
License:Open Source License
public static int processReadIndex(ByteBuf buffer) { int newReaderIndex = buffer.readerIndex(); if (newReaderIndex < (buffer.capacity())) { buffer.readerIndex(newReaderIndex + 1); return 1; }//from w ww. j av a2s .com return 0; }