
nl.weeaboo.lua2.io.ObjectDeserializer Maven / Gradle / Ivy
package nl.weeaboo.lua2.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Reads Lua objects from a binary stream.
*
* @see LuaSerializer
*/
public class ObjectDeserializer extends ObjectInputStream {
private static final Logger LOG = LoggerFactory.getLogger(ObjectDeserializer.class);
private final @Nullable Environment env;
private final ExecutorService executor;
private boolean collectStats = true;
private int depthWarnLimit = 100;
protected ObjectDeserializer(InputStream in, Environment e) throws IOException {
super(in);
env = (e.size() > 0 ? e : null);
executor = new DelayedIoExecutor("LuaObjectDeserializer");
updateEnableReplace();
}
@Override
public void close() throws IOException {
try {
super.close();
} finally {
executor.shutdown();
}
}
private void updateEnableReplace() {
boolean replace = (env != null || collectStats);
try {
enableResolveObject(replace);
} catch (SecurityException se) {
LOG.error("Error calling 'enableReplaceObject'", se);
}
}
/**
* Calls {@link ObjectInputStream#readObject()} on a new thread and returns the result.
*
* This method can be used to avoid stack space issues when deserializing large object graphs.
*
* @throws IOException If the thread throws an exception, or if the wait for the thread's result is interrupted.
*/
public Object readObjectOnNewThread() throws IOException {
Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy