public T uncompressCopy(byte[] buffer) { T copy = null; try (ObjectInputStream in = new CopierObjectInputStream(loader, new GZIPInputStream(new ByteArrayInputStream(buffer)))) { copy = (T) in.readObject(); } catch(IOException | ClassNotFoundException e) { e.printStackTrace(); } return copy; } }
private Game loadGame(UUID gameId) { InputStream file = null; InputStream buffer = null; InputStream gzip = null; ObjectInput input = null; try{ file = new FileInputStream("saved/" + gameId.toString() + ".game"); buffer = new BufferedInputStream(file); gzip = new GZIPInputStream(buffer); input = new CopierObjectInputStream(Main.classLoader, gzip); Game loadGame = (Game) input.readObject(); GameStates states = (GameStates) input.readObject(); loadGame.loadGameStates(states); return loadGame; } catch(ClassNotFoundException ex) { logger.fatal("Cannot load game. Class not found.", ex); } catch(IOException ex) { logger.fatal("Cannot load game:" + gameId, ex); } finally { StreamUtils.closeQuietly(file); StreamUtils.closeQuietly(buffer); StreamUtils.closeQuietly(input); StreamUtils.closeQuietly(gzip); } return null; }
public T copy(T obj) { T copy = null; FastByteArrayOutputStream fbos = null; ObjectOutputStream out = null; ObjectInputStream in = null; try { fbos = new FastByteArrayOutputStream(); out = new ObjectOutputStream(fbos); // Write the object out to a byte array out.writeObject(obj); out.flush(); // Retrieve an input stream from the byte array and read // a copy of the object back in. in = new CopierObjectInputStream(loader, fbos.getInputStream()); copy = (T) in.readObject(); } catch(IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { StreamUtils.closeQuietly(fbos); StreamUtils.closeQuietly(out); StreamUtils.closeQuietly(in); } return copy; }