diff --git a/.gitignore b/.gitignore index abf8c52..5a41e61 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,5 @@ bin/ .DS_Store /map-compressed.ubo /map-normal.ubo +/list-compressed.ubo +/list-normal.ubo diff --git a/README.md b/README.md index 24cd69e..a40a347 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,12 @@ Assuming you use gradle. ~~Here's the [jitpack listing](https://jitpack.io/#Ultreon/ultreon-data) for the current versions and builds you can use.~~ (Temporarily not used) Check out the releases page for latest releases. + +## Naming conventions +The following conventions are for map keys: + * `MapType` and `ListType` are in `DromedaryCase`. + * Any other types are in `camelCase`. + +Do note that in some cases keys can have a different case. +For example if the key is used for identifiers. (Like those: `namespace:id`) +Tho it's generally not recommended to use map keys for objects like identifiers or numbers. diff --git a/src/main/java/com/ultreon/data/DataIo.java b/src/main/java/com/ultreon/data/DataIo.java index 6f394ec..17cb60f 100644 --- a/src/main/java/com/ultreon/data/DataIo.java +++ b/src/main/java/com/ultreon/data/DataIo.java @@ -51,7 +51,7 @@ public static > T read(InputStream stream, T... type) throws Class componentType = (Class) type.getClass().getComponentType(); int componentId = TypeRegistry.getId(componentType); - int id = inputStream.readByte(); + int id = inputStream.readUnsignedByte(); if (componentId != id) { throw new DataTypeException("The read data id " + id + " is different from the expected id: " + componentId); diff --git a/src/main/java/com/ultreon/data/types/BitSetType.java b/src/main/java/com/ultreon/data/types/BitSetType.java index eac6909..b699da2 100644 --- a/src/main/java/com/ultreon/data/types/BitSetType.java +++ b/src/main/java/com/ultreon/data/types/BitSetType.java @@ -38,6 +38,7 @@ public int id() { @Override public void write(DataOutputStream stream) throws IOException { byte[] arr = this.obj.toByteArray(); + if (arr.length >= 32768) throw new IllegalArgumentException("Bitset is too big to be written"); stream.writeShort(arr.length); for (byte b : arr) { stream.writeByte(b); @@ -45,7 +46,7 @@ public void write(DataOutputStream stream) throws IOException { } public static BitSetType read(DataInputStream stream) throws IOException { - int len = stream.readUnsignedShort(); + int len = stream.readShort(); byte[] arr = new byte[len]; for (int i = 0; i < len; i++) { arr[i] = stream.readByte(); diff --git a/src/main/java/com/ultreon/data/types/ListType.java b/src/main/java/com/ultreon/data/types/ListType.java index 7e21478..fd8267c 100644 --- a/src/main/java/com/ultreon/data/types/ListType.java +++ b/src/main/java/com/ultreon/data/types/ListType.java @@ -83,7 +83,7 @@ public void write(DataOutputStream stream) throws IOException { } public static ListType read(DataInputStream stream) throws IOException { - byte id = stream.readByte(); + int id = stream.readUnsignedByte(); int len = stream.readInt(); List> list = new ArrayList<>(); for (int i = 0; i < len; i++) { diff --git a/src/main/java/com/ultreon/data/types/MapType.java b/src/main/java/com/ultreon/data/types/MapType.java index b1a5b1e..e4f31ad 100644 --- a/src/main/java/com/ultreon/data/types/MapType.java +++ b/src/main/java/com/ultreon/data/types/MapType.java @@ -75,7 +75,7 @@ public static MapType read(DataInputStream stream) throws IOException { bytes[j] = stream.readByte(); } String key = new String(bytes, StandardCharsets.UTF_8); - byte id = stream.readByte(); + int id = stream.readUnsignedByte(); map.put(key, TypeRegistry.read(id, stream)); } @@ -248,7 +248,7 @@ public BigInteger getBigInt(String key, BigInteger def) { } public float getFloat(String key) { - return getFloat(key, (float) 0); + return getFloat(key, 0); } public float getFloat(String key, float def) { diff --git a/src/test/java/com/ultreon/tests/data/ReadWriteTests.java b/src/test/java/com/ultreon/tests/data/ReadWriteTests.java index 91c8844..1f3c2e9 100644 --- a/src/test/java/com/ultreon/tests/data/ReadWriteTests.java +++ b/src/test/java/com/ultreon/tests/data/ReadWriteTests.java @@ -1,10 +1,10 @@ package com.ultreon.tests.data; import com.ultreon.data.DataIo; -import com.ultreon.data.Types; import com.ultreon.data.types.ListType; import com.ultreon.data.types.MapType; import com.ultreon.data.types.StringType; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,18 +12,29 @@ import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.BitSet; import java.util.UUID; -public class ReadWriteTests { +class ReadWriteTests { @Test - @DisplayName("Write -> Map") - void writeMap() { + @DisplayName("MapTypes") + void readWriteMap() { + BitSet bitSet = new BitSet(16); + bitSet.set(0); + bitSet.set(2); + bitSet.set(5); + bitSet.set(8); + bitSet.set(12); + bitSet.set(14); + bitSet.set(15); + MapType type = new MapType(); type.putInt("integer", 123456789); type.putString("string", "Hello World"); type.putByte("byte", 64); type.putShort("short", 1024); type.putUUID("uuid", UUID.nameUUIDFromBytes("Hello World".getBytes())); + type.putBitSet("bitSet", bitSet); type.putDouble("double", 123456.123456789); type.putFloat("float", 123.456f); type.putLong("long", 7342041283402173783L); @@ -45,7 +56,7 @@ void writeMap() { inner.putLong("fileSize", 7_323_358_494L); type.put("Map", inner); - ListType list = new ListType(Types.STRING); + ListType list = new ListType<>(); list.add(new StringType("Glitch")); list.add(new StringType("Qboi")); list.add(new StringType("QTechCommunity")); @@ -65,23 +76,66 @@ void writeMap() { } catch (IOException e) { throw new RuntimeException(e); } - } - @Test - @DisplayName("Read -> Map") - void readMap() { + MapType readMap; try { System.out.println("Reading normal map data..."); - MapType map = DataIo.read(new File("map-normal.ubo")); + readMap = DataIo.read(new File("map-normal.ubo")); } catch (IOException e) { throw new RuntimeException(e); } + Assertions.assertEquals(readMap, type); + MapType readCompressedMap; try { System.out.println("Reading compressed map data..."); - MapType map = DataIo.readCompressed(new File("map-compressed.ubo")); + readCompressedMap = DataIo.readCompressed(new File("map-compressed.ubo")); + } catch (IOException e) { + throw new RuntimeException(e); + } + Assertions.assertEquals(readCompressedMap, type); + } + + @Test + @DisplayName("ListTypes") + void readWriteList() { + ListType list = new ListType<>(); + list.add(new StringType("Apple")); + list.add(new StringType("Banana")); + list.add(new StringType("Pear")); + list.add(new StringType("Orange")); + list.add(new StringType("Watermelon")); + + try { + System.out.println("Writing normal list data..."); + DataIo.write(list, new File("list-normal.ubo")); + } catch (IOException e) { + throw new RuntimeException(e); + } + + try { + System.out.println("Writing compressed list data..."); + DataIo.writeCompressed(list, new File("list-compressed.ubo")); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ListType readList; + try { + System.out.println("Reading normal list data..."); + readList = DataIo.read(new File("list-normal.ubo")); + } catch (IOException e) { + throw new RuntimeException(e); + } + Assertions.assertEquals(readList, list); + + ListType readCompressedList; + try { + System.out.println("Reading compressed list data..."); + readCompressedList = DataIo.readCompressed(new File("list-compressed.ubo")); } catch (IOException e) { throw new RuntimeException(e); } + Assertions.assertEquals(readCompressedList, list); } }