use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project pravega by pravega.
the class CommandEncoder method writeMessage.
@SneakyThrows(IOException.class)
private void writeMessage(AppendBlock block, int blockSize) {
int startIdx = buffer.writerIndex();
ByteBufOutputStream bout = new ByteBufOutputStream(buffer);
bout.writeInt(block.getType().getCode());
bout.write(LENGTH_PLACEHOLDER);
block.writeFields(bout);
bout.flush();
bout.close();
int endIdx = buffer.writerIndex();
int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE;
buffer.setInt(startIdx + TYPE_SIZE, fieldsSize + blockSize);
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project pravega by pravega.
the class CommandEncoder method writeMessage.
@SneakyThrows(IOException.class)
@VisibleForTesting
static int writeMessage(WireCommand msg, ByteBuf destination) {
int startIdx = destination.writerIndex();
ByteBufOutputStream bout = new ByteBufOutputStream(destination);
bout.writeInt(msg.getType().getCode());
bout.write(LENGTH_PLACEHOLDER);
msg.writeFields(bout);
bout.flush();
bout.close();
int endIdx = destination.writerIndex();
int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE;
destination.setInt(startIdx + TYPE_SIZE, fieldsSize);
return endIdx - startIdx;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project vert.x by eclipse.
the class JacksonCodec method toBuffer.
@Override
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
ByteBuf buf = Unpooled.buffer();
// There is no need to use a try with resources here as jackson
// is a well-behaved and always calls the closes all streams in the
// "finally" block bellow.
ByteBufOutputStream out = new ByteBufOutputStream(buf);
JsonGenerator generator = createGenerator(out, pretty);
try {
encodeJson(object, generator);
generator.flush();
return Buffer.buffer(buf);
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
} finally {
close(generator);
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project traccar by tananaev.
the class GlobalstarProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, String messageId) throws TransformerException {
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("stuResponseMsg");
rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootElement.setAttribute("xsi:noNamespaceSchemaLocation", "http://cody.glpconnect.com/XSD/StuResponse_Rev1_0.xsd");
rootElement.setAttribute("deliveryTimeStamp", new SimpleDateFormat("dd/MM/yyyy hh:mm:ss z").format(new Date()));
rootElement.setAttribute("messageID", "00000000000000000000000000000000");
rootElement.setAttribute("correlationID", messageId);
document.appendChild(rootElement);
Element state = document.createElement("state");
state.appendChild(document.createTextNode("pass"));
rootElement.appendChild(state);
Element stateMessage = document.createElement("stateMessage");
stateMessage.appendChild(document.createTextNode("Store OK"));
rootElement.appendChild(stateMessage);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
ByteBuf content = Unpooled.buffer();
transformer.transform(new DOMSource(document), new StreamResult(new ByteBufOutputStream(content)));
if (channel != null) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
response.headers().add(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()).add(HttpHeaderNames.CONTENT_TYPE, "text/xml");
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream in project Almura by AlmuraDev.
the class ClientboundBiomeInformationPacket method writeTo.
@Override
public void writeTo(final ChannelBuf buf) {
final int size = this.biomes.size();
buf.writeInteger(size);
if (size > 0) {
for (final BiomeConfig biomeConfig : this.biomes.values()) {
ByteBufOutputStream byteBufOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
final ByteBuf objBuf = Unpooled.buffer();
byteBufOutputStream = new ByteBufOutputStream(objBuf);
objectOutputStream = new ObjectOutputStream(byteBufOutputStream);
objectOutputStream.writeObject(biomeConfig);
buf.writeInteger(objBuf.array().length);
buf.writeBytes(objBuf.array());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (byteBufOutputStream != null) {
try {
byteBufOutputStream.close();
} catch (IOException ignored) {
}
}
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException ignored) {
}
}
}
}
}
}
Aggregations