Java Code Examples for io.netty.buffer.ByteBuf#retainedDuplicate()

The following examples show how to use io.netty.buffer.ByteBuf#retainedDuplicate() . 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: DiskAttribute.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public Attribute retainedDuplicate() {
    ByteBuf content = content();
    if (content != null) {
        content = content.retainedDuplicate();
        boolean success = false;
        try {
            Attribute duplicate = replace(content);
            success = true;
            return duplicate;
        } finally {
            if (!success) {
                content.release();
            }
        }
    } else {
        return replace(null);
    }
}
 
Example 2
Source File: DiskFileUpload.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public FileUpload retainedDuplicate() {
    ByteBuf content = content();
    if (content != null) {
        content = content.retainedDuplicate();
        boolean success = false;
        try {
            FileUpload duplicate = replace(content);
            success = true;
            return duplicate;
        } finally {
            if (!success) {
                content.release();
            }
        }
    } else {
        return replace(null);
    }
}
 
Example 3
Source File: MemoryFileUpload.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public FileUpload retainedDuplicate() {
    ByteBuf content = content();
    if (content != null) {
        content = content.retainedDuplicate();
        boolean success = false;
        try {
            FileUpload duplicate = replace(content);
            success = true;
            return duplicate;
        } finally {
            if (!success) {
                content.release();
            }
        }
    } else {
        return replace(null);
    }
}
 
Example 4
Source File: MemoryAttribute.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public Attribute retainedDuplicate() {
    ByteBuf content = content();
    if (content != null) {
        content = content.retainedDuplicate();
        boolean success = false;
        try {
            Attribute duplicate = replace(content);
            success = true;
            return duplicate;
        } finally {
            if (!success) {
                content.release();
            }
        }
    } else {
        return replace(null);
    }
}
 
Example 5
Source File: PendingWriteQueueTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void assertWrite(ChannelHandler handler, int count) {
    final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.US_ASCII);
    final EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.config().setWriteBufferLowWaterMark(1);
    channel.config().setWriteBufferHighWaterMark(3);

    ByteBuf[] buffers = new ByteBuf[count];
    for (int i = 0; i < buffers.length; i++) {
        buffers[i] = buffer.retainedDuplicate();
    }
    assertTrue(channel.writeOutbound(buffers));
    assertTrue(channel.finish());
    channel.closeFuture().syncUninterruptibly();

    for (int i = 0; i < buffers.length; i++) {
        assertBuffer(channel, buffer);
    }
    buffer.release();
    assertNull(channel.readOutbound());
}
 
Example 6
Source File: PendingWriteQueueTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void assertWriteFails(ChannelHandler handler, int count) {
    final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.US_ASCII);
    final EmbeddedChannel channel = new EmbeddedChannel(handler);
    ByteBuf[] buffers = new ByteBuf[count];
    for (int i = 0; i < buffers.length; i++) {
        buffers[i] = buffer.retainedDuplicate();
    }
    try {
        assertFalse(channel.writeOutbound(buffers));
        fail();
    } catch (Exception e) {
        assertTrue(e instanceof TestException);
    }
    assertFalse(channel.finish());
    channel.closeFuture().syncUninterruptibly();

    buffer.release();
    assertNull(channel.readOutbound());
}
 
Example 7
Source File: SimpleFrameInfoDecoder.java    From drift with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<FrameInfo> tryDecodeFrameInfo(ByteBuf buffer)
{
    TChannelBufferInputTransport transport = new TChannelBufferInputTransport(buffer.retainedDuplicate());
    try {
        TProtocol protocol = protocolType.createProtocol(transport);
        TMessage message;
        try {
            message = protocol.readMessageBegin();
        }
        catch (TException | RuntimeException e) {
            // not enough bytes in the input to decode sequence id
            return Optional.empty();
        }
        return Optional.of(new FrameInfo(
                message.getName(),
                message.getType(),
                message.getSequenceId(),
                transportType,
                protocolType,
                assumeClientsSupportOutOfOrderResponses));
    }
    finally {
        transport.release();
    }
}
 
Example 8
Source File: Session.java    From cassandana with Apache License 2.0 6 votes vote down vote up
public void resendInflightNotAcked() {
    Collection<InFlightPacket> expired = new ArrayList<>(INFLIGHT_WINDOW_SIZE);
    inflightTimeouts.drainTo(expired);

    debugLogPacketIds(expired);

    for (InFlightPacket notAckPacketId : expired) {
        if (inflightWindow.containsKey(notAckPacketId.packetId)) {
            final SessionRegistry.PublishedMessage msg =
                (SessionRegistry.PublishedMessage) inflightWindow.get(notAckPacketId.packetId);
            final Topic topic = msg.topic;
            final MqttQoS qos = msg.publishingQos;
            final ByteBuf payload = msg.payload;
            final ByteBuf copiedPayload = payload.retainedDuplicate();
            MqttPublishMessage publishMsg = publishNotRetainedDuplicated(notAckPacketId, topic, qos, copiedPayload);
            mqttConnection.sendPublish(publishMsg);
        }
    }
}
 
Example 9
Source File: MqttSession.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
public void resendInflightNotAcked() {
    Collection<InFlightPacket> expired = new ArrayList<>(INFLIGHT_WINDOW_SIZE);
    inflightTimeouts.drainTo(expired);

    debugLogPacketIds(expired);

    for (InFlightPacket notAckPacketId : expired) {
        if (inflightWindow.containsKey(notAckPacketId.packetId)) {
            final MqttSessionRegistry.PublishedMessage msg =
                (MqttSessionRegistry.PublishedMessage) inflightWindow.get(notAckPacketId.packetId);
            final Topic topic = msg.topic;
            final MqttQoS qos = msg.publishingQos;
            final ByteBuf payload = msg.payload;
            final ByteBuf copiedPayload = payload.retainedDuplicate();
            MqttPublishMessage publishMsg = publishNotRetainedDuplicated(notAckPacketId, topic, qos, copiedPayload);
            mqttConnection.sendPublish(publishMsg);
        }
    }
}
 
Example 10
Source File: DefaultFullBinaryMemcacheResponse.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheResponse replace(ByteBuf content) {
    ByteBuf key = key();
    if (key != null) {
        key = key.retainedDuplicate();
    }
    ByteBuf extras = extras();
    if (extras != null) {
        extras = extras.retainedDuplicate();
    }
    return new DefaultFullBinaryMemcacheResponse(key, extras, content);
}
 
Example 11
Source File: LengthLimitingContentPreviewer.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ByteBuf duplicateData(HttpData httpData, int length) {
    if (httpData instanceof ByteBufHolder) {
        final ByteBuf content = ((ByteBufHolder) httpData).content();
        if (content.readableBytes() == length) {
            return content.retainedDuplicate();
        }
        return content.retainedSlice(content.readerIndex(), length);
    } else {
        return Unpooled.wrappedBuffer(httpData.array(), 0, length);
    }
}
 
Example 12
Source File: HeaderTransport.java    From drift with Apache License 2.0 5 votes vote down vote up
public static Optional<FrameInfo> tryDecodeFrameInfo(ByteBuf input)
{
    ByteBuf buffer = input.retainedDuplicate();
    try {
        if (buffer.readableBytes() < FRAME_HEADER_SIZE) {
            return Optional.empty();
        }
        // skip magic
        buffer.readShort();
        short flags = buffer.readShort();
        boolean outOfOrderResponse = (flags & FLAG_SUPPORT_OUT_OF_ORDER_MASK) == 1;
        int headerSequenceId = buffer.readInt();
        int headerSize = buffer.readShort() << 2;

        if (buffer.readableBytes() < headerSize) {
            return Optional.empty();
        }

        byte protocolId = buffer.getByte(buffer.readerIndex());
        Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId);

        buffer.skipBytes(headerSize);
        SimpleFrameInfoDecoder simpleFrameInfoDecoder = new SimpleFrameInfoDecoder(HEADER, protocol, outOfOrderResponse);
        Optional<FrameInfo> frameInfo = simpleFrameInfoDecoder.tryDecodeFrameInfo(buffer);
        if (frameInfo.isPresent()) {
            int messageSequenceId = frameInfo.get().getSequenceId();
            checkArgument(
                    headerSequenceId == messageSequenceId,
                    "Sequence ids don't match. headerSequenceId: %s. messageSequenceId: %s",
                    headerSequenceId,
                    messageSequenceId);
        }
        return frameInfo;
    }
    finally {
        buffer.release();
    }
}
 
Example 13
Source File: Ukcp.java    From java-Kcp with Apache License 2.0 5 votes vote down vote up
/**
 * 发送无序不可靠消息
 * @param byteBuf  发送后需要手动释放
 */
public void writeUnorderedUnReliableMessage(ByteBuf byteBuf)
{
    User user   = (User) kcp.getUser();
    byteBuf = byteBuf.retainedDuplicate();
    //写入头信息
    ByteBuf head = PooledByteBufAllocator.DEFAULT.directBuffer(1);
    head.writeByte(UNORDERED_UNRELIABLE_PROTOCOL);
    ByteBuf content = Unpooled.wrappedBuffer(head, byteBuf);

    DatagramPacket temp = new DatagramPacket(content,user.getLocalAddress(), user.getRemoteAddress());
    user.getChannel().writeAndFlush(temp);
}
 
Example 14
Source File: Ukcp.java    From java-Kcp with Apache License 2.0 5 votes vote down vote up
/**
 * 发送有序可靠消息
 * 线程安全的
 * @param byteBuf 发送后需要手动释放
 * @return
 */
public boolean writeOrderedReliableMessage(ByteBuf byteBuf) {
    byteBuf = byteBuf.retainedDuplicate();
    if (!sendList.offer(byteBuf)) {
        log.error("conv "+kcp.getConv()+" sendList is full");
        byteBuf.release();
        notifyCloseEvent();
        return false;
    }
    notifyWriteEvent();
    return true;
}
 
Example 15
Source File: MqttPostOffice.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
private void publish2Subscribers(ByteBuf origPayload, Topic topic, MqttQoS publishingQos) {
    Set<Subscription> topicMatchingSubscriptions = subscriptions.matchQosSharpening(topic);

    for (final Subscription sub : topicMatchingSubscriptions) {
        MqttQoS qos = lowerQosToTheSubscriptionDesired(sub, publishingQos);
        MqttSession targetSession = this.sessionRegistry.retrieve(sub.getClientId());

        boolean isSessionPresent = targetSession != null;
        if (isSessionPresent) {
            LOG.debug("Sending PUBLISH message to active subscriber CId: {}, topicFilter: {}, qos: {}",
                      sub.getClientId(), sub.getTopicFilter(), qos);
            //TODO determine the user bounded to targetSession
            if (!authorizatorPolicy.canRead(topic, "TODO", sub.getClientId())) {
                LOG.debug("Authorizator prohibit Client {} to be notified on {}", sub.getClientId(), topic);
                return;
            }

            // we need to retain because duplicate only copy r/w indexes and don't retain() causing refCnt = 0
            ByteBuf payload = origPayload.retainedDuplicate();
            targetSession.sendPublishOnSessionAtQos(topic, qos, payload);
        } else {
            // If we are, the subscriber disconnected after the subscriptions tree selected that session as a
            // destination.
            LOG.debug("PUBLISH to not yet present session. CId: {}, topicFilter: {}, qos: {}", sub.getClientId(),
                      sub.getTopicFilter(), qos);
        }
    }
}
 
Example 16
Source File: DefaultFullBinaryMemcacheRequest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheRequest replace(ByteBuf content) {
    ByteBuf key = key();
    if (key != null) {
        key = key.retainedDuplicate();
    }
    ByteBuf extras = extras();
    if (extras != null) {
        extras = extras.retainedDuplicate();
    }
    return new DefaultFullBinaryMemcacheRequest(key, extras, content);
}
 
Example 17
Source File: CryptoServiceTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the given {@link ByteBuf} to a {@link CompositeByteBuf} if the {@code isCompositeByteBuf} is true.
 * @param buf The given {@link ByteBuf}.
 * @return The result {@link ByteBuf}.
 */
private ByteBuf maybeConvertToComposite(ByteBuf buf) {
  if (!isCompositeByteBuf) {
    return buf.retainedDuplicate();
  } else {
    byte[] data = new byte[buf.readableBytes()];
    buf.getBytes(buf.readerIndex(), data);
    return fromByteArrayToCompositeByteBuf(data);
  }
}
 
Example 18
Source File: PostOffice.java    From cassandana with Apache License 2.0 5 votes vote down vote up
private void publish2Subscribers(ByteBuf origPayload, Topic topic, MqttQoS publishingQos) {
    Set<Subscription> topicMatchingSubscriptions = subscriptions.matchQosSharpening(topic);

    for (final Subscription sub : topicMatchingSubscriptions) {
        MqttQoS qos = lowerQosToTheSubscriptionDesired(sub, publishingQos);
        Session targetSession = this.sessionRegistry.retrieve(sub.getClientId());

        boolean isSessionPresent = targetSession != null;
        if (isSessionPresent) {
            LOG.debug("Sending PUBLISH message to active subscriber CId: {}, topicFilter: {}, qos: {}",
                      sub.getClientId(), sub.getTopicFilter(), qos);
            if (!authorizator.canRead(topic, sub.getUsername(), sub.getClientId())) {
                LOG.debug("Authorizator prohibit Client {} to be notified on {}", sub.getClientId(), topic);
                return;
            }

            // we need to retain because duplicate only copy r/w indexes and don't retain() causing refCnt = 0
            ByteBuf payload = origPayload.retainedDuplicate();
            targetSession.sendPublishOnSessionAtQos(topic, qos, payload);
        } else {
            // If we are, the subscriber disconnected after the subscriptions tree selected that session as a
            // destination.
            LOG.debug("PUBLISH to not yet present session. CId: {}, topicFilter: {}, qos: {}", sub.getClientId(),
                      sub.getTopicFilter(), qos);
        }
    }
}
 
Example 19
Source File: FecEncode.java    From java-Kcp with Apache License 2.0 4 votes vote down vote up
/**
 *
 *  使用方法:
 *  1,入bytebuf后 把bytebuf发送出去,并释放bytebuf
 *  2,判断返回值是否为null,如果不为null发送出去并释放它
 *
 *  headerOffset +6字节fectHead +  2字节bodylenth(lenth-headerOffset-6)
 *
 * 1,对数据写入头标记为数据类型  markData
 * 2,写入消息长度
 * 3,获得缓存数据中最大长度,其他的缓存进行扩容到同样长度
 * 4,去掉头长度,进行fec编码
 * 5,对冗余字节数组进行标记为fec  makefec
 * 6,返回完整长度
 *
 *  注意: 传入的bytebuf如果需要释放在传入后手动释放。
 *  返回的bytebuf 也需要自己释放
 * @param byteBuf
 * @return
 */
public ByteBuf[] encode(ByteBuf byteBuf){
    markData(byteBuf,headerOffset);
    int sz = byteBuf.writerIndex();
    byteBuf.setShort(payloadOffset,sz-headerOffset- Fec.fecHeaderSizePlus2);
    this.shardCache[shardCount] = byteBuf.retainedDuplicate();
    this.shardCount ++;
    if (sz > this.maxSize) {
        this.maxSize = sz;
    }
    if(shardCount!=dataShards) {
        return null;
    }
    //填充parityShards
    for (int i = 0; i < parityShards; i++) {
        ByteBuf parityByte = ByteBufAllocator.DEFAULT.buffer(this.maxSize);
        shardCache[i+dataShards]  = parityByte;
        encodeCache[i] = parityByte;
        markParity(parityByte,headerOffset);
        parityByte.writerIndex(this.maxSize);
    }

    //按着最大长度不足补充0
    for (int i = 0; i < this.dataShards; i++) {
        ByteBuf shard = shardCache[i];
        int left = this.maxSize-shard.writerIndex();
        if(left<=0) {
            continue;
        }
        //是否需要扩容  会出现吗??
        //if(shard.capacity()<this.maxSize){
        //    ByteBuf newByteBuf = ByteBufAllocator.DEFAULT.buffer(this.maxSize);
        //    newByteBuf.writeBytes(shard);
        //    shard.release();
        //    shard = newByteBuf;
        //    shardCache[i] = shard;
        //}
        shard.writeBytes(zeros,left);
        zeros.readerIndex(0);
    }
    codec.encodeParity(shardCache,payloadOffset,this.maxSize-payloadOffset);
    //释放dataShards
    for (int i = 0; i < dataShards; i++) {
        this.shardCache[i].release();
        this.shardCache[i]=null;
    }
    this.shardCount = 0;
    this.maxSize = 0;
    return this.encodeCache;
}
 
Example 20
Source File: DefaultStreamMessageDuplicator.java    From armeria with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T retainedDuplicate(ByteBuf o) {
    return (T) o.retainedDuplicate();
}