All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.proto.streamio.PBStreamWriter Maven / Gradle / Ivy

Go to download

A package to read and write Google Protocol Buffer messages to and from Input and Output Streams

There is a newer version: 1.5.5
Show newest version
package com.google.code.proto.streamio;

import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.GeneratedMessage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A writer for an output stream of Google protocol buffer generated messages.
 * 
*
* Usage:
* {@code List messages = getGPBMessages...} *
* {@code PBStreamWriter.writeToStream(out, messages); } * * @author nichole */ public class PBStreamWriter { /** * Write GeneratedMessages to the given output stream. * * It uses a byte marker and array for delimiting messages. * * @param out output stream being written to * @param messages messages generated by Google's protocol buffer * @throws IOException */ public static void writeToStream(OutputStream out, List messages) throws IOException { writeToStream(out, messages, new PBWireByteMarkerHelper()); } /** * Write GeneratedMessages to the given output stream, * and use the a specialized PBWireByteMarkerHelper to write the delimiters. * * It uses a byte marker and array for delimiting messages. * * @param out output stream being written to * @param messages messages generated by Google's protocol buffer * @param gpbWireByteMarkerHelper instance of PBWireByteMarkerHelper used to write delimiters * @throws IOException */ public static void writeToStream(OutputStream out, List messages, PBWireByteMarkerHelper gpbWireByteMarkerHelper) throws IOException { for (GeneratedMessage message : messages) { byte[] delimiter = gpbWireByteMarkerHelper.createMessageDelimiter(message); out.write(delimiter); CodedOutputStream cos = CodedOutputStream.newInstance(out); message.writeTo(cos); cos.flush(); out.flush(); Logger.getLogger(PBStreamWriter.class.getName()).log( Level.INFO, "===> Wrote {0} bytes to stream + {1} bytes for delimeter", new Object[]{ Integer.toString(message.getSerializedSize()), Integer.toString(delimiter.length) }); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy