Java Code Examples for io.netty.buffer.ByteBuf#isReadable()
The following examples show how to use
io.netty.buffer.ByteBuf#isReadable() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TsiHandshakeHandler.java From grpc-java with Apache License 2.0 | 6 votes |
/** Sends as many bytes as are available from the handshaker to the remote peer. */ @SuppressWarnings("FutureReturnValueIgnored") // for addListener private void sendHandshake(ChannelHandlerContext ctx) throws GeneralSecurityException { while (true) { boolean written = false; ByteBuf buf = ctx.alloc().buffer(HANDSHAKE_FRAME_SIZE).retain(); // refcnt = 2 try { handshaker.getBytesToSendToPeer(buf); if (buf.isReadable()) { ctx.writeAndFlush(buf).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); written = true; } else { break; } } catch (GeneralSecurityException e) { throw new GeneralSecurityException("TsiHandshakeHandler encountered exception", e); } finally { buf.release(written ? 1 : 2); } } }
Example 2
Source File: RouteTargetConstrainNlriHandler.java From bgpcep with Eclipse Public License 1.0 | 6 votes |
private static List<RouteTargetConstrainDestination> parseNlriDestinations(final ByteBuf nlri, final boolean addPathSupported) { final List<RouteTargetConstrainDestination> dests = new ArrayList<>(); while (nlri.isReadable()) { final RouteTargetConstrainDestinationBuilder builder = new RouteTargetConstrainDestinationBuilder(); if (addPathSupported) { builder.setPathId(PathIdUtil.readPathId(nlri)); } final int length = nlri.readUnsignedByte() / 8; final ByteBuf nlriBuf = nlri.readSlice(length); Integer type = null; if (length != 0) { builder.setOriginAs(new AsNumber(ByteBufUtils.readUint32(nlriBuf))); type = (int) nlriBuf.readUnsignedByte(); //Skip Subtype nlriBuf.skipBytes(1); } builder.setRouteTargetConstrainChoice(SimpleRouteTargetConstrainNlriRegistry.getInstance() .parseRouteTargetConstrain(type, nlriBuf)); dests.add(builder.build()); } return dests; }
Example 3
Source File: LegacyAbstractFromClientPacketDecoder.java From ProtocolSupportBungee with GNU Affero General Public License v3.0 | 6 votes |
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> packets) throws Exception { if (!buf.isReadable()) { return; } buffer.writeBytes(buf); buffer.markReaderIndex(); try { while (buffer.isReadable()) { buffer.markReaderIndex(); ReadableMiddlePacket transformer = registry.getTransformer(protocol, buffer.readUnsignedByte(), true); transformer.read(buffer); packets.addAll(transformer.toNative()); buffer.discardReadBytes(); } } catch (EOFSignal e) { buffer.resetReaderIndex(); } }
Example 4
Source File: SingletonBlob.java From r2dbc-mysql with Apache License 2.0 | 5 votes |
@Override protected ByteBuffer convert(ByteBuf buf) { if (!buf.isReadable()) { return ByteBuffer.wrap(EMPTY_BYTES); } // Maybe allocateDirect? ByteBuffer result = ByteBuffer.allocate(buf.readableBytes()); buf.readBytes(result); result.flip(); return result; }
Example 5
Source File: MessageDecoder.java From codes-scratch-zookeeper-netty with Apache License 2.0 | 5 votes |
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // 防止不发报文就关闭连接出现的错误 if (!in.isReadable()) { return; } LOGGER.info(String.format("[%s]收到的的报文:[%s]", ctx.channel().localAddress().toString(), ByteBufUtil.hexDump(in))); byte[] ss = new byte[in.readableBytes()]; in.readBytes(ss); out.add(in); }
Example 6
Source File: AbstractByteBufDecoder.java From teku with Apache License 2.0 | 5 votes |
@Override public Optional<TMessage> decodeOneMessage(ByteBuf in) throws TException { if (!in.isReadable()) { return Optional.empty(); } compositeByteBuf.addComponent(true, in.retainedSlice()); try { Optional<TMessage> outBuf; while (true) { int readerIndex = compositeByteBuf.readerIndex(); outBuf = decodeOneImpl(compositeByteBuf); if (outBuf.isPresent() || readerIndex == compositeByteBuf.readerIndex() || compositeByteBuf.readableBytes() == 0) { break; } } if (outBuf.isPresent()) { in.skipBytes(in.readableBytes() - compositeByteBuf.readableBytes()); compositeByteBuf.release(); compositeByteBuf = Unpooled.compositeBuffer(); } else { in.skipBytes(in.readableBytes()); } return outBuf; } catch (Throwable t) { compositeByteBuf.release(); compositeByteBuf = Unpooled.compositeBuffer(); throw t; } }
Example 7
Source File: ChunkedWriteHandlerTest.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private static void check(ChunkedInput<?>... inputs) { EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler()); for (ChunkedInput<?> input: inputs) { ch.writeOutbound(input); } assertTrue(ch.finish()); int i = 0; int read = 0; for (;;) { ByteBuf buffer = (ByteBuf) ch.readOutbound(); if (buffer == null) { break; } while (buffer.isReadable()) { assertEquals(BYTES[i++], buffer.readByte()); read++; if (i == BYTES.length) { i = 0; } } buffer.release(); } assertEquals(BYTES.length * inputs.length, read); }
Example 8
Source File: AmqpProvider.java From qpid-jms with Apache License 2.0 | 5 votes |
@Override public void onData(final ByteBuf input) { try { if (isTraceBytes()) { TRACE_BYTES.info("Received: {}", ByteBufUtil.hexDump(input)); } if(protonTransportErrorHandled) { LOG.trace("Skipping data processing, proton transport previously errored."); return; } do { ByteBuffer buffer = protonTransport.tail(); int chunkSize = Math.min(buffer.remaining(), input.readableBytes()); buffer.limit(buffer.position() + chunkSize); input.readBytes(buffer); protonTransport.process(); } while (input.isReadable()); // Process the state changes from the latest data and then answer back // any pending updates to the Broker. processUpdates(); pumpToProtonTransport(); } catch (Throwable t) { LOG.warn("Caught problem during data processing: {}", t.getMessage(), t); fireProviderException(ProviderExceptionSupport.createOrPassthroughFatal(t)); } }
Example 9
Source File: MultiPartParserDefinition.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public void data(final ByteBuf buffer) throws IOException { this.currentFileSize += buffer.readableBytes(); if (this.maxIndividualFileSize > 0 && this.currentFileSize > this.maxIndividualFileSize) { throw UndertowMessages.MESSAGES.maxFileSizeExceeded(this.maxIndividualFileSize); } if (file == null && fileName != null && fileSizeThreshold < this.currentFileSize) { try { if (tempFileLocation != null) { file = Files.createTempFile(tempFileLocation, "undertow", "upload"); } else { file = Files.createTempFile("undertow", "upload"); } createdFiles.add(file); FileOutputStream fileOutputStream = new FileOutputStream(file.toFile()); contentBytes.writeTo(fileOutputStream); fileChannel = fileOutputStream.getChannel(); } catch (IOException e) { throw new RuntimeException(e); } } if (file == null) { while (buffer.isReadable()) { contentBytes.write(0xFF & buffer.readByte()); } } else { fileChannel.write(buffer.nioBuffer()); } }
Example 10
Source File: ByteBufTestUtils.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
/** Fragment byte buffer into multiple pieces. */ public static List<ByteBuf> fragmentByteBuf(ByteBuf in, int num, RegisterRef ref) { ByteBuf buf = in.slice(); Preconditions.checkArgument(num > 0); List<ByteBuf> fragmentedBufs = new ArrayList<>(num); int fragmentSize = buf.readableBytes() / num; while (buf.isReadable()) { int readBytes = num == 0 ? buf.readableBytes() : fragmentSize; ByteBuf tmpBuf = getDirectBuffer(readBytes, ref); tmpBuf.writeBytes(buf, readBytes); fragmentedBufs.add(tmpBuf); num--; } return fragmentedBufs; }
Example 11
Source File: PostOffice.java From cassandana with Apache License 2.0 | 5 votes |
void receivedPublishQos1(MQTTConnection connection, Topic topic, String username, ByteBuf payload, int messageID, boolean retain, MqttPublishMessage msg) { // verify if topic can be write topic.getTokens(); if (!topic.isValid()) { LOG.warn("Invalid topic format, force close the connection"); connection.dropConnection(); return; } final String clientId = connection.getClientId(); if (!authorizator.canWrite(topic, username, clientId)) { LOG.error("MQTT client: {} is not authorized to publish on topic: {}", clientId, topic); return; } if(silo != null) silo.put(topic, username, payload, AT_LEAST_ONCE); publish2Subscribers(payload, topic, AT_LEAST_ONCE); connection.sendPubAck(messageID); if (retain) { if (!payload.isReadable()) { retainedRepository.cleanRetained(topic); } else { // before wasn't stored retainedRepository.retain(topic, msg); } } interceptor.notifyTopicPublished(msg, clientId, username); }
Example 12
Source File: PCEPSvecObjectParser.java From bgpcep with Eclipse Public License 1.0 | 5 votes |
@Override public Svec parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (bytes.readableBytes() < MIN_SIZE) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: >=" + MIN_SIZE + "."); } bytes.skipBytes(FLAGS_F_OFFSET); final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE); final List<RequestId> requestIDs = new ArrayList<>(); while (bytes.isReadable()) { requestIDs.add(new RequestId(ByteBufUtils.readUint32(bytes))); } if (requestIDs.isEmpty()) { throw new PCEPDeserializerException("Empty Svec Object - no request ids."); } return new SvecBuilder() .setIgnore(header.isIgnore()) .setProcessingRule(header.isProcessingRule()) .setLinkDiverse(flags.get(L_FLAG_OFFSET)) .setNodeDiverse(flags.get(N_FLAG_OFFSET)) .setSrlgDiverse(flags.get(S_FLAG_OFFSET)) .setLinkDirectionDiverse(flags.get(D_FLAG_OFFSET)) .setPartialPathDiverse(flags.get(P_FLAG_OFFSET)) .setRequestsIds(requestIDs) .build(); }
Example 13
Source File: PostOffice.java From cassandana with Apache License 2.0 | 5 votes |
/** * First phase of a publish QoS2 protocol, sent by publisher to the broker. Publish to all interested * subscribers. */ void receivedPublishQos2(MQTTConnection connection, MqttPublishMessage mqttPublishMessage, String username) { LOG.trace("Processing PUBREL message on connection: {}", connection); final Topic topic = new Topic(mqttPublishMessage.variableHeader().topicName()); final ByteBuf payload = mqttPublishMessage.payload(); final String clientId = connection.getClientId(); if (!authorizator.canWrite(topic, username, clientId)) { LOG.error("MQTT client is not authorized to publish on topic. CId={}, topic: {}", clientId, topic); return; } if(silo != null) silo.put(topic, username, payload, EXACTLY_ONCE); publish2Subscribers(payload, topic, EXACTLY_ONCE); final boolean retained = mqttPublishMessage.fixedHeader().isRetain(); if (retained) { if (!payload.isReadable()) { retainedRepository.cleanRetained(topic); } else { // before wasn't stored retainedRepository.retain(topic, mqttPublishMessage); } } String clientID = connection.getClientId(); interceptor.notifyTopicPublished(mqttPublishMessage, clientID, username); }
Example 14
Source File: RouteTargetConstrainNlriHandler.java From bgpcep with Eclipse Public License 1.0 | 5 votes |
@Override public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder, final PeerSpecificParserConstraint constraint) throws BGPParsingException { if (!nlri.isReadable()) { return; } final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi())); builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder() .setDestinationType(new DestinationRouteTargetConstrainAdvertizedCaseBuilder() .setDestinationRouteTargetConstrain(new DestinationRouteTargetConstrainBuilder() .setRouteTargetConstrainDestination(parseNlriDestinations(nlri, mPathSupported)) .build()).build()).build()); }
Example 15
Source File: Bzip2Encoder.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { if (finished) { out.writeBytes(in); return; } for (;;) { switch (currentState) { case INIT: out.ensureWritable(4); out.writeMedium(MAGIC_NUMBER); out.writeByte('0' + streamBlockSize / BASE_BLOCK_SIZE); currentState = State.INIT_BLOCK; // fall through case INIT_BLOCK: blockCompressor = new Bzip2BlockCompressor(writer, streamBlockSize); currentState = State.WRITE_DATA; // fall through case WRITE_DATA: if (!in.isReadable()) { return; } Bzip2BlockCompressor blockCompressor = this.blockCompressor; final int length = Math.min(in.readableBytes(), blockCompressor.availableSize()); final int bytesWritten = blockCompressor.write(in, in.readerIndex(), length); in.skipBytes(bytesWritten); if (!blockCompressor.isFull()) { if (in.isReadable()) { break; } else { return; } } currentState = State.CLOSE_BLOCK; // fall through case CLOSE_BLOCK: closeBlock(out); currentState = State.INIT_BLOCK; break; default: throw new IllegalStateException(); } } }
Example 16
Source File: Snappy.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
public void decode(ByteBuf in, ByteBuf out) { while (in.isReadable()) { switch (state) { case READY: state = State.READING_PREAMBLE; // fall through case READING_PREAMBLE: int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { // We've not yet read all of the preamble, so wait until we can return; } if (uncompressedLength == 0) { // Should never happen, but it does mean we have nothing further to do state = State.READY; return; } out.ensureWritable(uncompressedLength); state = State.READING_TAG; // fall through case READING_TAG: if (!in.isReadable()) { return; } tag = in.readByte(); switch (tag & 0x03) { case LITERAL: state = State.READING_LITERAL; break; case COPY_1_BYTE_OFFSET: case COPY_2_BYTE_OFFSET: case COPY_4_BYTE_OFFSET: state = State.READING_COPY; break; } break; case READING_LITERAL: int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += literalWritten; } else { // Need to wait for more data return; } break; case READING_COPY: int decodeWritten; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { // Need to wait for more data return; } break; case COPY_2_BYTE_OFFSET: decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { // Need to wait for more data return; } break; case COPY_4_BYTE_OFFSET: decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { // Need to wait for more data return; } break; } } } }
Example 17
Source File: EtcdResponseHandler.java From etcd4j with Apache License 2.0 | 4 votes |
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) throws Exception { final HttpResponseStatus status =response.status(); final HttpHeaders headers = response.headers(); final ByteBuf content = response.content(); if (logger.isDebugEnabled()) { logger.debug("Received {} for {} {}", status.code(), this.request.getMethod().name(), this.request.getUri()); } if (status.equals(HttpResponseStatus.MOVED_PERMANENTLY) || status.equals(HttpResponseStatus.TEMPORARY_REDIRECT)) { if (headers.contains(HttpHeaderNames.LOCATION)) { this.request.setUrl(headers.get(HttpHeaderNames.LOCATION)); this.client.connect(this.request); // Closing the connection which handled the previous request. ctx.close(); if (logger.isDebugEnabled()) { logger.debug("redirect for {} to {}", this.request.getHttpRequest().uri() , headers.get(HttpHeaderNames.LOCATION) ); } } else { this.promise.setFailure(new Exception("Missing Location header on redirect")); } } else { EtcdResponseDecoder<? extends Throwable> failureDecoder = failureDecoders.get(status); if(failureDecoder != null) { this.promise.setFailure(failureDecoder.decode(headers, content)); } else if (!content.isReadable()) { // If connection was accepted maybe response has to be waited for if (!status.equals(HttpResponseStatus.OK) && !status.equals(HttpResponseStatus.ACCEPTED) && !status.equals(HttpResponseStatus.CREATED)) { this.promise.setFailure(new IOException( "Content was not readable. HTTP Status: " + status)); } } else { try { this.promise.setSuccess( request.getResponseDecoder().decode(headers, content)); } catch (Exception e) { if (e instanceof EtcdException) { this.promise.setFailure(e); } else { try { // Try to be smart, if an exception is thrown, first try to decode // the content and see if it is an EtcdException, i.e. an error code // not included in failureDecoders this.promise.setFailure(EtcdException.DECODER.decode(headers, content)); } catch (Exception e1) { // if it fails again, set the original exception as failure this.promise.setFailure(e); } } } } } }
Example 18
Source File: DnsTextEndpointGroup.java From armeria with Apache License 2.0 | 4 votes |
@Override ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception { final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder(); for (DnsRecord r : records) { if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.TXT) { continue; } final ByteBuf content = ((ByteBufHolder) r).content(); if (!content.isReadable()) { // Missing length octet warnInvalidRecord(DnsRecordType.TXT, content); continue; } content.markReaderIndex(); final int txtLen = content.readUnsignedByte(); if (txtLen == 0) { // Empty content continue; } if (content.readableBytes() != txtLen) { // Mismatching number of octets content.resetReaderIndex(); warnInvalidRecord(DnsRecordType.TXT, content); continue; } final byte[] txt = new byte[txtLen]; content.readBytes(txt); final Endpoint endpoint; try { endpoint = mapping.apply(txt); } catch (Exception e) { content.resetReaderIndex(); warnInvalidRecord(DnsRecordType.TXT, content); continue; } if (endpoint != null) { builder.add(endpoint); } } final ImmutableSortedSet<Endpoint> endpoints = builder.build(); if (logger().isDebugEnabled()) { logger().debug("{} Resolved: {} (TTL: {})", logPrefix(), endpoints.stream().map(Object::toString).collect(Collectors.joining(", ")), ttl); } return endpoints; }
Example 19
Source File: SslHandler.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
/** * Unwraps inbound SSL records. */ private void unwrap( ChannelHandlerContext ctx, ByteBuf packet, int offset, int length) throws SSLException { boolean wrapLater = false; boolean notifyClosure = false; ByteBuf decodeOut = allocate(ctx, length); try { for (;;) { final SSLEngineResult result = unwrap(engine, packet, offset, length, decodeOut); final Status status = result.getStatus(); final HandshakeStatus handshakeStatus = result.getHandshakeStatus(); final int produced = result.bytesProduced(); final int consumed = result.bytesConsumed(); // Update indexes for the next iteration offset += consumed; length -= consumed; if (status == Status.CLOSED) { // notify about the CLOSED state of the SSLEngine. See #137 notifyClosure = true; } switch (handshakeStatus) { case NEED_UNWRAP: break; case NEED_WRAP: wrapNonAppData(ctx, true); break; case NEED_TASK: runDelegatedTasks(); break; case FINISHED: setHandshakeSuccess(); wrapLater = true; continue; case NOT_HANDSHAKING: if (setHandshakeSuccessIfStillHandshaking()) { wrapLater = true; continue; } if (flushedBeforeHandshake) { // We need to call wrap(...) in case there was a flush done before the handshake completed. // // See https://github.com/netty/netty/pull/2437 flushedBeforeHandshake = false; wrapLater = true; } break; default: throw new IllegalStateException("unknown handshake status: " + handshakeStatus); } if (status == Status.BUFFER_UNDERFLOW || consumed == 0 && produced == 0) { break; } } if (wrapLater) { wrap(ctx, true); } if (notifyClosure) { sslCloseFuture.trySuccess(ctx.channel()); } } catch (SSLException e) { setHandshakeFailure(ctx, e); throw e; } finally { if (decodeOut.isReadable()) { ctx.fireChannelRead(decodeOut); } else { decodeOut.release(); } } }
Example 20
Source File: ClientToServerChannelHandler.java From datamill with ISC License | 4 votes |
@Override public void channelRead(ChannelHandlerContext context, Object message) { if (message instanceof HttpRequest) { HttpRequest request = (HttpRequest) message; if (HttpUtil.is100ContinueExpected(request)) { sendContinueResponse(context); } bodyStream = ReplaySubject.create(); serverRequest = ServerRequestBuilder.buildServerRequest(request, bodyStream); processRequest(context, request); if (request.decoderResult().isFailure()) { bodyStream.onError(request.decoderResult().cause()); } } if (message instanceof HttpContent) { HttpContent httpContent = (HttpContent) message; ByteBuf content = httpContent.content(); if (content.isReadable()) { bodyStream.onNext(content.nioBuffer()); if (httpContent.decoderResult().isFailure()) { bodyStream.onError(httpContent.decoderResult().cause()); } } if (message instanceof LastHttpContent) { LastHttpContent trailer = (LastHttpContent) message; if (!trailer.trailingHeaders().isEmpty()) { serverRequest.setTrailingHeaders(ServerRequestBuilder.buildHeadersMap(trailer.trailingHeaders())); } bodyStream.onCompleted(); } } }