Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use data IO interface and add buffered IO #1

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/main/java/com/ultreon/data/DataIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
import com.ultreon.data.util.DataTypeVisitor;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.BitSet;
import java.util.UUID;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class DataIo {

private static final short VERSION = 3;
private static final int HEADER = 0xff804269;
private static final int BUFFER_SIZE = 4096;

@SafeVarargs
public static <T extends IType<?>> T read(File file, T... type) throws IOException {
try (FileInputStream stream = new FileInputStream(file)) {
try (InputStream stream = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE)) {
return read(stream, type);
}
}

@SafeVarargs
public static <T extends IType<?>> T read(URL url, T... type) throws IOException {
try (InputStream stream = url.openStream()) {
try (InputStream stream = new BufferedInputStream(url.openStream(), BUFFER_SIZE)) {
return read(stream, type);
}
}
Expand All @@ -37,44 +35,50 @@ public static <T extends IType<?>> T read(URL url, T... type) throws IOException
@SafeVarargs
@SuppressWarnings("unchecked")
public static <T extends IType<?>> T read(InputStream stream, T... type) throws IOException {
DataInputStream inputStream;
if (stream instanceof DataInputStream) {
inputStream = (DataInputStream) stream;
} else {
inputStream = new DataInputStream(stream);
if (stream instanceof DataInput) {
return read((DataInput) stream, type);
}
return read((DataInput) new DataInputStream(stream), type);
}

int magic = inputStream.readInt();
/**
* @throws IOException when an I/O error occurs.
* @throws DataTypeException when the read data type is invalid.
*/
@SafeVarargs
@SuppressWarnings("unchecked")
public static <T extends IType<?>> T read(DataInput input, T... type) throws IOException {
int magic = input.readInt();
if (magic != HEADER) {
throw new StreamCorruptedException(String.format("Invalid header got 0x%08X (expected 0xFF804269)", magic));
}

short readVersion = inputStream.readShort();
short readVersion = input.readShort();
if (readVersion > VERSION) {
throw new FutureVersionException(readVersion, VERSION);
}

Class<T> componentType = (Class<T>) type.getClass().getComponentType();
int componentId = TypeRegistry.getId(componentType);
int id = inputStream.readUnsignedByte();
int id = input.readUnsignedByte();

if (componentId != id) {
throw new DataTypeException("The read data id " + id + " is different from the expected id: " + componentId);
}

return (T) TypeRegistry.read(id, inputStream);
return (T) TypeRegistry.read(id, input);
}

@SafeVarargs
public static <T extends IType<?>> T readCompressed(File file, T... type) throws IOException {
try (FileInputStream stream = new FileInputStream(file)) {
return DataIo.readCompressed(stream, type);
try (InputStream stream = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE)) {
return readCompressed(stream, type);
}
}

@SafeVarargs
public static <T extends IType<?>> T readCompressed(URL url, T... type) throws IOException {
try (InputStream stream = url.openStream()) {
try (InputStream stream = new BufferedInputStream(url.openStream())) {
return readCompressed(stream, type);
}
}
Expand All @@ -86,40 +90,39 @@ public static <T extends IType<?>> T readCompressed(InputStream stream, T... typ
}

public static void write(IType<?> type, File file) throws IOException {
try (FileOutputStream stream = new FileOutputStream(file)) {
try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE)) {
write(type, stream);
}
}

public static void write(IType<?> type, URL file) throws IOException {
try (OutputStream stream = file.openConnection().getOutputStream()) {
try (OutputStream stream = new BufferedOutputStream(file.openConnection().getOutputStream(), BUFFER_SIZE)) {
write(type, stream);
}
}

public static void write(IType<?> type, OutputStream stream) throws IOException {
DataOutputStream outputStream;
if (stream instanceof DataOutputStream) {
outputStream = (DataOutputStream) stream;
} else {
outputStream = new DataOutputStream(stream);
if (stream instanceof DataOutput) {
write(type, (DataOutput) stream);
}
write(type, (DataOutput) new DataOutputStream(stream));
}

outputStream.writeInt(HEADER);
outputStream.writeShort(VERSION); // Version
outputStream.writeByte(type.id()); // Type
type.write(outputStream);
outputStream.flush();
public static void write(IType<?> type, DataOutput output) throws IOException {
output.writeInt(HEADER);
output.writeShort(VERSION); // Version
output.writeByte(type.id()); // Type
type.write(output);
}

public static void writeCompressed(IType<?> type, URL file) throws IOException {
try (OutputStream stream = file.openConnection().getOutputStream()) {
try (OutputStream stream = new BufferedOutputStream(file.openConnection().getOutputStream(), BUFFER_SIZE)) {
writeCompressed(type, stream);
}
}

public static void writeCompressed(IType<?> type, File file) throws IOException {
try (FileOutputStream stream = new FileOutputStream(file)) {
try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE)) {
writeCompressed(type, stream);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ultreon/data/IReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.ultreon.data.types.IType;

import java.io.DataInputStream;
import java.io.DataInput;
import java.io.IOException;

@FunctionalInterface
public interface IReader<T extends IType<?>> {
T read(DataInputStream stream) throws IOException;
T read(DataInput input) throws IOException;
}
14 changes: 9 additions & 5 deletions src/main/java/com/ultreon/data/TypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ultreon.data.types.*;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -46,11 +47,11 @@ public static <T extends IType<?>> void register(int id, IReader<T> reader, T...
ID_MAP.put(componentType.getName(), id);
}

public static IType<?> read(int id, DataInputStream stream) throws IOException {
public static IType<?> read(int id, DataInput input) throws IOException {
if (!READERS.containsKey(id))
throw new DataTypeException("Unknown datatype id: " + id);

return READERS.get(id).read(stream);
return READERS.get(id).read(input);
}

public static Class<? extends IType<?>> getType(int id) {
Expand All @@ -62,9 +63,12 @@ public static int getId(Class<?> componentType) {
}

public static int getIdOrThrow(Class<?> componentType) {
if (!ID_MAP.containsKey(componentType.getName()))
throw new IllegalArgumentException("No type registered for " + componentType.getName());
String name = componentType.getName();
Integer id = ID_MAP.get(name);

return ID_MAP.get(componentType.getName());
if (id == null)
throw new IllegalArgumentException("No type registered for " + name);

return id;
}
}
20 changes: 10 additions & 10 deletions src/main/java/com/ultreon/data/types/BigDecType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.Types;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -36,21 +36,21 @@ public int id() {
}

@Override
public void write(DataOutputStream stream) throws IOException {
public void write(DataOutput output) throws IOException {
byte[] bytes = obj.unscaledValue().toByteArray();
stream.writeInt(bytes.length);
stream.writeInt(obj.scale());
output.writeInt(bytes.length);
output.writeInt(obj.scale());
for (byte aByte : bytes) {
stream.writeByte(aByte);
output.writeByte(aByte);
}
}

public static BigDecType read(DataInputStream stream) throws IOException {
int len = stream.readInt();
int scale = stream.readInt();
public static BigDecType read(DataInput input) throws IOException {
int len = input.readInt();
int scale = input.readInt();
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
bytes[i] = stream.readByte();
bytes[i] = input.readByte();
}

return new BigDecType(new BigDecimal(new BigInteger(bytes), scale));
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/ultreon/data/types/BigIntType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.Types;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.math.BigInteger;

Expand Down Expand Up @@ -35,19 +35,19 @@ public int id() {
}

@Override
public void write(DataOutputStream stream) throws IOException {
public void write(DataOutput output) throws IOException {
byte[] bytes = obj.toByteArray();
stream.writeInt(bytes.length);
output.writeInt(bytes.length);
for (byte aByte : bytes) {
stream.writeByte(aByte);
output.writeByte(aByte);
}
}

public static BigIntType read(DataInputStream stream) throws IOException {
int len = stream.readInt();
public static BigIntType read(DataInput input) throws IOException {
int len = input.readInt();
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
bytes[i] = stream.readByte();
bytes[i] = input.readByte();
}

return new BigIntType(new BigInteger(bytes));
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/ultreon/data/types/BitSetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.Types;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.BitSet;
import java.util.Objects;
Expand Down Expand Up @@ -47,20 +47,20 @@ public int id() {
}

@Override
public void write(DataOutputStream stream) throws IOException {
public void write(DataOutput output) 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);
output.writeShort(arr.length);
for (byte b : arr) {
stream.writeByte(b);
output.writeByte(b);
}
}

public static BitSetType read(DataInputStream stream) throws IOException {
int len = stream.readShort();
public static BitSetType read(DataInput input) throws IOException {
int len = input.readUnsignedShort();
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) {
arr[i] = stream.readByte();
arr[i] = input.readByte();
}
return new BitSetType(arr);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/ultreon/data/types/BooleanType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.Types;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class BooleanType implements IType<Boolean> {
Expand All @@ -30,12 +30,12 @@ public int id() {
}

@Override
public void write(DataOutputStream stream) throws IOException {
stream.writeBoolean(obj);
public void write(DataOutput output) throws IOException {
output.writeBoolean(obj);
}

public static BooleanType read(DataInputStream stream) throws IOException {
return new BooleanType(stream.readBoolean());
public static BooleanType read(DataInput input) throws IOException {
return new BooleanType(input.readBoolean());
}

@Override
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/ultreon/data/types/ByteArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.Types;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -62,18 +62,18 @@ public int id() {
}

@Override
public void write(DataOutputStream stream) throws IOException {
stream.writeInt(obj.length);
public void write(DataOutput output) throws IOException {
output.writeInt(obj.length);
for (byte b : obj) {
stream.writeByte(b);
output.writeByte(b);
}
}

public static ByteArrayType read(DataInputStream stream) throws IOException {
int len = stream.readInt();
public static ByteArrayType read(DataInput input) throws IOException {
int len = input.readInt();
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) {
arr[i] = stream.readByte();
arr[i] = input.readByte();
}
return new ByteArrayType(arr);
}
Expand Down
Loading
Loading