Java Code Examples for io.netty.buffer.ByteBufOutputStream#write()

The following examples show how to use io.netty.buffer.ByteBufOutputStream#write() . 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: ObjectEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();

    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    ObjectOutputStream oout = null;
    try {
        bout.write(LENGTH_PLACEHOLDER);
        oout = new CompactObjectOutputStream(bout);
        oout.writeObject(msg);
        oout.flush();
    } finally {
        if (oout != null) {
            oout.close();
        } else {
            bout.close();
        }
    }

    int endIdx = out.writerIndex();

    out.setInt(startIdx, endIdx - startIdx - 4);
}
 
Example 2
Source File: BatchRequest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes and writes {@link #indexRequests} into {@link #buffer}
 *
 * @return underlying buffer filled with serialized indexRequests
 * @throws IOException if serialization failed
 */
public ItemSource serialize() throws IOException {

    ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(buffer.getSource());

    // in current impl with no IDs, it's possible to reduce serialization by reusing first action
    IndexRequest identicalAction = uniformAction(indexRequests);
    byte[] actionTemplate = identicalAction != null ? objectWriter.writeValueAsBytes(identicalAction) : null;

    for (IndexRequest action : indexRequests) {

        if (actionTemplate == null) {
            objectWriter.writeValue((OutputStream) byteBufOutputStream, action);
        } else {
            byteBufOutputStream.write(actionTemplate);
        }
        byteBufOutputStream.writeByte(LINE_SEPARATOR);

        ByteBuf source = action.getSource().getSource();
        buffer.getSource().writeBytes(source);
        byteBufOutputStream.writeByte(LINE_SEPARATOR);

    }
    return buffer;
}
 
Example 3
Source File: GeoWaveRedisRowCodec.java    From geowave with Apache License 2.0 6 votes vote down vote up
protected static void encodeRow(
    final ByteBufOutputStream out,
    final GeoWaveRedisPersistedRow row,
    final boolean visibilityEnabled) throws IOException {
  out.writeByte(row.getDataId().length);
  out.writeByte(row.getFieldMask().length);
  if (visibilityEnabled) {
    out.writeByte(row.getVisibility().length);
  }
  Varint.writeUnsignedVarInt(row.getValue().length, out);
  out.writeByte(row.getNumDuplicates());
  out.write(row.getDataId());
  out.write(row.getFieldMask());
  if (visibilityEnabled) {
    out.write(row.getVisibility());
  }
  out.write(row.getValue());
}
 
Example 4
Source File: CommandEncoder.java    From pravega with Apache License 2.0 5 votes vote down vote up
@SneakyThrows(IOException.class)
private void writeMessage(AppendBlock block, int blockSize, ByteBuf out) {
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.writeInt(block.getType().getCode());
    bout.write(LENGTH_PLACEHOLDER);
    block.writeFields(bout);
    bout.flush();
    bout.close();
    int endIdx = out.writerIndex();
    int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE;
    out.setInt(startIdx + TYPE_SIZE, fieldsSize + blockSize);
}
 
Example 5
Source File: CommandEncoder.java    From pravega with Apache License 2.0 5 votes vote down vote up
@SneakyThrows(IOException.class)
@VisibleForTesting
static int writeMessage(WireCommand msg, ByteBuf out) {
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.writeInt(msg.getType().getCode());
    bout.write(LENGTH_PLACEHOLDER);
    msg.writeFields(bout);
    bout.flush();
    bout.close();
    int endIdx = out.writerIndex();
    int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE;
    out.setInt(startIdx + TYPE_SIZE, fieldsSize);
    return endIdx - startIdx;
}
 
Example 6
Source File: ObjectEncoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();

    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.write(LENGTH_PLACEHOLDER);
    ObjectOutputStream oout = new CompactObjectOutputStream(bout);
    oout.writeObject(msg);
    oout.flush();
    oout.close();

    int endIdx = out.writerIndex();

    out.setInt(startIdx, endIdx - startIdx - 4);
}
 
Example 7
Source File: MalmoMod.java    From malmo with MIT License 3 votes vote down vote up
/** Write a potentially long string as UTF8<br>
 * The ByteBufInputStream.readUTF() and writeUTF() calls use the first two bytes of the message
 * to encode the length of the string, which limits the string length to 64k.
 * This method gets around that limitation by using a four byte header.
 * @param s The string we are sending
 * @param bbos The ByteBufOutputStream we are writing to
 * @throws IOException
 */
private void writeLargeUTF(String s, ByteBufOutputStream bbos) throws IOException
{
    byte[] data = s.getBytes("utf-8");
    bbos.writeInt(data.length);
    bbos.write(data);
}