Skip to content

Commit a620d3d

Browse files
committed
Added tuple and vector base types, and added boolean array.
1 parent 69fbf70 commit a620d3d

19 files changed

+998
-112
lines changed

build.gradle.kts

+25-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ dependencies {
3333
compileOnly("org.jetbrains:annotations:23.0.0")
3434
}
3535

36-
tasks.compileJava {
37-
sourceCompatibility = "1.8"
38-
targetCompatibility = "1.8"
39-
}
4036

4137
java {
4238
withSourcesJar()
4339
withJavadocJar()
40+
41+
sourceCompatibility = JavaVersion.VERSION_1_8
42+
targetCompatibility = JavaVersion.VERSION_1_8
43+
44+
toolchain {
45+
languageVersion.set(JavaLanguageVersion.of(8))
46+
}
4447
}
4548

4649
tasks.test {
@@ -95,6 +98,24 @@ publishing {
9598
name = "Staging"
9699
url = uri("file://${projectDir.path.replace("\\", "/")}/build/staging-deploy")
97100
}
101+
102+
maven {
103+
name = "UltreonMavenReleases"
104+
url = uri("https://maven.ultreon.dev/releases")
105+
credentials {
106+
username = (findProperty("ultreonmvn.name") ?: System.getenv("ULTREON_MVN_NAME")).toString()
107+
password = (findProperty("ultreonmvn.secret") ?: System.getenv("ULTREON_MVN_SEC")).toString()
108+
}
109+
}
110+
111+
maven {
112+
name = "UltreonMavenSnapshots"
113+
url = uri("https://maven.ultreon.dev/snapshots")
114+
credentials {
115+
username = (findProperty("ultreonmvn.name") ?: System.getenv("ULTREON_MVN_NAME")).toString()
116+
password = (findProperty("ultreonmvn.secret") ?: System.getenv("ULTREON_MVN_SEC")).toString()
117+
}
118+
}
98119
}
99120

100121
publications {

src/main/java/dev/ultreon/ubo/DataIo.java

+168-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import dev.ultreon.ubo.util.DataTypeVisitor;
55

66
import java.io.*;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.nio.file.Files;
10+
import java.nio.file.OpenOption;
11+
import java.nio.file.Path;
912
import java.util.zip.GZIPInputStream;
1013
import java.util.zip.GZIPOutputStream;
1114

1215
public class DataIo {
13-
private static final short VERSION = 3;
16+
private static final short VERSION = 4;
1417
private static final int HEADER = 0xff804269;
1518
private static final int BUFFER_SIZE = 4096;
1619

@@ -20,6 +23,20 @@ public static <T extends DataType<?>> T read(File file, T... type) throws IOExce
2023
return read(stream, type);
2124
}
2225
}
26+
27+
@SafeVarargs
28+
public static <T extends DataType<?>> T read(Path path, T... type) throws IOException {
29+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(path), BUFFER_SIZE)) {
30+
return read(stream, type);
31+
}
32+
}
33+
34+
@SafeVarargs
35+
public static <T extends DataType<?>> T read(Path path, OpenOption[] options, T... type) throws IOException {
36+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(path, options), BUFFER_SIZE)) {
37+
return read(stream, type);
38+
}
39+
}
2340

2441
@SafeVarargs
2542
public static <T extends DataType<?>> T read(URL url, T... type) throws IOException {
@@ -28,6 +45,13 @@ public static <T extends DataType<?>> T read(URL url, T... type) throws IOExcept
2845
}
2946
}
3047

48+
@SafeVarargs
49+
public static <T extends DataType<?>> T read(URI uri, T... type) throws IOException {
50+
try (InputStream stream = new BufferedInputStream(uri.toURL().openStream(), BUFFER_SIZE)) {
51+
return read(stream, type);
52+
}
53+
}
54+
3155
/**
3256
* @throws IOException when an I/O error occurs.
3357
* @throws DataTypeException when the read data type is invalid.
@@ -45,7 +69,6 @@ public static <T extends DataType<?>> T read(InputStream stream, T... type) thro
4569
* @throws DataTypeException when the read data type is invalid.
4670
*/
4771
@SafeVarargs
48-
@SuppressWarnings("unchecked")
4972
public static <T extends DataType<?>> T read(DataInput input, T... type) throws IOException {
5073
int magic = input.readInt();
5174
if (magic != HEADER) {
@@ -74,6 +97,20 @@ public static <T extends DataType<?>> T readCompressed(File file, T... type) thr
7497
return readCompressed(stream, type);
7598
}
7699
}
100+
101+
@SafeVarargs
102+
public static <T extends DataType<?>> T readCompressed(Path path, T... type) throws IOException {
103+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(path), BUFFER_SIZE)) {
104+
return readCompressed(stream, type);
105+
}
106+
}
107+
108+
@SafeVarargs
109+
public static <T extends DataType<?>> T readCompressed(Path path, OpenOption[] options, T... type) throws IOException {
110+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(path, options), BUFFER_SIZE)) {
111+
return readCompressed(stream, type);
112+
}
113+
}
77114

78115
@SafeVarargs
79116
public static <T extends DataType<?>> T readCompressed(URL url, T... type) throws IOException {
@@ -82,24 +119,133 @@ public static <T extends DataType<?>> T readCompressed(URL url, T... type) throw
82119
}
83120
}
84121

122+
@SafeVarargs
123+
public static <T extends DataType<?>> T readCompressed(URI uri, T... type) throws IOException {
124+
try (InputStream stream = new BufferedInputStream(uri.toURL().openConnection().getInputStream())) {
125+
return readCompressed(stream, type);
126+
}
127+
}
128+
85129
@SafeVarargs
86130
public static <T extends DataType<?>> T readCompressed(InputStream stream, T... type) throws IOException {
87131
GZIPInputStream gzipStream = new GZIPInputStream(stream);
88132
return read(gzipStream, type);
89133
}
90134

135+
public static <T extends DataType<?>> T read(File file, Class<T> type) throws IOException {
136+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(file.toPath()), BUFFER_SIZE)) {
137+
return read(stream, type);
138+
}
139+
}
140+
141+
public static <T extends DataType<?>> T read(Path path, Class<T> type, OpenOption... options) throws IOException {
142+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(path, options), BUFFER_SIZE)) {
143+
return read(stream, type);
144+
}
145+
}
146+
147+
public static <T extends DataType<?>> T read(URL url, Class<T> type) throws IOException {
148+
try (InputStream stream = new BufferedInputStream(url.openStream(), BUFFER_SIZE)) {
149+
return read(stream, type);
150+
}
151+
}
152+
153+
public static <T extends DataType<?>> T read(URI uri, Class<T> type) throws IOException {
154+
try (InputStream stream = new BufferedInputStream(uri.toURL().openStream(), BUFFER_SIZE)) {
155+
return read(stream, type);
156+
}
157+
}
158+
159+
/**
160+
* @throws IOException when an I/O error occurs.
161+
* @throws DataTypeException when the read data type is invalid.
162+
*/
163+
public static <T extends DataType<?>> T read(InputStream stream, Class<T> type) throws IOException {
164+
if (stream instanceof DataInput) {
165+
return read((DataInput) stream, type);
166+
}
167+
return read((DataInput) new DataInputStream(stream), type);
168+
}
169+
170+
/**
171+
* @throws IOException when an I/O error occurs.
172+
* @throws DataTypeException when the read data type is invalid.
173+
*/
174+
public static <T extends DataType<?>> T read(DataInput input, Class<T> type) throws IOException {
175+
int magic = input.readInt();
176+
if (magic != HEADER) {
177+
throw new StreamCorruptedException(String.format("Invalid header got 0x%08X (expected 0xFF804269)", magic));
178+
}
179+
180+
short readVersion = input.readShort();
181+
if (readVersion > VERSION) {
182+
throw new FutureVersionException(readVersion, VERSION);
183+
}
184+
185+
Class<T> componentType = (Class<T>) type.getClass().getComponentType();
186+
int componentId = DataTypeRegistry.getId(componentType);
187+
int id = input.readUnsignedByte();
188+
189+
if (componentId != id) {
190+
throw new DataTypeException("The read data id " + id + " is different from the expected id: " + componentId);
191+
}
192+
193+
return (T) DataTypeRegistry.read(id, input);
194+
}
195+
196+
public static <T extends DataType<?>> T readCompressed(File file, Class<T> type) throws IOException {
197+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(file.toPath()), BUFFER_SIZE)) {
198+
return readCompressed(stream, type);
199+
}
200+
}
201+
202+
public static <T extends DataType<?>> T readCompressed(Path path, Class<T> type, OpenOption... options) throws IOException {
203+
try (InputStream stream = new BufferedInputStream(Files.newInputStream(path, options), BUFFER_SIZE)) {
204+
return readCompressed(stream, type);
205+
}
206+
}
207+
208+
public static <T extends DataType<?>> T readCompressed(URL url, Class<T> type) throws IOException {
209+
try (InputStream stream = new BufferedInputStream(url.openStream())) {
210+
return readCompressed(stream, type);
211+
}
212+
}
213+
214+
public static <T extends DataType<?>> T readCompressed(URI uri, Class<T> type) throws IOException {
215+
try (InputStream stream = new BufferedInputStream(uri.toURL().openConnection().getInputStream())) {
216+
return readCompressed(stream, type);
217+
}
218+
}
219+
220+
public static <T extends DataType<?>> T readCompressed(InputStream stream, Class<T> type) throws IOException {
221+
GZIPInputStream gzipStream = new GZIPInputStream(stream);
222+
return read(gzipStream, type);
223+
}
224+
91225
public static void write(DataType<?> dataType, File file) throws IOException {
92226
try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(file.toPath()), BUFFER_SIZE)) {
93227
write(dataType, stream);
94228
}
95229
}
230+
231+
public static void write(DataType<?> dataType, Path path, OpenOption... options) throws IOException {
232+
try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(path, options), BUFFER_SIZE)) {
233+
write(dataType, stream);
234+
}
235+
}
96236

97237
public static void write(DataType<?> dataType, URL file) throws IOException {
98238
try (OutputStream stream = new BufferedOutputStream(file.openConnection().getOutputStream(), BUFFER_SIZE)) {
99239
write(dataType, stream);
100240
}
101241
}
102242

243+
public static void wrote(DataType<?> dataType, URI uri) throws IOException {
244+
try (OutputStream stream = new BufferedOutputStream(uri.toURL().openConnection().getOutputStream(), BUFFER_SIZE)) {
245+
write(dataType, stream);
246+
}
247+
}
248+
103249
public static void write(DataType<?> dataType, OutputStream stream) throws IOException {
104250
if (stream instanceof DataOutput) {
105251
write(dataType, (DataOutput) stream);
@@ -120,11 +266,23 @@ public static void writeCompressed(DataType<?> dataType, URL file) throws IOExce
120266
}
121267
}
122268

269+
public static void writeCompressed(DataType<?> dataType, URI uri) throws IOException {
270+
try (OutputStream stream = new BufferedOutputStream(uri.toURL().openConnection().getOutputStream(), BUFFER_SIZE)) {
271+
writeCompressed(dataType, stream);
272+
}
273+
}
274+
123275
public static void writeCompressed(DataType<?> dataType, File file) throws IOException {
124276
try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(file.toPath()), BUFFER_SIZE)) {
125277
writeCompressed(dataType, stream);
126278
}
127279
}
280+
281+
public static void writeCompressed(DataType<?> dataType, Path path, OpenOption... options) throws IOException {
282+
try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(path, options), BUFFER_SIZE)) {
283+
writeCompressed(dataType, stream);
284+
}
285+
}
128286

129287
public static void writeCompressed(DataType<?> dataType, OutputStream stream) throws IOException {
130288
GZIPOutputStream gzipStream = new GZIPOutputStream(stream);
@@ -141,15 +299,22 @@ public static <T> T visit(DataTypeVisitor<T> visitor, DataType<?> dataType) {
141299
return dataType.accept(visitor);
142300
}
143301

144-
@SuppressWarnings("unchecked")
145302
@SafeVarargs
303+
@Deprecated
146304
public static <T extends DataType<?>> T fromUso(String value, T... type) throws IOException {
147305
try (BufferedReader reader = new BufferedReader(new StringReader(value))) {
148306
DataType<?> iDataType = readUso(reader.readLine());
149307
return (T) iDataType;
150308
}
151309
}
152310

311+
public static <T extends DataType<?>> T fromUso(String value) throws IOException {
312+
try (BufferedReader reader = new BufferedReader(new StringReader(value))) {
313+
DataType<?> iDataType = readUso(reader.readLine());
314+
return (T) iDataType;
315+
}
316+
}
317+
153318
private static DataType<?> readUso(String value) throws IOException {
154319
return new UsoParser(value).parse();
155320
}

src/main/java/dev/ultreon/ubo/DataTypeRegistry.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static void init() {
3737
register(DataTypes.FLOAT_ARRAY, FloatArrayType::read);
3838
register(DataTypes.DOUBLE_ARRAY, DoubleArrayType::read);
3939
register(DataTypes.CHAR_ARRAY, CharArrayType::read);
40+
register(DataTypes.BOOLEAN_ARRAY, BooleanArrayType::read);
4041
register(DataTypes.UUID, UUIDType::read);
4142
register(DataTypes.BIT_SET, BitSetType::read);
4243
}
@@ -69,12 +70,12 @@ public static Class<? extends DataType<?>> getType(int id) {
6970
return TYPES.get(id);
7071
}
7172

72-
public static int getId(Class<?> componentType) {
73-
return ID_MAP.get(componentType.getName());
73+
public static int getId(Class<? extends DataType<?>> dataType) {
74+
return ID_MAP.get(dataType.getName());
7475
}
7576

76-
public static int getIdOrThrow(Class<?> componentType) {
77-
String name = componentType.getName();
77+
public static int getIdOrThrow(Class<? extends DataType<?>> dataType) {
78+
String name = dataType.getName();
7879
Integer id = ID_MAP.get(name);
7980

8081
if (id == null)

src/main/java/dev/ultreon/ubo/DataTypes.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class DataTypes {
2121
public static int FLOAT_ARRAY = 0x56;
2222
public static int DOUBLE_ARRAY = 0x57;
2323
public static int CHAR_ARRAY = 0x58;
24+
public static int BOOLEAN_ARRAY = 0x59;
2425
public static int UUID = 0x70;
2526
public static int BIT_SET = 0x80;
2627
}

src/main/java/dev/ultreon/ubo/types/ArrayType.java

+6
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ default boolean isEmpty() {
1010
B get(int index);
1111

1212
void set(int index, B value);
13+
14+
default void fill(B value) {
15+
for (int i = 0; i < size(); i++) {
16+
set(i, value);
17+
}
18+
}
1319
}

0 commit comments

Comments
 (0)