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);
}
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());
}
}
}
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();
}
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);
}
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());
}
Aggregations