Search in sources :

Example 26 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project flink by apache.

the class MessageSerializer method deserializeRequestFailure.

/**
 * De-serializes the {@link RequestFailure} sent to the {@link
 * org.apache.flink.queryablestate.network.Client} in case of protocol related errors.
 *
 * <pre>
 *  <b>The buffer is expected to be at the correct position.</b>
 * </pre>
 *
 * @param buf The {@link ByteBuf} containing the serialized failure message.
 * @return The failure message.
 */
public static RequestFailure deserializeRequestFailure(final ByteBuf buf) throws IOException, ClassNotFoundException {
    long requestId = buf.readLong();
    Throwable cause;
    try (ByteBufInputStream bis = new ByteBufInputStream(buf);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        cause = (Throwable) in.readObject();
    }
    return new RequestFailure(requestId, cause);
}
Also used : ByteBufInputStream(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 27 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty-socketio by mrniko.

the class PacketDecoder method parseBody.

private void parseBody(ClientHead head, ByteBuf frame, Packet packet) throws IOException {
    if (packet.getType() == PacketType.MESSAGE) {
        if (packet.getSubType() == PacketType.CONNECT || packet.getSubType() == PacketType.DISCONNECT) {
            packet.setNsp(readNamespace(frame));
        }
        if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) {
            packet.setDataSource(Unpooled.copiedBuffer(frame));
            frame.readerIndex(frame.readableBytes());
            head.setLastBinaryPacket(packet);
        }
        if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) {
            return;
        }
        if (packet.getSubType() == PacketType.ACK || packet.getSubType() == PacketType.BINARY_ACK) {
            ByteBufInputStream in = new ByteBufInputStream(frame);
            AckCallback<?> callback = ackManager.getCallback(head.getSessionId(), packet.getAckId());
            AckArgs args = jsonSupport.readAckArgs(in, callback);
            packet.setData(args.getArgs());
        }
        if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.BINARY_EVENT) {
            ByteBufInputStream in = new ByteBufInputStream(frame);
            Event event = jsonSupport.readValue(packet.getNsp(), in, Event.class);
            packet.setName(event.getName());
            packet.setData(event.getArgs());
        }
    }
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 28 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class ZlibTest method testGZIPCompressOnly0.

private void testGZIPCompressOnly0(byte[] data) throws IOException {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.GZIP));
    if (data != null) {
        chEncoder.writeOutbound(Unpooled.wrappedBuffer(data));
    }
    assertTrue(chEncoder.finish());
    ByteBuf encoded = Unpooled.buffer();
    for (; ; ) {
        ByteBuf buf = chEncoder.readOutbound();
        if (buf == null) {
            break;
        }
        encoded.writeBytes(buf);
        buf.release();
    }
    ByteBuf decoded = Unpooled.buffer();
    GZIPInputStream stream = new GZIPInputStream(new ByteBufInputStream(encoded, true));
    try {
        byte[] buf = new byte[8192];
        for (; ; ) {
            int readBytes = stream.read(buf);
            if (readBytes < 0) {
                break;
            }
            decoded.writeBytes(buf, 0, readBytes);
        }
    } finally {
        stream.close();
    }
    if (data != null) {
        assertEquals(Unpooled.wrappedBuffer(data), decoded);
    } else {
        assertFalse(decoded.isReadable());
    }
    decoded.release();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 29 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class ZstdEncoderTest method decompress.

@Override
protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
    InputStream is = new ByteBufInputStream(compressed, true);
    ZstdInputStream zstdIs = null;
    byte[] decompressed = new byte[originalLength];
    try {
        zstdIs = new ZstdInputStream(is);
        int remaining = originalLength;
        while (remaining > 0) {
            int read = zstdIs.read(decompressed, originalLength - remaining, remaining);
            if (read > 0) {
                remaining -= read;
            } else {
                break;
            }
        }
        assertEquals(-1, zstdIs.read());
    } finally {
        if (zstdIs != null) {
            zstdIs.close();
        } else {
            is.close();
        }
    }
    return Unpooled.wrappedBuffer(decompressed);
}
Also used : ZstdInputStream(com.github.luben.zstd.ZstdInputStream) ZstdInputStream(com.github.luben.zstd.ZstdInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 30 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class Http2StreamChannelIdTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    ChannelId normalInstance = new Http2StreamChannelId(DefaultChannelId.newInstance(), 0);
    ByteBuf buf = Unpooled.buffer();
    ObjectOutputStream outStream = new ObjectOutputStream(new ByteBufOutputStream(buf));
    try {
        outStream.writeObject(normalInstance);
    } finally {
        outStream.close();
    }
    ObjectInputStream inStream = new ObjectInputStream(new ByteBufInputStream(buf, true));
    final ChannelId deserializedInstance;
    try {
        deserializedInstance = (ChannelId) inStream.readObject();
    } finally {
        inStream.close();
    }
    assertEquals(normalInstance, deserializedInstance);
    assertEquals(normalInstance.hashCode(), deserializedInstance.hashCode());
    assertEquals(0, normalInstance.compareTo(deserializedInstance));
    assertEquals(normalInstance.asLongText(), deserializedInstance.asLongText());
    assertEquals(normalInstance.asShortText(), deserializedInstance.asShortText());
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) DefaultChannelId(io.netty.channel.DefaultChannelId) ChannelId(io.netty.channel.ChannelId) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)69 ByteBuf (io.netty.buffer.ByteBuf)22 IOException (java.io.IOException)22 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 InputStream (java.io.InputStream)10 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)6 POST (javax.ws.rs.POST)6 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ObjectOutputStream (java.io.ObjectOutputStream)3