Beatmup Java package
ChunkCollection.java
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 package Beatmup.Utils;
20 
21 import java.io.IOError;
22 
23 import Beatmup.Object;
24 
25 /**
26  * A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
27  * A chunk is a header and a piece of data packed in memory like this: (idLength[4], id[idLength], size[sizeof(chunksize_t)], data[size])
28  * ChunkCollection defines an interface to retrieve chunks by their ids.
29  */
30 public class ChunkCollection extends Object {
31  private native void open(long handle) throws IOError;
32  private native void close(long handle);
33  private native long size(long handle);
34  private native boolean chunkExists(long handle, String id);
35  private native long chunkSize(long handle, String id);
36  private native void save(long handle, String filename, boolean append);
37  private native String read(long handle, String id);
38 
39  protected ChunkCollection(long handle) {
40  super(handle);
41  }
42 
43  /**
44  * Opens the collection to read chunks from it.
45  */
46  public void open() throws IOError {
47  open(handle);
48  }
49 
50  /**
51  * Closes the collection after a reading session.
52  */
53  public void close() {
54  close(handle);
55  }
56 
57  /**
58  * @return number of chunks in the collection.
59  */
60  public long size() {
61  return size(handle);
62  }
63 
64  /**
65  * Check if a specific chunk exists.
66  * @param id The chunk id
67  * @return `true` if the chunk exists in the collection.
68  */
69  public boolean chunkExists(String id) {
70  return chunkExists(handle, id);
71  }
72 
73  /**
74  * Retrieves size of a specific chunk.
75  * @param id The chunk id
76  * @return size of the chunk in bytes, 0 if not found.
77  */
78  public long chunkSize(String id) {
79  return chunkSize(handle, id);
80  }
81 
82  /**
83  * Reads a chunk with a specific id and returns it as a string.
84  * If there is no chunk with the given id, returns an empty string.
85  * @param id The chunk id
86  * @return the chunk content as string.
87  */
88  public String read(String id) {
89  return read(handle, id);
90  }
91 }
Base class for objects natively managed by Beatmup.
Definition: Object.java:24
long handle
pointer to the native object
Definition: Object.java:25
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
String read(String id)
Reads a chunk with a specific id and returns it as a string.
void close()
Closes the collection after a reading session.
long chunkSize(String id)
Retrieves size of a specific chunk.
boolean chunkExists(String id)
Check if a specific chunk exists.
void open()
Opens the collection to read chunks from it.