proto_stream.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 #include "glog/logging.h"
19 
20 namespace cartographer {
21 namespace io {
22 
23 namespace {
24 
25 // First eight bytes to identify our proto stream format.
26 const uint64 kMagic = 0x7b1d1f7b5bf501db;
27 
28 void WriteSizeAsLittleEndian(uint64 size, std::ostream* out) {
29  for (int i = 0; i != 8; ++i) {
30  out->put(size & 0xff);
31  size >>= 8;
32  }
33 }
34 
35 bool ReadSizeAsLittleEndian(std::istream* in, uint64* size) {
36  *size = 0;
37  for (int i = 0; i != 8; ++i) {
38  *size >>= 8;
39  *size += static_cast<uint64>(in->get()) << 56;
40  }
41  return !in->fail();
42 }
43 
44 } // namespace
45 
46 ProtoStreamWriter::ProtoStreamWriter(const std::string& filename)
47  : out_(filename, std::ios::out | std::ios::binary) {
48  WriteSizeAsLittleEndian(kMagic, &out_);
49 }
50 
51 void ProtoStreamWriter::Write(const std::string& uncompressed_data) {
52  std::string compressed_data;
53  common::FastGzipString(uncompressed_data, &compressed_data);
54  WriteSizeAsLittleEndian(compressed_data.size(), &out_);
55  out_.write(compressed_data.data(), compressed_data.size());
56 }
57 
58 void ProtoStreamWriter::WriteProto(const google::protobuf::Message& proto) {
59  std::string uncompressed_data;
60  proto.SerializeToString(&uncompressed_data);
61  Write(uncompressed_data);
62 }
63 
65  out_.close();
66  return !out_.fail();
67 }
68 
69 ProtoStreamReader::ProtoStreamReader(const std::string& filename)
70  : in_(filename, std::ios::in | std::ios::binary) {
71  uint64 magic;
72  if (!ReadSizeAsLittleEndian(&in_, &magic) || magic != kMagic) {
73  in_.setstate(std::ios::failbit);
74  }
75  CHECK(in_.good()) << "Failed to open proto stream '" << filename << "'.";
76 }
77 
78 bool ProtoStreamReader::Read(std::string* decompressed_data) {
79  uint64 compressed_size;
80  if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
81  return false;
82  }
83  std::string compressed_data(compressed_size, '\0');
84  if (!in_.read(&compressed_data.front(), compressed_size)) {
85  return false;
86  }
87  common::FastGunzipString(compressed_data, decompressed_data);
88  return true;
89 }
90 
91 bool ProtoStreamReader::ReadProto(google::protobuf::Message* proto) {
92  std::string decompressed_data;
93  return Read(&decompressed_data) && proto->ParseFromString(decompressed_data);
94 }
95 
96 bool ProtoStreamReader::eof() const { return in_.eof(); }
97 
98 } // namespace io
99 } // namespace cartographer
uint64_t uint64
Definition: port.h:37
ProtoStreamWriter(const std::string &filename)
Definition: proto_stream.cc:46
bool ReadProto(google::protobuf::Message *proto) override
Definition: proto_stream.cc:91
ProtoStreamReader(const std::string &filename)
Definition: proto_stream.cc:69
void Write(const std::string &uncompressed_data)
Definition: proto_stream.cc:51
void FastGunzipString(const std::string &compressed, std::string *decompressed)
Definition: port.h:60
void WriteProto(const google::protobuf::Message &proto) override
Definition: proto_stream.cc:58
void FastGzipString(const std::string &uncompressed, std::string *compressed)
Definition: port.h:49
bool Read(std::string *decompressed_data)
Definition: proto_stream.cc:78


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58