Search in sources :

Example 1 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project hadoop by apache.

the class TestProtoUtil method doVarIntTest.

private void doVarIntTest(int value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CodedOutputStream cout = CodedOutputStream.newInstance(baos);
    cout.writeRawVarint32(value);
    cout.flush();
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
    assertEquals(value, ProtoUtil.readRawVarint32(dis));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 2 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project platformlayer by platformlayer.

the class JobLogStoreBase method serialize.

protected void serialize(SimpleJobLogger logger, OutputStream os) throws IOException {
    GZIPOutputStream gzip = new GZIPOutputStream(os);
    CodedOutputStream out = CodedOutputStream.newInstance(gzip);
    Iterable<JobLogLine> lines = logger.getLogEntries();
    JobDataProtobuf.JobLogLine.Builder protobuf = JobDataProtobuf.JobLogLine.newBuilder();
    for (JobLogLine line : lines) {
        protobuf.setLevel(line.level);
        if (line.message != null) {
            protobuf.setMessage(line.message);
        } else {
            protobuf.clearMessage();
        }
        protobuf.setTimestamp(line.timestamp);
        if (line.type != null) {
            protobuf.setType(line.type);
        } else {
            protobuf.clearType();
        }
        if (line.exception != null) {
            mapToProtobuf(line.exception, protobuf.getExceptionBuilder());
        } else {
            protobuf.clearException();
        }
        JobDataProtobuf.JobLogLine message = protobuf.build();
        int size = message.getSerializedSize();
        out.writeRawVarint32(size);
        message.writeTo(out);
    }
    out.flush();
    gzip.finish();
}
Also used : JobDataProtobuf(org.platformlayer.ops.jobstore.protobuf.JobDataProtobuf) GZIPOutputStream(java.util.zip.GZIPOutputStream) JobLogLine(org.platformlayer.jobs.model.JobLogLine) CodedOutputStream(com.google.protobuf.CodedOutputStream)

Example 3 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project spring-framework by spring-projects.

the class ProtobufHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    if (contentType == null) {
        contentType = getDefaultContentType(message);
    }
    Charset charset = contentType.getCharset();
    if (charset == null) {
        charset = DEFAULT_CHARSET;
    }
    if (PROTOBUF.isCompatibleWith(contentType)) {
        setProtoHeader(outputMessage, message);
        CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
        message.writeTo(codedOutputStream);
        codedOutputStream.flush();
    } else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
        final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
        TextFormat.print(message, outputStreamWriter);
        outputStreamWriter.flush();
        outputMessage.getBody().flush();
    } else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent) {
        this.protobufFormatsSupport.print(message, outputMessage.getBody(), contentType, charset);
        outputMessage.getBody().flush();
    }
}
Also used : CodedOutputStream(com.google.protobuf.CodedOutputStream) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project gerrit by GerritCodeReview.

the class ChangeField method toProtos.

private static <T> List<byte[]> toProtos(ProtobufCodec<T> codec, Collection<T> objs) throws OrmException {
    List<byte[]> result = Lists.newArrayListWithCapacity(objs.size());
    ByteArrayOutputStream out = new ByteArrayOutputStream(256);
    try {
        for (T obj : objs) {
            out.reset();
            CodedOutputStream cos = CodedOutputStream.newInstance(out);
            codec.encode(obj, cos);
            cos.flush();
            result.add(out.toByteArray());
        }
    } catch (IOException e) {
        throw new OrmException(e);
    }
    return result;
}
Also used : OrmException(com.google.gwtorm.server.OrmException) CodedOutputStream(com.google.protobuf.CodedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 5 with CodedOutputStream

use of com.google.protobuf.CodedOutputStream in project hbase by apache.

the class PBCell method encode.

@Override
public int encode(PositionedByteRange dst, CellProtos.Cell val) {
    CodedOutputStream os = outputStreamFromByteRange(dst);
    try {
        int before = os.spaceLeft(), after, written;
        val.writeTo(os);
        after = os.spaceLeft();
        written = before - after;
        dst.setPosition(dst.getPosition() + written);
        return written;
    } catch (IOException e) {
        throw new RuntimeException("Error while encoding type.", e);
    }
}
Also used : CodedOutputStream(com.google.protobuf.CodedOutputStream) IOException(java.io.IOException)

Aggregations

CodedOutputStream (com.google.protobuf.CodedOutputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OrmException (com.google.gwtorm.server.OrmException)1 Message (com.google.protobuf.Message)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 DataInputStream (java.io.DataInputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Charset (java.nio.charset.Charset)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 RpcHeader (org.apache.drill.exec.proto.GeneralRPCProtos.RpcHeader)1 JobLogLine (org.platformlayer.jobs.model.JobLogLine)1 JobDataProtobuf (org.platformlayer.ops.jobstore.protobuf.JobDataProtobuf)1 MediaType (org.springframework.http.MediaType)1