List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
From source file:com.torchmind.netty.msgpack.codec.MessageFrameCodec.java
License:Apache License
/** * {@inheritDoc}//from w w w .j a va 2s . co m */ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { while (in.readableBytes() >= 4) { // mark reader index in.markReaderIndex(); // read length int length = in.readInt(); // check whether enough data is available if (length > in.readableBytes()) { // reset index in.resetReaderIndex(); // skip further execution due to missing data break; } // read buffer ByteBuf buffer = ctx.alloc().buffer(length); in.readBytes(buffer); // append to output out.add(buffer); } }
From source file:com.trinity.engine.protocol.detail.ConnectionReplayingDecoder.java
License:Apache License
/** * {@inheritDoc}//from ww w. j a v a 2 s . com */ @Override protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) throws Exception { switch (state()) { case READ_ID: mMessageId = input.readUnsignedByte(); checkpoint(DecoderState.READ_LENGTH); break; case READ_LENGTH: mMessageLength = input.readUnsignedShort(); checkpoint(DecoderState.READ_CONTENT); break; case READ_CONTENT: output.add(mLookupService.decode(mMessageId, input.readBytes(mMessageLength))); checkpoint(DecoderState.READ_ID); } }
From source file:com.turn.ttorrent.client.io.PeerHandshakeMessage.java
License:Apache License
@Override public void fromWire(ByteBuf in) { int pstrlen = in.readUnsignedByte(); if (pstrlen < 0 || in.readableBytes() < BASE_HANDSHAKE_LENGTH + pstrlen - 1) throw new IllegalArgumentException("Incorrect handshake message length (pstrlen=" + pstrlen + ") !"); // Check the protocol identification string protocolName = new byte[pstrlen]; in.readBytes(protocolName); if (!Arrays.equals(protocolName, BITTORRENT_PROTOCOL_IDENTIFIER)) throw new IllegalArgumentException("Unknown protocol " + new String(protocolName, Charsets.ISO_8859_1)); // Ignore reserved bytes in.readBytes(reserved);// w w w . ja v a 2s . c o m infoHash = new byte[20]; in.readBytes(infoHash); peerId = new byte[20]; in.readBytes(peerId); }
From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceRequestMessage.java
License:Apache License
@Override public void fromWire(ByteBuf in) throws MessageValidationException { _fromWire(in, UDP_ANNOUNCE_REQUEST_MESSAGE_SIZE); infoHash = new byte[20]; in.readBytes(infoHash); peerId = new byte[20]; in.readBytes(peerId);//from w ww . j av a2 s .c o m downloaded = in.readLong(); uploaded = in.readLong(); left = in.readLong(); event = AnnounceEvent.getById(in.readInt()); if (event == null) throw new MessageValidationException("Invalid event type in announce request!"); InetAddress address; try { byte[] ipBytes = new byte[4]; in.readBytes(ipBytes); address = InetAddress.getByAddress(ipBytes); } catch (UnknownHostException e) { throw new MessageValidationException("Invalid IP address in announce request!", e); } key = in.readInt(); numWant = in.readInt(); int port = in.readShort() & 0xFFFF; peerAddress = new InetSocketAddress(address, port); }
From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceResponseMessage.java
License:Apache License
@Override public void fromWire(ByteBuf in) throws MessageValidationException { if (in.readableBytes() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE || (in.readableBytes() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) { throw new MessageValidationException("Invalid announce response message size " + in.readableBytes()); }//from w ww . j a v a 2 s .com _fromWire(in, -1); interval = in.readInt(); incomplete = in.readInt(); complete = in.readInt(); peers.clear(); while (in.readableBytes() > 0) { try { byte[] ipBytes = new byte[4]; in.readBytes(ipBytes); InetAddress ip = InetAddress.getByAddress(ipBytes); int port = in.readShort() & 0xFFFF; peers.add(new Peer(new InetSocketAddress(ip, port), null)); } catch (UnknownHostException uhe) { throw new MessageValidationException("Invalid IP address in announce request!"); } } }
From source file:com.turn.ttorrent.common.protocol.udp.UDPTrackerErrorMessage.java
License:Apache License
@Override public void fromWire(ByteBuf in) throws MessageValidationException { if (in.readableBytes() < UDP_TRACKER_ERROR_MIN_MESSAGE_SIZE) throw new MessageValidationException("Invalid tracker error message size " + in.readableBytes()); _fromWire(in, -1);//from w w w . jav a2 s. c om byte[] reasonBytes = new byte[in.readableBytes()]; in.readBytes(reasonBytes); reason = new String(reasonBytes, Torrent.BYTE_ENCODING); }
From source file:com.turn.ttorrent.protocol.tracker.udp.UDPTrackerErrorMessage.java
License:Apache License
@Override public void fromWire(ByteBuf in) throws MessageValidationException { if (in.readableBytes() < UDP_TRACKER_ERROR_MIN_MESSAGE_SIZE) throw new MessageValidationException("Invalid tracker error message size " + in.readableBytes()); _fromWire(in, -1);// w w w . j a v a2 s . co m byte[] reasonBytes = new byte[in.readableBytes()]; in.readBytes(reasonBytes); reason = new String(reasonBytes, BEUtils.BYTE_ENCODING); }
From source file:com.turo.pushy.apns.auth.ApnsKey.java
License:Open Source License
protected static byte[] decodeBase64EncodedString(final String base64EncodedString) { final ByteBuf base64EncodedByteBuf = Unpooled .wrappedBuffer(base64EncodedString.getBytes(StandardCharsets.US_ASCII)); final ByteBuf decodedByteBuf = Base64.decode(base64EncodedByteBuf); final byte[] decodedBytes = new byte[decodedByteBuf.readableBytes()]; decodedByteBuf.readBytes(decodedBytes); base64EncodedByteBuf.release();//from ww w.j av a 2 s. co m decodedByteBuf.release(); return decodedBytes; }
From source file:com.turo.pushy.apns.auth.AuthenticationToken.java
License:Open Source License
static byte[] decodeBase64UrlEncodedString(final String base64UrlEncodedString) { final String paddedBase64UrlEncodedString; switch (base64UrlEncodedString.length() % 4) { case 2: {/*from ww w. j a va 2s.c o m*/ paddedBase64UrlEncodedString = base64UrlEncodedString + "=="; break; } case 3: { paddedBase64UrlEncodedString = base64UrlEncodedString + "="; break; } default: { paddedBase64UrlEncodedString = base64UrlEncodedString; } } final ByteBuf base64EncodedByteBuf = Unpooled .wrappedBuffer(paddedBase64UrlEncodedString.getBytes(StandardCharsets.US_ASCII)); final ByteBuf decodedByteBuf = Base64.decode(base64EncodedByteBuf, Base64Dialect.URL_SAFE); final byte[] decodedBytes = new byte[decodedByteBuf.readableBytes()]; decodedByteBuf.readBytes(decodedBytes); base64EncodedByteBuf.release(); decodedByteBuf.release(); return decodedBytes; }
From source file:com.twitter.http2.HttpConnectionHandler.java
License:Apache License
/** * {@inheritDoc}// ww w . j a v a 2s . c o m */ @Override public void readDataFrame(int streamId, boolean endStream, boolean endSegment, ByteBuf data) { // HTTP/2 DATA frame processing requirements: // // If an endpoint receives a data frame for a Stream-ID which is not open // and the endpoint has not sent a GOAWAY frame, it must issue a stream error // with the error code INVALID_STREAM for the Stream-ID. // // If an endpoint receives multiple data frames for invalid Stream-IDs, // it may close the connection. // // If an endpoint refuses a stream it must ignore any data frames for that stream. // // If an endpoint receives a data frame after the stream is half-closed (remote) // or closed, it must respond with a stream error of type STREAM_CLOSED. int deltaWindowSize = -1 * data.readableBytes(); int newConnectionWindowSize = httpConnection.updateReceiveWindowSize(HTTP_CONNECTION_STREAM_ID, deltaWindowSize); // Check if connection window size is reduced beyond allowable lower bound if (newConnectionWindowSize < 0) { issueConnectionError(HttpErrorCode.PROTOCOL_ERROR); return; } // Send a WINDOW_UPDATE frame if less than half the connection window size remains if (newConnectionWindowSize <= initialConnectionReceiveWindowSize / 2) { int windowSizeIncrement = initialConnectionReceiveWindowSize - newConnectionWindowSize; httpConnection.updateReceiveWindowSize(HTTP_CONNECTION_STREAM_ID, windowSizeIncrement); ByteBuf frame = httpFrameEncoder.encodeWindowUpdateFrame(HTTP_CONNECTION_STREAM_ID, windowSizeIncrement); context.writeAndFlush(frame); } // Check if we received a DATA frame for a stream which is half-closed (remote) or closed if (httpConnection.isRemoteSideClosed(streamId)) { if (streamId <= lastStreamId) { issueStreamError(streamId, HttpErrorCode.STREAM_CLOSED); } else if (!sentGoAwayFrame) { issueStreamError(streamId, HttpErrorCode.PROTOCOL_ERROR); } return; } // Update receive window size int newWindowSize = httpConnection.updateReceiveWindowSize(streamId, deltaWindowSize); // Window size can become negative if we sent a SETTINGS frame that reduces the // size of the transfer window after the peer has written data frames. // The value is bounded by the length that SETTINGS frame decrease the window. // This difference is stored for the connection when writing the SETTINGS frame // and is cleared once we send a WINDOW_UPDATE frame. if (newWindowSize < httpConnection.getReceiveWindowSizeLowerBound(streamId)) { issueStreamError(streamId, HttpErrorCode.FLOW_CONTROL_ERROR); return; } // Window size became negative due to sender writing frame before receiving SETTINGS // Send data frames upstream in initialReceiveWindowSize chunks if (newWindowSize < 0) { while (data.readableBytes() > initialReceiveWindowSize) { ByteBuf partialData = data.readBytes(initialReceiveWindowSize); HttpDataFrame partialDataFrame = new DefaultHttpDataFrame(streamId, partialData); context.fireChannelRead(partialDataFrame); } } // Send a WINDOW_UPDATE frame if less than half the stream window size remains // Recipient should not send a WINDOW_UPDATE frame as it consumes the last data frame. if (handleStreamWindowUpdates && newWindowSize <= initialReceiveWindowSize / 2 && !endStream) { int windowSizeIncrement = initialReceiveWindowSize - newWindowSize; httpConnection.updateReceiveWindowSize(streamId, windowSizeIncrement); ByteBuf frame = httpFrameEncoder.encodeWindowUpdateFrame(streamId, windowSizeIncrement); context.writeAndFlush(frame); } // Close the remote side of the stream if this is the last frame if (endStream) { halfCloseStream(streamId, true, context.channel().newSucceededFuture()); } HttpDataFrame httpDataFrame = new DefaultHttpDataFrame(streamId, data); httpDataFrame.setLast(endStream); context.fireChannelRead(httpDataFrame); }