io.deephaven.util.codec.ExternalizableCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deephaven-codec-builtin Show documentation
Show all versions of deephaven-codec-builtin Show documentation
Codec Builtin: Deephaven builtin codec implementations
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.util.codec;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
public class ExternalizableCodec implements ObjectCodec {
private final Class externalizableClass;
public ExternalizableCodec(String className) {
try {
// noinspection unchecked
this.externalizableClass = (Class) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
public byte @NotNull [] encode(@Nullable T input) {
if (input == null) {
throw new UnsupportedOperationException(getClass() + " does not support null input");
}
try {
final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
final ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput);
input.writeExternal(objectOutput);
objectOutput.flush();
return byteOutput.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public boolean isNullable() {
return true;
}
@Override
public int getPrecision() {
return 0;
}
@Override
public int getScale() {
return 0;
}
@Nullable
@Override
public T decode(byte @NotNull [] input, int offset, int length) {
try {
final ByteArrayInputStream byteInput = new ByteArrayInputStream(input, offset, length);
final ObjectInputStream objectInput = new ObjectInputStream(byteInput);
T result = externalizableClass.getDeclaredConstructor().newInstance();
result.readExternal(objectInput);
return result;
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (IllegalAccessException | InstantiationException | ClassNotFoundException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
@Override
public int expectedObjectWidth() {
return VARIABLE_WIDTH_SENTINEL;
}
}