29 Exception(
"Chunk id exceeds max allowed length (255 chars): %s",
id.c_str()) {}
31 static void check(
const std::string&
id) {
48 std::string
id(idLength, 0);
49 if (!
stream(&
id[0], idLength))
62 pos += chunkDesc.size;
71 const auto& chunk =
map.find(
id);
72 return chunk ==
map.end() ? 0 : chunk->second.size;
81 const auto& chunk =
map.find(
id);
82 if (chunk ==
map.end())
88 const chunksize_t size = chunk->second.size < limit ? chunk->second.size : limit;
99 Chunk chunk(*
this, it.first);
100 writer(it.first, chunk(), chunk.
size());
106 std::ifstream
stream(
filename, std::fstream::in | std::fstream::binary);
146 stream.write((
char*)&idLen,
sizeof(idLen));
147 stream.write(&
id[0], idLen);
159 Chunk(collection.chunkSize(
id))
162 DebugAssertion::check(collection.
chunkExists(
id),
"Chunk not found: " +
id);
uint32_t id_size_t
chunk id length type
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
virtual bool chunkExists(const std::string &id) const =0
Check if a specific chunk exists.
virtual chunksize_t fetch(const std::string &id, void *data, const chunksize_t limit)=0
Reads a chunk.
void operator()(const std::string &id, const void *data, const chunksize_t size)
ChunkFileWriter(const std::string &filename, bool append=false)
void open()
Opens the collection to read chunks from it.
static bool readable(const std::string &filename)
Returns true if a given file is readable.
void close()
Closes the collection after a reading session.
ChunkFile(const std::string &filename, bool openNow=true)
Creates a read-only chunk collection from a file.
const std::string filename
void save(const std::string &filename, bool append=false)
Saves the collection to a file.
std::map< std::string, ChunkDesc > map
size_t size() const
Returns the number of chunks available in the collection after it is opened.
bool parse()
Goes through the input stream to build the list of existing chunks.
chunksize_t chunkSize(const std::string &id) const
Retrieves size of a specific chunk.
chunksize_t fetch(const std::string &id, void *data, const chunksize_t limit)
Reads a chunk.
Simply a piece of binary data of a specific size.
void writeTo(ChunkFileWriter &file, const std::string &id) const
Writes a chunk out to a file.
Chunk()
Makes an empty chunk.
Base class for all exceptions.
JNIEnv jstring jboolean openNow
JNIEnv jlong jstring filename
JNIEnv jlong jstring jboolean append