@Override public void call() throws IOException { IOUtils.copy(in, out); } }.execute();
/** * Copy all data from the input stream to the output stream. Both streams * are kept open. * * @param in the input stream * @param out the output stream (null if writing is not required) * @return the number of bytes copied */ public static long copy(InputStream in, OutputStream out) throws IOException { return copy(in, out, Long.MAX_VALUE); }
public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); copy(in, out); return out.toByteArray(); } }
/** * Read a number of bytes from an input stream and close the stream. * * @param in the input stream * @param length the maximum number of bytes to read, or -1 to read until * the end of file * @return the bytes read */ public static byte[] readBytesAndClose(InputStream in, int length) throws IOException { try { if (length <= 0) { length = Integer.MAX_VALUE; } int block = Math.min(Constants.IO_BUFFER_SIZE, length); ByteArrayOutputStream out = new ByteArrayOutputStream(block); copy(in, out, length); return out.toByteArray(); } catch (Exception e) { throw DbException.convertToIOException(e); } finally { in.close(); } }
/** * Copy all data from the input stream to the output stream and close the * input stream. Exceptions while closing are ignored. * * @param in the input stream * @param out the output stream (null if writing is not required) * @return the number of bytes copied */ public static long copyAndCloseInput(InputStream in, OutputStream out) throws IOException { try { return copy(in, out); } catch (Exception e) { throw DbException.convertToIOException(e); } finally { closeSilently(in); } }
/** * Returns some bytes of the object. * * @param pos the index, the first byte is at position 1 * @param length the number of bytes * @return the bytes, at most length bytes * @throws SQLException */ @Override public byte[] getBytes(long pos, int length) throws SQLException { try { if (isDebugEnabled()) { debugCode("getBytes(" + pos + ", " + length + ");"); } checkClosed(); ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = value.getInputStream(); try { IOUtils.skipFully(in, pos - 1); IOUtils.copy(in, out, length); } finally { in.close(); } return out.toByteArray(); } catch (Exception e) { throw logAndConvert(e); } }