uncompressed_stream.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #include "rosbag/chunked_file.h"
36 
37 #include <iostream>
38 #include <cstring>
39 
40 #include <boost/format.hpp>
41 
42 using std::string;
43 using boost::format;
44 using ros::Exception;
45 
46 namespace rosbag {
47 
49 
52 }
53 
54 void UncompressedStream::write(void* ptr, size_t size) {
55  size_t result = fwrite(ptr, 1, size, getFilePointer());
56  if (result != size)
57  throw BagIOException((format("Error writing to file: writing %1% bytes, wrote %2% bytes") % size % result).str());
58 
59  advanceOffset(size);
60 }
61 
62 void UncompressedStream::read(void* ptr, size_t size) {
63  size_t nUnused = (size_t) getUnusedLength();
64  char* unused = getUnused();
65 
66  if (nUnused > 0) {
67  // We have unused data from the last compressed read
68  if (nUnused == size) {
69  // Copy the unused data into the buffer
70  memcpy(ptr, unused, nUnused);
71 
72  clearUnused();
73  }
74  else if (nUnused < size) {
75  // Copy the unused data into the buffer
76  memcpy(ptr, unused, nUnused);
77 
78  // Still have data to read
79  size -= nUnused;
80 
81  // Read the remaining data from the file
82  int result = fread((char*) ptr + nUnused, 1, size, getFilePointer());
83  if ((size_t) result != size)
84  throw BagIOException((format("Error reading from file + unused: wanted %1% bytes, read %2% bytes") % size % result).str());
85 
86  advanceOffset(size);
87 
88  clearUnused();
89  }
90  else {
91  // nUnused_ > size
92  memcpy(ptr, unused, size);
93 
94  setUnused(unused + size);
95  setUnusedLength(nUnused - size);
96  }
97  }
98 
99  // No unused data - read from stream
100  int result = fread(ptr, 1, size, getFilePointer());
101  if ((size_t) result != size)
102  throw BagIOException((format("Error reading from file: wanted %1% bytes, read %2% bytes") % size % result).str());
103 
104  advanceOffset(size);
105 }
106 
107 void UncompressedStream::decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) {
108  if (dest_len < source_len)
109  throw BagException("dest_len not large enough");
110 
111  memcpy(dest, source, source_len);
112 }
113 
114 } // namespace rosbag
rosbag::ChunkedFile
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
Definition: chunked_file.h:83
rosbag::Stream::getFilePointer
FILE * getFilePointer()
Definition: stream.cpp:73
rosbag::Stream::clearUnused
void clearUnused()
Definition: stream.cpp:81
rosbag::UncompressedStream::UncompressedStream
UncompressedStream(ChunkedFile *file)
Definition: uncompressed_stream.cpp:48
rosbag::Stream::setUnused
void setUnused(char *unused)
Definition: stream.cpp:79
ros::Exception
rosbag::UncompressedStream::write
void write(void *ptr, size_t size)
Definition: uncompressed_stream.cpp:54
rosbag::BagException
Base class for rosbag exceptions.
Definition: exceptions.h:75
rosbag::BagIOException
Exception thrown when on IO problems.
Definition: exceptions.h:82
rosbag::UncompressedStream::getCompressionType
CompressionType getCompressionType() const
Definition: uncompressed_stream.cpp:50
rosbag::Stream::getUnusedLength
int getUnusedLength()
Definition: stream.cpp:78
rosbag::UncompressedStream::decompress
void decompress(uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
Definition: uncompressed_stream.cpp:107
rosbag::compression::CompressionType
CompressionType
Definition: stream.h:119
rosbag::Stream::getUnused
char * getUnused()
Definition: stream.cpp:77
rosbag::Stream
Definition: stream.h:100
rosbag::Stream::setUnusedLength
void setUnusedLength(int nUnused)
Definition: stream.cpp:80
rosbag::compression::Uncompressed
@ Uncompressed
Definition: stream.h:153
rosbag::UncompressedStream::read
void read(void *ptr, size_t size)
Definition: uncompressed_stream.cpp:62
rosbag
Definition: aes_encryptor.h:43
rosbag::Stream::advanceOffset
void advanceOffset(uint64_t nbytes)
Definition: stream.cpp:76
chunked_file.h


rosbag_storage
Author(s): Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:52