1 /*************************************************************************
6 * [2011] - [2015] Realm Inc
9 * NOTICE: All information contained herein is, and remains
10 * the property of Realm Incorporated and its suppliers,
11 * if any. The intellectual and technical concepts contained
12 * herein are proprietary to Realm Incorporated
13 * and its suppliers and may be covered by U.S. and Foreign Patents,
14 * patents in process, and are protected by trade secret or copyright law.
15 * Dissemination of this information or reproduction of this material
16 * is strictly forbidden unless prior written permission is obtained
17 * from Realm Incorporated.
19 **************************************************************************/
21 #ifndef REALM_NOINST_CHUNKED_BINARY_HPP
22 #define REALM_NOINST_CHUNKED_BINARY_HPP
24 #include <realm/binary_data.hpp>
25 #include <realm/column_binary.hpp>
26 #include <realm/table.hpp>
28 #include <realm/util/buffer_stream.hpp>
29 #include <realm/impl/input_stream.hpp>
34 /// ChunkedBinaryData manages a vector of BinaryData. It is used to facilitate
35 /// extracting large binaries from binary columns and tables.
36 class ChunkedBinaryData {
40 ChunkedBinaryData(const BinaryData& bd);
41 ChunkedBinaryData(const BinaryIterator& bd);
42 ChunkedBinaryData(const BinaryColumn& col, size_t index);
44 /// size() returns the number of bytes in the chunked binary.
45 /// FIXME: This operation is O(n).
48 /// is_null returns true if the chunked binary has zero chunks or if
49 /// the first chunk points to the nullptr.
53 char operator[](size_t index) const;
55 std::string hex_dump(const char* separator = " ", int min_digits = -1) const;
57 void write_to(util::ResettableExpandableBufferOutputStream& out) const;
59 /// copy_to() copies the chunked binary data to \a buffer of size
60 /// \a buffer_size starting at \a offset in the ChunkedBinary.
61 /// copy_to() copies until the end of \a buffer or the end of
62 /// the ChunkedBinary whichever comes first.
63 /// copy_to() returns the number of copied bytes.
64 size_t copy_to(char* buffer, size_t buffer_size, size_t offset) const;
66 /// copy_to() allocates a buffer of size() in \a dest and
67 /// copies the chunked binary data to \a dest.
68 size_t copy_to(std::unique_ptr<char[]>& dest) const;
70 /// get_first_chunk() is used in situations
71 /// where it is known that there is exactly one
72 /// chunk. This is the case if the ChunkedBinary
73 /// has been constructed from BinaryData.
74 BinaryData get_first_chunk() const;
77 BinaryIterator m_begin;
78 friend class ChunkedBinaryInputStream;
81 // FIXME: When ChunkedBinaryData is moved into Core, this should be moved as well.
82 class ChunkedBinaryInputStream : public _impl::NoCopyInputStream {
84 explicit ChunkedBinaryInputStream(const ChunkedBinaryData& chunks)
85 : m_it(chunks.m_begin)
89 bool next_block(const char*& begin, const char*& end) override
91 BinaryData block = m_it.get_next();
93 end = begin + block.size();
105 inline ChunkedBinaryData::ChunkedBinaryData()
109 inline ChunkedBinaryData::ChunkedBinaryData(const BinaryData& bd) : m_begin{bd}
113 inline ChunkedBinaryData::ChunkedBinaryData(const BinaryIterator& bd) : m_begin{bd}
117 inline ChunkedBinaryData::ChunkedBinaryData(const BinaryColumn& col, size_t index)
118 : m_begin{&col, index}
125 #endif // REALM_NOINST_CHUNKED_BINARY_HPP