title: Cannot deserialize protobuf data from C++ in Java
body: <p>My problem is to serialize protobuf data in C++ and deserialize the data in Java probably. 
  Here is the code I use to the hints given by dcn:</p>
  
  <p>With this I create the protobuf data in C++ and write it to an ostream which is send via socket.</p>
  
  <pre><code>Name name;
  name.set_name("platzhirsch");
  
  boost::asio::streambuf b;
  std::ostream os(&amp;b);
  
  ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&amp;os);
  CodedOutputStream *coded_output = new CodedOutputStream(raw_output);
  
  coded_output-&gt;WriteLittleEndian32(name.ByteSize());
  name.SerializeToCodedStream(coded_output);
  socket.send(b);
  </code></pre>
  
  <p>This is the Java side where I try to parse it:</p>
  
  <pre><code>NameProtos.Name name = NameProtos.Name.parseDelimitedFrom(socket.getInputStream());
  System.out.println(name.newBuilder().build().toString());
  </code></pre>
  
  <p>However by this I get this Exception:
  <strong>com.google.protobuf.UninitializedMessageException: Message missing required fields: name</strong></p>
  
  <p>What am I missing?</p>
  
  <hr>
  
  <p>The flawed code line is: <code>name.newBuilder().build().toString()</code></p>
  
  <p>This would have never worked, a new instance is created with uninitialized name field. Anyway the answer here solved the rest of my problem.</p>
  
  <p>One last thing, which I was told in the protobuf mailinglist: In order to flush the CodedOutputStreams, the objects have to be deleted!</p>
  
  <pre><code>delete coded_output;
  delete raw_output;
  </code></pre>