18 #include "glog/logging.h" 26 const uint64 kMagic = 0x7b1d1f7b5bf501db;
28 void WriteSizeAsLittleEndian(
uint64 size, std::ostream* out) {
29 for (
int i = 0; i != 8; ++i) {
30 out->put(size & 0xff);
35 bool ReadSizeAsLittleEndian(std::istream* in,
uint64* size) {
37 for (
int i = 0; i != 8; ++i) {
39 *size +=
static_cast<uint64>(in->get()) << 56;
47 : out_(filename, std::ios::out | std::ios::binary) {
48 WriteSizeAsLittleEndian(kMagic, &
out_);
52 std::string compressed_data;
54 WriteSizeAsLittleEndian(compressed_data.size(), &
out_);
55 out_.write(compressed_data.data(), compressed_data.size());
59 std::string uncompressed_data;
60 proto.SerializeToString(&uncompressed_data);
61 Write(uncompressed_data);
70 : in_(filename, std::ios::in | std::ios::binary) {
72 if (!ReadSizeAsLittleEndian(&
in_, &magic) || magic != kMagic) {
73 in_.setstate(std::ios::failbit);
75 CHECK(
in_.good()) <<
"Failed to open proto stream '" << filename <<
"'.";
80 if (!ReadSizeAsLittleEndian(&
in_, &compressed_size)) {
83 std::string compressed_data(compressed_size,
'\0');
84 if (!
in_.read(&compressed_data.front(), compressed_size)) {
92 std::string decompressed_data;
93 return Read(&decompressed_data) && proto->ParseFromString(decompressed_data);
ProtoStreamWriter(const std::string &filename)
bool ReadProto(google::protobuf::Message *proto) override
ProtoStreamReader(const std::string &filename)
void Write(const std::string &uncompressed_data)
void FastGunzipString(const std::string &compressed, std::string *decompressed)
void WriteProto(const google::protobuf::Message &proto) override
void FastGzipString(const std::string &uncompressed, std::string *compressed)
bool eof() const override
bool Read(std::string *decompressed_data)