/** * As {@link #shove(InputStream, OutputStream)} but the streams are closed * at the end of the process. * * @param is the input stream. * @param os the output stream. * @throws IOException in case of read or write problems. */ public static void shoveAndClose( final InputStream is, final OutputStream os) throws IOException { try { shove(is, os); } finally { close(is); close(os); } }
@Override public void read(InputStream aInputStream) throws IOException { try { Util.shove(aInputStream, target); } finally { Util.close(aInputStream); } } }
/** * As {@link #shove(InputStream, OutputStream)} but the streams are closed * at the end of the process. * * @param is the input stream. * @param os the output stream. * @throws IOException in case of read or write problems. */ public static void shoveAndClose( final InputStream is, final OutputStream os) throws IOException { try { shove(is, os); } finally { close(is); close(os); } }
@Override public void read(InputStream aInputStream) throws IOException { try { Util.shove(aInputStream, target); } finally { Util.close(aInputStream); } } }
@Override public void storeBinary(String aContextId, String aKey, final InputStream aStream) { try { storeBinary(aContextId, aKey, new StreamWriter() { @Override public void write(OutputStream aOs) throws Exception { Util.shoveAndClose(aStream, aOs); } }); } finally { Util.close(aStream); } }
/** * Makes the given stream available as a file. The created file is temporary * and deleted upon normal termination of the JVM. Still the file should be * deleted as soon as possible if it is no longer required. In case the JVM * crashes the file would not be deleted. The source stream is closed by * this operation in all cases. * * @param is * the source. * @return the file. * @throws IOException * in case of read or write problems. */ public static File getStreamAsFile(final InputStream is) throws IOException { OutputStream os = null; try { final File f = File.createTempFile("lab_stream", "tmp"); f.deleteOnExit(); os = new FileOutputStream(f); shove(is, os); return f; } finally { close(os); close(is); } }
@Override public void storeBinary(String aContextId, String aKey, final InputStream aStream) { try { storeBinary(aContextId, aKey, new StreamWriter() { @Override public void write(OutputStream aOs) throws Exception { Util.shoveAndClose(aStream, aOs); } }); } finally { Util.close(aStream); } }
/** * Makes the given stream available as a file. The created file is temporary * and deleted upon normal termination of the JVM. Still the file should be * deleted as soon as possible if it is no longer required. In case the JVM * crashes the file would not be deleted. The source stream is closed by * this operation in all cases. * * @param is * the source. * @return the file. * @throws IOException * in case of read or write problems. */ public static File getStreamAsFile(final InputStream is) throws IOException { OutputStream os = null; try { final File f = File.createTempFile("lab_stream", "tmp"); f.deleteOnExit(); os = new FileOutputStream(f); shove(is, os); return f; } finally { close(os); close(is); } }
@Override public void write(OutputStream aStream) throws IOException { InputStream is = null; try { if (urlSource != null) { is = urlSource.openStream(); } else if (fileSource != null) { is = new FileInputStream(fileSource); } else { throw new IllegalStateException("Neither URL nor file source has been set"); } Util.shove(is, aStream); } finally { Util.close(is); } } }
@Override public void write(OutputStream aStream) throws IOException { InputStream is = null; try { if (urlSource != null) { is = urlSource.openStream(); } else if (fileSource != null) { is = new FileInputStream(fileSource); } else { throw new IllegalStateException("Neither URL nor file source has been set"); } Util.shove(is, aStream); } finally { Util.close(is); } } }
public static <T extends StreamReader> T retrieveBinary(final File aFile, final T aConsumer) { InputStream is = null; try { is = new FileInputStream(aFile); if (aFile.getName().toLowerCase().endsWith(".gz")) { is = new GZIPInputStream(is); } aConsumer.read(is); return aConsumer; } catch (IOException e) { throw new DataAccessResourceFailureException(e.getMessage(), e); } finally { Util.close(is); } }
public static <T extends StreamReader> T retrieveBinary(final File aFile, final T aConsumer) { InputStream is = null; try { is = new FileInputStream(aFile); if (aFile.getName().toLowerCase().endsWith(".gz")) { is = new GZIPInputStream(is); } aConsumer.read(is); return aConsumer; } catch (IOException e) { throw new DataAccessResourceFailureException(e.getMessage(), e); } finally { Util.close(is); } }
@Override public void storeBinary(String aContextId, String aKey, StreamWriter aProducer) { OutputStream os = null; try { File f = new File(getContextFolder(aContextId, false), aKey); f.getParentFile().mkdirs(); // Necessary if the key addresses a sub-directory log.debug("Storing to: " + f); os = new FileOutputStream(f); if (aKey.endsWith(".gz")) { os = new GZIPOutputStream(os); } aProducer.write(os); } catch (Exception e) { throw new DataAccessResourceFailureException(e.getMessage(), e); } finally { Util.close(os); } }
@Override public void storeBinary(String aContextId, String aKey, StreamWriter aProducer) { OutputStream os = null; try { File f = new File(getContextFolder(aContextId, false), aKey); f.getParentFile().mkdirs(); // Necessary if the key addresses a sub-directory log.debug("Storing to: " + f); os = new FileOutputStream(f); if (aKey.endsWith(".gz")) { os = new GZIPOutputStream(os); } aProducer.write(os); } catch (Exception e) { throw new DataAccessResourceFailureException(e.getMessage(), e); } finally { Util.close(os); } }
@Override public <T extends StreamReader> T retrieveBinary(String aContextId, String aKey, T aConsumer) { InputStream is = null; try { is = new FileInputStream(new File(getContextFolder(aContextId, true), aKey)); if (aKey.endsWith(".gz")) { is = new GZIPInputStream(is); } aConsumer.read(is); return aConsumer; } catch (IOException e) { throw new DataAccessResourceFailureException("Unable to access [" + aKey + "] in context [" + aContextId + "]", e); } catch (Throwable e) { throw new DataAccessResourceFailureException("Unable to load [" + aKey + "] from context [" + aContextId + "]", e); } finally { Util.close(is); } }
@Override public <T extends StreamReader> T retrieveBinary(String aContextId, String aKey, T aConsumer) { InputStream is = null; try { is = new FileInputStream(new File(getContextFolder(aContextId, true), aKey)); if (aKey.endsWith(".gz")) { is = new GZIPInputStream(is); } aConsumer.read(is); return aConsumer; } catch (IOException e) { throw new DataAccessResourceFailureException("Unable to access [" + aKey + "] in context [" + aContextId + "]", e); } catch (Throwable e) { throw new DataAccessResourceFailureException("Unable to load [" + aKey + "] from context [" + aContextId + "]", e); } finally { Util.close(is); } }