proto_stream.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/io/proto_stream.h"
00018 
00019 #include "glog/logging.h"
00020 
00021 namespace cartographer {
00022 namespace io {
00023 
00024 namespace {
00025 
00026 // First eight bytes to identify our proto stream format.
00027 const uint64 kMagic = 0x7b1d1f7b5bf501db;
00028 
00029 void WriteSizeAsLittleEndian(uint64 size, std::ostream* out) {
00030   for (int i = 0; i != 8; ++i) {
00031     out->put(size & 0xff);
00032     size >>= 8;
00033   }
00034 }
00035 
00036 bool ReadSizeAsLittleEndian(std::istream* in, uint64* size) {
00037   *size = 0;
00038   for (int i = 0; i != 8; ++i) {
00039     *size >>= 8;
00040     *size += static_cast<uint64>(in->get()) << 56;
00041   }
00042   return !in->fail();
00043 }
00044 
00045 }  // namespace
00046 
00047 ProtoStreamWriter::ProtoStreamWriter(const std::string& filename)
00048     : out_(filename, std::ios::out | std::ios::binary) {
00049   WriteSizeAsLittleEndian(kMagic, &out_);
00050 }
00051 
00052 void ProtoStreamWriter::Write(const std::string& uncompressed_data) {
00053   std::string compressed_data;
00054   common::FastGzipString(uncompressed_data, &compressed_data);
00055   WriteSizeAsLittleEndian(compressed_data.size(), &out_);
00056   out_.write(compressed_data.data(), compressed_data.size());
00057 }
00058 
00059 void ProtoStreamWriter::WriteProto(const google::protobuf::Message& proto) {
00060   std::string uncompressed_data;
00061   proto.SerializeToString(&uncompressed_data);
00062   Write(uncompressed_data);
00063 }
00064 
00065 bool ProtoStreamWriter::Close() {
00066   out_.close();
00067   return !out_.fail();
00068 }
00069 
00070 ProtoStreamReader::ProtoStreamReader(const std::string& filename)
00071     : in_(filename, std::ios::in | std::ios::binary) {
00072   uint64 magic;
00073   if (!ReadSizeAsLittleEndian(&in_, &magic) || magic != kMagic) {
00074     in_.setstate(std::ios::failbit);
00075   }
00076   CHECK(in_.good()) << "Failed to open proto stream '" << filename << "'.";
00077 }
00078 
00079 bool ProtoStreamReader::Read(std::string* decompressed_data) {
00080   uint64 compressed_size;
00081   if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
00082     return false;
00083   }
00084   std::string compressed_data(compressed_size, '\0');
00085   if (!in_.read(&compressed_data.front(), compressed_size)) {
00086     return false;
00087   }
00088   common::FastGunzipString(compressed_data, decompressed_data);
00089   return true;
00090 }
00091 
00092 bool ProtoStreamReader::ReadProto(google::protobuf::Message* proto) {
00093   std::string decompressed_data;
00094   return Read(&decompressed_data) && proto->ParseFromString(decompressed_data);
00095 }
00096 
00097 bool ProtoStreamReader::eof() const { return in_.eof(); }
00098 
00099 }  // namespace io
00100 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35