[protobuf] CopyingOutputStreamAdaptor Not Writing to CopyingOutputStream?

2013-05-10 Thread jonathan . wolk
Hey All,

I've stumbled upon something this is confusing me and need a little help. 
I've made a message, compiled it  (for C++) optimized for LITE_RUNTIME in 
proto buffers 2.5.0.

From my understanding, using the lite runtime means no default File/Ostream 
output streams are implemented. So, I implemented my own with the following 
code for testing purposes (so, please ignore some sloppy code)

class MyStream : public google::protobuf::io::CopyingOutputStream

{

public:

MyStream(const string filename) : google::protobuf::io::
CopyingOutputStream()

{

m_ofstream = new ofstream(filename.c_str(), ios::out | ios::binary);

}

~ MyStream()

{

m_ofstream-flush();

m_ofstream-close();

delete m_ofstream;

}

bool Write(const void* buffer, int size)

{

LOGI(WRITING TO OFSTREAM);

m_ofstream-write(reinterpret_castconst char*(buffer), size);

return m_ofstream-good();

}

private:

ofstream* m_ofstream;

};


I create an instance of the message I want to serialize and attempt to 
write out to a file with the following code


MyStream* bla = new  MyStream(example_out.scr);

google::protobuf::io::CopyingOutputStreamAdaptor outStream(bla);

if ( !person.SerializeToZeroCopyStream(outStream) ) {

LOGI(FAILED TO WRITE PERSON);

}

delete bla;


Upon doing the serialization (which doesn't fail), I noticed that the Write 
function 
in my stream class is never called. Further debugging notes that in 
MessageLite::SerializePartialToCodedStream that the function serializes the 
message to a byte array, but that byte array seemingly is never passed to 
the CodedOutputStream that is passed as a parameter. Am I doing something 
wrong? Code snippet of MessageLite::SerializePartialToCodedStream and 
MessageLite::SerializeToCodedStream are below.


bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) 
const {

  GOOGLE_DCHECK(IsInitialized())  InitializationErrorMessage(serialize, 
*this);

  return SerializePartialToCodedStream(output);

}

MessageLite::SerializePartialToCodedStream(

io::CodedOutputStream* output) const {

  const int size = ByteSize();  // Force size to be cached.

  uint8* buffer = output-GetDirectBufferForNBytesAndAdvance(size);

  if (buffer != NULL) {

uint8* end = SerializeWithCachedSizesToArray(buffer);

if (end - buffer != size) {

  ByteSizeConsistencyError(size, ByteSize(), end - buffer);

}

return true;



}

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [protobuf] CopyingOutputStreamAdaptor Not Writing to CopyingOutputStream?

2013-05-10 Thread Feng Xiao
On Fri, May 10, 2013 at 11:16 AM, jonathan.w...@gree.co.jp wrote:

 Hey All,

 I've stumbled upon something this is confusing me and need a little help.
 I've made a message, compiled it  (for C++) optimized for LITE_RUNTIME in
 proto buffers 2.5.0.

 From my understanding, using the lite runtime means no default
 File/Ostream output streams are implemented. So, I implemented my own with
 the following code for testing purposes (so, please ignore some sloppy code)

 class MyStream : public google::protobuf::io::CopyingOutputStream

 {

 public:

 MyStream(const string filename) : google::protobuf::io::
 CopyingOutputStream()

 {

 m_ofstream = new ofstream(filename.c_str(), ios::out | ios::binary
 );

 }

 ~ MyStream()

 {

 m_ofstream-flush();

 m_ofstream-close();

 delete m_ofstream;

 }

 bool Write(const void* buffer, int size)

 {

 LOGI(WRITING TO OFSTREAM);

 m_ofstream-write(reinterpret_castconst char*(buffer), size);

 return m_ofstream-good();

 }

 private:

 ofstream* m_ofstream;

 };


 I create an instance of the message I want to serialize and attempt to
 write out to a file with the following code


 MyStream* bla = new  MyStream(example_out.scr);

 google::protobuf::io::CopyingOutputStreamAdaptor outStream(bla);

 if ( !person.SerializeToZeroCopyStream(outStream) ) {

 LOGI(FAILED TO WRITE PERSON);

 }

You need to call outStream.Flush() here.



 delete bla;


 Upon doing the serialization (which doesn't fail), I noticed that the
 Write function in my stream class is never called. Further debugging
 notes that in MessageLite::SerializePartialToCodedStream that the
 function serializes the message to a byte array, but that byte array
 seemingly is never passed to the CodedOutputStream that is passed as a
 parameter. Am I doing something wrong? Code snippet of 
 MessageLite::SerializePartialToCodedStream
 and MessageLite::SerializeToCodedStream are below.


 bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output)
 const {

   GOOGLE_DCHECK(IsInitialized())  InitializationErrorMessage(serialize,
 *this);

   return SerializePartialToCodedStream(output);

 }

 MessageLite::SerializePartialToCodedStream(

 io::CodedOutputStream* output) const {

   const int size = ByteSize();  // Force size to be cached.

   uint8* buffer = output-GetDirectBufferForNBytesAndAdvance(size);

   if (buffer != NULL) {

 uint8* end = SerializeWithCachedSizesToArray(buffer);

 if (end - buffer != size) {

   ByteSizeConsistencyError(size, ByteSize(), end - buffer);

 }

 return true;

 

 }

  --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to protobuf+unsubscr...@googlegroups.com.
 To post to this group, send email to protobuf@googlegroups.com.
 Visit this group at http://groups.google.com/group/protobuf?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.




-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.