@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; }
@Override public void write(int b) throws IOException { ensureCapacity(outputPos); bytes[outputPos++] = (byte) b; }
/** * Returns an input stream that reads from the bytes in this stream. * Only bytes written to the stream before calling this method are * available to the input stream. */ public InputStream getInputStream() { return new Input(size()); }
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); } }
@Override public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } ensureCapacity(outputPos + len - 1); System.arraycopy(b, off, bytes, outputPos, len); outputPos += len; }
/** * Returns an input stream that reads from the bytes in this stream. * Only bytes written to the stream before calling this method are * available to the input stream. */ public InputStream getInputStream() { return new Input(size()); }
@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; }
@Override public void write(int b) throws IOException { ensureCapacity(outputPos); bytes[outputPos++] = (byte) b; }
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); } }
@Override public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } ensureCapacity(outputPos + len - 1); System.arraycopy(b, off, bytes, outputPos, len); outputPos += len; }