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