/** * 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); } }
/** * Copy all data from the input stream to the output stream and close both * streams. Exceptions while closing are ignored. * * @param in the input stream * @param out the output stream * @return the number of bytes copied */ public static long copyAndClose(InputStream in, OutputStream out) throws IOException { try { long len = copyAndCloseInput(in, out); out.close(); return len; } catch (Exception e) { throw DbException.convertToIOException(e); } finally { closeSilently(out); } }
private static void copyFileTo(String sourceFileName, String targetFileName) { try { IOUtils.copyFiles(sourceFileName, targetFileName); } catch (IOException e) { throw DbException.convertIOException(e, null); } }
return null; return IOUtils.readBytesAndClose(in, 0); IOUtils.copy(zipIn, out); zipIn.closeEntry(); return out.toByteArray();
/** * 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); }
/** * Read a number of characters from a reader and close it. * * @param in the reader * @param length the maximum number of characters to read, or -1 to read * until the end of file * @return the string read */ public static String readStringAndClose(Reader in, int length) throws IOException { try { if (length <= 0) { length = Integer.MAX_VALUE; } int block = Math.min(Constants.IO_BUFFER_SIZE, length); StringWriter out = new StringWriter(block); copyAndCloseInput(in, out, length); return out.toString(); } finally { in.close(); } }
/** * 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 */ @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); } }
Reader reader = value.getReader(); try { IOUtils.skipFully(reader, pos - 1); IOUtils.copyAndCloseInput(reader, writer, length); } finally { reader.close();
try { OutputStream f = FileUtils.newOutputStream(javaFile.getAbsolutePath(), false); Writer out = IOUtils.getBufferedWriter(f); classFile.delete(); out.write(source);
/** * Returns the input stream. * * @return the input stream */ @Override public InputStream getAsciiStream() throws SQLException { try { debugCodeCall("getAsciiStream"); checkClosed(); String s = value.getString(); return IOUtils.getInputStreamFromString(s); } catch (Exception e) { throw logAndConvert(e); } }
/** * Updates a column in the current or insert row. * * @param columnIndex (1,2,...) * @param x the value * @param length the number of characters * @throws SQLException if the result set is closed or not updatable */ @Override public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateAsciiStream(" + columnIndex + ", x, " + length + "L);"); } checkClosed(); Value v = conn.createClob(IOUtils.getAsciiReader(x), length); update(columnIndex, v); } catch (Exception e) { throw logAndConvert(e); } }
@Override public Reader getReader() { return IOUtils.getBufferedReader(getInputStream()); }
/** * INTERNAL */ @Override public void close() { IOUtils.closeSilently(input); input = null; IOUtils.closeSilently(output); output = null; }
@Override public void call() { value = c.createClob(IOUtils.getReader(in), -1); } };
/** * Copy a file from one directory to another, or to another file. * * @param original the original file name * @param copy the file name of the copy */ public static void copyFiles(String original, String copy) throws IOException { InputStream in = FileUtils.newInputStream(original); OutputStream out = FileUtils.newOutputStream(copy, false); copyAndClose(in, out); }
@Override public void call() throws IOException { IOUtils.copy(in, out); } }.execute();
/** * Returns the length. * * @return the length */ @Override public long length() throws SQLException { try { debugCodeCall("length"); checkClosed(); if (value.getType() == Value.BLOB) { long precision = value.getPrecision(); if (precision > 0) { return precision; } } return IOUtils.copyAndCloseInput(value.getInputStream(), null); } catch (Exception e) { throw logAndConvert(e); } }
return IOUtils.getBufferedWriter(out); } catch (Exception e) { throw logAndConvert(e);
/** * Returns the value of the specified column as an input stream. * * @param columnIndex (1,2,...) * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public InputStream getAsciiStream(int columnIndex) throws SQLException { try { debugCodeCall("getAsciiStream", columnIndex); String s = get(columnIndex).getString(); return s == null ? null : IOUtils.getInputStreamFromString(s); } catch (Exception e) { throw logAndConvert(e); } }
/** * Updates a column in the current or insert row. * * @param columnLabel the column label * @param x the value * @param length the number of characters * @throws SQLException if the result set is closed or not updatable */ @Override public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateAsciiStream(" + quote(columnLabel) + ", x, " + length + "L);"); } checkClosed(); Value v = conn.createClob(IOUtils.getAsciiReader(x), length); update(columnLabel, v); } catch (Exception e) { throw logAndConvert(e); } }