@SuppressWarnings("unchecked") public static <T> T copy(T orig, ObjectInputStreamFactory factory) { T obj = null; ObjectOutputStream out = null; ObjectInputStream in = null; try { // Write the object out to a byte array ByteArrayIOStream byteStream = new ByteArrayIOStream(); out = new ObjectOutputStream(byteStream); out.writeObject(orig); out.close(); // Make an input stream from the byte array and read // a copy of the object back in. in = factory.create(byteStream.getInputStream(), null); obj = (T)in.readObject(); } catch(IOException e) { log.error("IO exception", e); } catch(ClassNotFoundException cnfe) { log.error("class not found exception", cnfe); } finally { closeQuietly(in); closeQuietly(out); } return obj; }
private InputStream tryGetStream(Transaction transaction, T id, StreamMetadata metadata) throws FileNotFoundException { checkStreamStored(id, metadata); if (metadata.getLength() == 0) { return new ByteArrayInputStream(new byte[0]); } else if (metadata.getLength() <= Math.min(getInMemoryThreshold(), BLOCK_SIZE_IN_BYTES)) { ByteArrayIOStream ios = new ByteArrayIOStream(Ints.saturatedCast(metadata.getLength())); loadSingleBlockToOutputStream(transaction, id, 0, ios); return ios.getInputStream(); } else { return makeStream(transaction, id, metadata); } }
@SuppressWarnings("unchecked") public static <T> T copy(T orig, ObjectInputStreamFactory factory) { T obj = null; ObjectOutputStream out = null; ObjectInputStream in = null; try { // Write the object out to a byte array ByteArrayIOStream byteStream = new ByteArrayIOStream(); out = new ObjectOutputStream(byteStream); out.writeObject(orig); out.close(); // Make an input stream from the byte array and read // a copy of the object back in. in = factory.create(byteStream.getInputStream(), null); obj = (T)in.readObject(); } catch(IOException e) { log.error("IO exception", e); } catch(ClassNotFoundException cnfe) { log.error("class not found exception", cnfe); } finally { closeQuietly(in); closeQuietly(out); } return obj; }
private InputStream tryGetStream(Transaction transaction, T id, StreamMetadata metadata) throws FileNotFoundException { checkStreamStored(id, metadata); if (metadata.getLength() == 0) { return new ByteArrayInputStream(new byte[0]); } else if (metadata.getLength() <= Math.min(getInMemoryThreshold(), BLOCK_SIZE_IN_BYTES)) { ByteArrayIOStream ios = new ByteArrayIOStream(Ints.saturatedCast(metadata.getLength())); loadSingleBlockToOutputStream(transaction, id, 0, ios); return ios.getInputStream(); } else { return makeStream(transaction, id, metadata); } }