Beatmup
Beatmup::ChunkStream Class Reference

Stream of chunks. More...

#include <chunkfile.h>

Inheritance diagram for Beatmup::ChunkStream:
Beatmup::ChunkCollection Beatmup::Object Beatmup::Android::ChunkAsset Beatmup::ChunkFile

Classes

struct  ChunkDesc
 

Public Member Functions

size_t size () const
 Returns the number of chunks available in the collection after it is opened. More...
 
bool chunkExists (const std::string &id) const
 Check if a specific chunk exists. More...
 
chunksize_t chunkSize (const std::string &id) const
 Retrieves size of a specific chunk. More...
 
chunksize_t fetch (const std::string &id, void *data, const chunksize_t limit)
 Reads a chunk. More...
 
void save (const std::string &filename, bool append=false)
 Saves the collection to a file. More...
 
- Public Member Functions inherited from Beatmup::ChunkCollection
virtual void open ()=0
 Opens the collection to read chunks from it. More...
 
virtual void close ()=0
 Closes the collection after a reading session. More...
 
template<typename datatype >
datatype read (const std::string &id)
 Reads a chunk and casts it into a given type. More...
 
template<typename datatype >
std::vector< datatype > readVector (const std::string &id)
 Reads a chunk into a vector of a specific type. More...
 
template<>
std::string read (const std::string &id)
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Member Functions

 ChunkStream (InputStream &stream)
 
bool parse ()
 Goes through the input stream to build the list of existing chunks. More...
 

Private Attributes

std::map< std::string, ChunkDescmap
 
InputStreamstream
 

Detailed Description

Stream of chunks.

Implements ChunkCollection from a streamed source.

Definition at line 132 of file chunkfile.h.

Constructor & Destructor Documentation

◆ ChunkStream()

Beatmup::ChunkStream::ChunkStream ( InputStream stream)
inlineprotected

Definition at line 143 of file chunkfile.h.

143 : stream(stream) {}
InputStream & stream
Definition: chunkfile.h:140

Member Function Documentation

◆ parse()

bool ChunkStream::parse ( )
protected

Goes through the input stream to build the list of existing chunks.

Does not read the chunks content, only headers.

Definition at line 39 of file chunkfile.cpp.

39  {
40  map.clear();
41  if (!stream.seek(0))
42  return false;
43 
44  size_t pos = 0;
45  id_size_t idLength;
46  while (stream(&idLength, sizeof(id_size_t))) {
47  // read id
48  std::string id(idLength, 0);
49  if (!stream(&id[0], idLength))
50  return false;
51  pos += idLength + sizeof(id_size_t);
52 
53  // read length
54  ChunkDesc chunkDesc;
55  if (!stream(&chunkDesc.size, sizeof(chunksize_t)))
56  return false;
57  pos += sizeof(chunksize_t);
58 
59  // read content
60  chunkDesc.pos = pos;
61  map[id] = chunkDesc;
62  pos += chunkDesc.size;
63  if (!stream.seek(pos))
64  return false;
65  }
66  return true;
67 }
uint32_t id_size_t
chunk id length type
Definition: chunkfile.cpp:23
std::map< std::string, ChunkDesc > map
Definition: chunkfile.h:139
virtual bool seek(msize pos)=0
Moves the read pointer to a given position in the stream.
uint32_t chunksize_t
Definition: chunkfile.h:29
JNIEnv jlong jstring id

◆ size()

size_t Beatmup::ChunkStream::size ( ) const
inlinevirtual

Returns the number of chunks available in the collection after it is opened.

Implements Beatmup::ChunkCollection.

Definition at line 152 of file chunkfile.h.

152 { return map.size(); }

◆ chunkExists()

bool Beatmup::ChunkStream::chunkExists ( const std::string &  id) const
inlinevirtual

Check if a specific chunk exists.

Parameters
[in]idThe chunk id
Returns
true if the chunk exists in the collection.

Implements Beatmup::ChunkCollection.

Definition at line 154 of file chunkfile.h.

154 { return map.find(id) != map.end(); }

◆ chunkSize()

chunksize_t ChunkStream::chunkSize ( const std::string &  id) const
virtual

Retrieves size of a specific chunk.

Parameters
[in]idThe chunk id
Returns
size of the chunk in bytes, 0 if not found.

Implements Beatmup::ChunkCollection.

Definition at line 70 of file chunkfile.cpp.

70  {
71  const auto& chunk = map.find(id);
72  return chunk == map.end() ? 0 : chunk->second.size;
73 }

◆ fetch()

chunksize_t ChunkStream::fetch ( const std::string &  id,
void *  data,
const chunksize_t  limit 
)
virtual

Reads a chunk.

The collection is expected to be opened.

Parameters
[in]idWanted chunk id.
[out]dataA buffer to write out the wanted chunk content.
[in]limitThe buffer capacity in bytes.
Returns
number of bytes written out to the buffer: if the chunk is found fits the buffer, the chunk size is returned; if the chunk is found and too big, limit is returned (number of bytes actually written); if no chunk found, 0 is returned.

Implements Beatmup::ChunkCollection.

Definition at line 76 of file chunkfile.cpp.

76  {
77 #ifdef BEATMUP_DEBUG
79 #endif
80 
81  const auto& chunk = map.find(id);
82  if (chunk == map.end())
83  return 0;
84 
85  if (!stream.seek(chunk->second.pos))
86  throw RuntimeError("Cannot seek for chunk " + id);
87 
88  const chunksize_t size = chunk->second.size < limit ? chunk->second.size : limit;
89  if (!stream(data, size))
90  throw RuntimeError("Cannot read chunk " + id);
91 
92  return size;
93 }
static void check(const std::string &id)
Definition: chunkfile.cpp:31
size_t size() const
Returns the number of chunks available in the collection after it is opened.
Definition: chunkfile.h:152

◆ save()

void ChunkStream::save ( const std::string &  filename,
bool  append = false 
)
virtual

Saves the collection to a file.

Parameters
[in]filenameThe name of the file to write chunks to
[in]appendIf true, writing to the end of the file (keeping the existing content). Rewriting the file otherwise.

Implements Beatmup::ChunkCollection.

Definition at line 96 of file chunkfile.cpp.

96  {
98  for (auto it : map) {
99  Chunk chunk(*this, it.first);
100  writer(it.first, chunk(), chunk.size());
101  }
102 }
Writes chunks to a file.
Definition: chunkfile.h:191
Simply a piece of binary data of a specific size.
Definition: chunkfile.h:210
JNIEnv jlong jstring filename
JNIEnv jlong jstring jboolean append

Member Data Documentation

◆ map

std::map<std::string, ChunkDesc> Beatmup::ChunkStream::map
private

Definition at line 139 of file chunkfile.h.

◆ stream

InputStream& Beatmup::ChunkStream::stream
private

Definition at line 140 of file chunkfile.h.


The documentation for this class was generated from the following files: