Search in sources :

Example 91 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.

the class MqttEncoder method encodeSubscribeMessage.

private static ByteBuf encodeSubscribeMessage(ByteBufAllocator byteBufAllocator, MqttSubscribeMessage message) {
    int variableHeaderBufferSize = 2;
    int payloadBufferSize = 0;
    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    MqttMessageIdVariableHeader variableHeader = message.variableHeader();
    MqttSubscribePayload payload = message.payload();
    for (MqttTopicSubscription topic : payload.topicSubscriptions()) {
        String topicName = topic.topicName();
        byte[] topicNameBytes = encodeStringUtf8(topicName);
        payloadBufferSize += 2 + topicNameBytes.length;
        payloadBufferSize += 1;
    }
    int variablePartSize = variableHeaderBufferSize + payloadBufferSize;
    int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize);
    ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variablePartSize);
    buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
    writeVariableLengthInt(buf, variablePartSize);
    // Variable Header
    int messageId = variableHeader.messageId();
    buf.writeShort(messageId);
    // Payload
    for (MqttTopicSubscription topic : payload.topicSubscriptions()) {
        String topicName = topic.topicName();
        byte[] topicNameBytes = encodeStringUtf8(topicName);
        buf.writeShort(topicNameBytes.length);
        buf.writeBytes(topicNameBytes, 0, topicNameBytes.length);
        buf.writeByte(topic.qualityOfService().value());
    }
    return buf;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 92 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.

the class MqttCodecTest method testMessageWithOnlyFixedHeader.

private void testMessageWithOnlyFixedHeader(MqttMessageType messageType) throws Exception {
    MqttMessage message = createMessageWithFixedHeader(messageType);
    ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
    final List<Object> out = new LinkedList<Object>();
    mqttDecoder.decode(ctx, byteBuf, out);
    assertEquals("Expected one object but got " + out.size(), 1, out.size());
    final MqttMessage decodedMessage = (MqttMessage) out.get(0);
    validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) LinkedList(java.util.LinkedList)

Example 93 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.

the class ChannelOutboundBuffer method nioBuffers.

/**
     * Returns an array of direct NIO buffers if the currently pending messages are made of {@link ByteBuf} only.
     * {@link #nioBufferCount()} and {@link #nioBufferSize()} will return the number of NIO buffers in the returned
     * array and the total number of readable bytes of the NIO buffers respectively.
     * <p>
     * Note that the returned array is reused and thus should not escape
     * {@link AbstractChannel#doWrite(ChannelOutboundBuffer)}.
     * Refer to {@link NioSocketChannel#doWrite(ChannelOutboundBuffer)} for an example.
     * </p>
     */
public ByteBuffer[] nioBuffers() {
    long nioBufferSize = 0;
    int nioBufferCount = 0;
    final InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
    ByteBuffer[] nioBuffers = NIO_BUFFERS.get(threadLocalMap);
    Entry entry = flushedEntry;
    while (isFlushedEntry(entry) && entry.msg instanceof ByteBuf) {
        if (!entry.cancelled) {
            ByteBuf buf = (ByteBuf) entry.msg;
            final int readerIndex = buf.readerIndex();
            final int readableBytes = buf.writerIndex() - readerIndex;
            if (readableBytes > 0) {
                if (Integer.MAX_VALUE - readableBytes < nioBufferSize) {
                    // - http://linux.die.net/man/2/writev
                    break;
                }
                nioBufferSize += readableBytes;
                int count = entry.count;
                if (count == -1) {
                    //noinspection ConstantValueVariableUse
                    entry.count = count = buf.nioBufferCount();
                }
                int neededSpace = nioBufferCount + count;
                if (neededSpace > nioBuffers.length) {
                    nioBuffers = expandNioBufferArray(nioBuffers, neededSpace, nioBufferCount);
                    NIO_BUFFERS.set(threadLocalMap, nioBuffers);
                }
                if (count == 1) {
                    ByteBuffer nioBuf = entry.buf;
                    if (nioBuf == null) {
                        // cache ByteBuffer as it may need to create a new ByteBuffer instance if its a
                        // derived buffer
                        entry.buf = nioBuf = buf.internalNioBuffer(readerIndex, readableBytes);
                    }
                    nioBuffers[nioBufferCount++] = nioBuf;
                } else {
                    ByteBuffer[] nioBufs = entry.bufs;
                    if (nioBufs == null) {
                        // cached ByteBuffers as they may be expensive to create in terms
                        // of Object allocation
                        entry.bufs = nioBufs = buf.nioBuffers();
                    }
                    nioBufferCount = fillBufferArray(nioBufs, nioBuffers, nioBufferCount);
                }
            }
        }
        entry = entry.next;
    }
    this.nioBufferCount = nioBufferCount;
    this.nioBufferSize = nioBufferSize;
    return nioBuffers;
}
Also used : InternalThreadLocalMap(io.netty.util.internal.InternalThreadLocalMap) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 94 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.

the class CoalescingBufferQueue method remove.

/**
     * Remove a {@link ByteBuf} from the queue with the specified number of bytes. Any added buffer who's bytes are
     * fully consumed during removal will have it's promise completed when the passed aggregate {@link ChannelPromise}
     * completes.
     *
     * @param bytes the maximum number of readable bytes in the returned {@link ByteBuf}, if {@code bytes} is greater
     *              than {@link #readableBytes} then a buffer of length {@link #readableBytes} is returned.
     * @param aggregatePromise used to aggregate the promises and listeners for the constituent buffers.
     * @return a {@link ByteBuf} composed of the enqueued buffers.
     */
public ByteBuf remove(int bytes, ChannelPromise aggregatePromise) {
    if (bytes < 0) {
        throw new IllegalArgumentException("bytes (expected >= 0): " + bytes);
    }
    ObjectUtil.checkNotNull(aggregatePromise, "aggregatePromise");
    // Use isEmpty rather than readableBytes==0 as we may have a promise associated with an empty buffer.
    if (bufAndListenerPairs.isEmpty()) {
        return Unpooled.EMPTY_BUFFER;
    }
    bytes = Math.min(bytes, readableBytes);
    ByteBuf toReturn = null;
    int originalBytes = bytes;
    for (; ; ) {
        Object entry = bufAndListenerPairs.poll();
        if (entry == null) {
            break;
        }
        if (entry instanceof ChannelFutureListener) {
            aggregatePromise.addListener((ChannelFutureListener) entry);
            continue;
        }
        ByteBuf entryBuffer = (ByteBuf) entry;
        if (entryBuffer.readableBytes() > bytes) {
            // Add the buffer back to the queue as we can't consume all of it.
            bufAndListenerPairs.addFirst(entryBuffer);
            if (bytes > 0) {
                // Take a slice of what we can consume and retain it.
                toReturn = compose(toReturn, entryBuffer.readRetainedSlice(bytes));
                bytes = 0;
            }
            break;
        } else {
            toReturn = compose(toReturn, entryBuffer);
            bytes -= entryBuffer.readableBytes();
        }
    }
    readableBytes -= originalBytes - bytes;
    assert readableBytes >= 0;
    return toReturn;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 95 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project netty by netty.

the class HttpPostMultipartRequestDecoder method readLine.

/**
     * Read one line up to the CRLF or LF
     *
     * @return the String from one line
     * @throws NotEnoughDataDecoderException
     *             Need more chunks and reset the readerInder to the previous
     *             value
     */
private String readLine() {
    SeekAheadOptimize sao;
    try {
        sao = new SeekAheadOptimize(undecodedChunk);
    } catch (SeekAheadNoBackArrayException ignored) {
        return readLineStandard();
    }
    int readerIndex = undecodedChunk.readerIndex();
    try {
        ByteBuf line = buffer(64);
        while (sao.pos < sao.limit) {
            byte nextByte = sao.bytes[sao.pos++];
            if (nextByte == HttpConstants.CR) {
                if (sao.pos < sao.limit) {
                    nextByte = sao.bytes[sao.pos++];
                    if (nextByte == HttpConstants.LF) {
                        sao.setReadPosition(0);
                        return line.toString(charset);
                    } else {
                        // Write CR (not followed by LF)
                        sao.pos--;
                        line.writeByte(HttpConstants.CR);
                    }
                } else {
                    line.writeByte(nextByte);
                }
            } else if (nextByte == HttpConstants.LF) {
                sao.setReadPosition(0);
                return line.toString(charset);
            } else {
                line.writeByte(nextByte);
            }
        }
    } catch (IndexOutOfBoundsException e) {
        undecodedChunk.readerIndex(readerIndex);
        throw new NotEnoughDataDecoderException(e);
    }
    undecodedChunk.readerIndex(readerIndex);
    throw new NotEnoughDataDecoderException();
}
Also used : SeekAheadOptimize(io.netty.handler.codec.http.multipart.HttpPostBodyUtil.SeekAheadOptimize) SeekAheadNoBackArrayException(io.netty.handler.codec.http.multipart.HttpPostBodyUtil.SeekAheadNoBackArrayException) NotEnoughDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.NotEnoughDataDecoderException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)5080 Test (org.junit.Test)1813 Test (org.junit.jupiter.api.Test)680 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)377 ArrayList (java.util.ArrayList)301 IOException (java.io.IOException)297 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)200 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)182 ByteBuffer (java.nio.ByteBuffer)167 InetSocketAddress (java.net.InetSocketAddress)145 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)144 Test (org.testng.annotations.Test)140 Channel (io.netty.channel.Channel)137 List (java.util.List)134 ChannelFuture (io.netty.channel.ChannelFuture)128 Map (java.util.Map)118 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)107 Position (org.traccar.model.Position)105 DeviceSession (org.traccar.DeviceSession)100 NetworkMessage (org.traccar.NetworkMessage)93