List of usage examples for com.google.common.io Closer create
public static Closer create()
From source file:org.ow2.proactive.scheduler.rest.SchedulerClient.java
@Override public void renewSession() throws NotConnectedException { Closer closer = Closer.create(); try {//from ww w. j av a 2 s . c om LoginForm loginForm = new LoginForm(); loginForm.setUsername(connectionInfo.getLogin()); loginForm.setPassword(connectionInfo.getPassword()); if (connectionInfo.getCredentialFile() != null) { FileInputStream inputStream = new FileInputStream(connectionInfo.getCredentialFile()); closer.register(inputStream); loginForm.setCredential(inputStream); } sid = restApi().loginOrRenewSession(sid, loginForm); } catch (Exception e) { throw new RuntimeException(e); } finally { try { closer.close(); } catch (IOException e) { // ignore } } }
From source file:com.tinspx.util.io.ByteUtils.java
/** * Copies at most {@code limit} bytes from {@code from} into {@code to}, * returning the total number of bytes copied. Byte processing stops on EOF * or when {@link ByteProcessor#processBytes(byte[], int, int) processBytes} * returns {@code false}./*from www . j a va2s .c o m*/ * * @param from the source to read bytes from * @param to the destination to copy bytes read from {@code from} into * @param limit the maximum number of bytes to copy * @return the total number of bytes copied from {@code from} to {@code to} * @throws IOException if an IOException occurs * @throws NullPointerException if either {@code from} or {@code to} is null * @throws IllegalArgumentException if {@code limit} is negative */ @ThreadLocalArray(8192) public static long copy(@NonNull ByteSource from, @NonNull ByteProcessor<?> to, long limit) throws IOException { checkLimit(limit); final Closer closer = Closer.create(); try { return copy(closer.register(from.openStream()), to, limit); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.apache.gobblin.runtime.AbstractJobLauncher.java
/** * Cleanup the left-over staging data possibly from the previous run of the job that may have failed * and not cleaned up its staging data.// w w w. j a va2s.c om * * Property {@link ConfigurationKeys#CLEANUP_STAGING_DATA_PER_TASK} controls whether to cleanup * staging data per task, or to cleanup entire job's staging data at once. * * Staging data will not be cleaned if the job has unfinished {@link CommitSequence}s. */ private void cleanLeftoverStagingData(WorkUnitStream workUnits, JobState jobState) throws JobException { if (jobState.getPropAsBoolean(ConfigurationKeys.CLEANUP_STAGING_DATA_BY_INITIALIZER, false)) { //Clean up will be done by initializer. return; } try { if (!canCleanStagingData(jobState)) { LOG.error("Job " + jobState.getJobName() + " has unfinished commit sequences. Will not clean up staging data."); return; } } catch (IOException e) { throw new JobException("Failed to check unfinished commit sequences", e); } try { if (this.jobContext.shouldCleanupStagingDataPerTask()) { if (workUnits.isSafeToMaterialize()) { Closer closer = Closer.create(); Map<String, ParallelRunner> parallelRunners = Maps.newHashMap(); try { for (WorkUnit workUnit : JobLauncherUtils .flattenWorkUnits(workUnits.getMaterializedWorkUnitCollection())) { JobLauncherUtils.cleanTaskStagingData(new WorkUnitState(workUnit, jobState), LOG, closer, parallelRunners); } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } else { throw new RuntimeException("Work unit streams do not support cleaning staging data per task."); } } else { if (jobState.getPropAsBoolean(ConfigurationKeys.CLEANUP_OLD_JOBS_DATA, ConfigurationKeys.DEFAULT_CLEANUP_OLD_JOBS_DATA)) { JobLauncherUtils.cleanUpOldJobData(jobState, LOG, jobContext.getStagingDirProvided(), jobContext.getOutputDirProvided()); } JobLauncherUtils.cleanJobStagingData(jobState, LOG); } } catch (Throwable t) { // Catch Throwable instead of just IOException to make sure failure of this won't affect the current run LOG.error("Failed to clean leftover staging data", t); } }
From source file:com.tinspx.util.io.ByteUtils.java
/** * Copies as many bytes as possible from {@code from} into {@code to}, * returning the total number of bytes copied. * /*from w ww . java 2s . c o m*/ * @param from the source to read bytes from * @param to the destination to copy bytes read from {@code from} into * @return the total number of bytes copied from {@code from} to {@code to} * @throws IOException if an IOException occurs * @throws NullPointerException if either {@code from} or {@code to} is null */ @ThreadLocalArray(8192) public static int copy(@NonNull ByteSource from, @NonNull ByteBuffer to) throws IOException { final Closer closer = Closer.create(); try { return copy(closer.register(from.openStream()), to); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.tinspx.util.io.ByteUtils.java
/** * Copies the entire contents of {@code from} into {@code to}. * * @param from the source to read bytes from * @param to the destination to copy bytes read from {@code from} into * @throws IOException if an IOException occurs * @throws NullPointerException if either {@code from} or {@code to} is null *//*from ww w . j a v a 2 s. co m*/ @ThreadLocalArray(8192) public static void copy(@NonNull ByteBuffer from, @NonNull ByteSink to) throws IOException { final Closer closer = Closer.create(); try { OutputStream out = closer.register(to.openStream()); copy(from, out); out.flush(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.tinspx.util.io.ByteUtils.java
/** * Copies the entire contents of {@code from}, starting at index {@code off} * (inclusive) and ending at index {@code off + len} (exclusive), into * {@code to}. This provides an alternative to * {@link ByteSink#write(byte[])}.//w w w . jav a2 s.c om * * @param from the source to read bytes from * @param off the offset into {@code from} to start copying * @param len the number of bytes to copy starting at index {@code off} * @param to the destination to copy bytes read from {@code from} into * @throws IOException if an IOException occurs * @throws NullPointerException if either {@code from} or {@code to} is null * @see ByteSink#write(byte[]) */ public static void copy(byte[] from, int off, int len, @NonNull ByteSink to) throws IOException { checkPositionIndexes(off, off + len, from.length); final Closer closer = Closer.create(); try { OutputStream out = closer.register(to.openStream()); out.write(from, off, len); out.flush(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.apache.gobblin.runtime.AbstractJobLauncher.java
private static void cleanupStagingDataPerTask(JobState jobState) { Closer closer = Closer.create(); Map<String, ParallelRunner> parallelRunners = Maps.newHashMap(); try {/*from ww w. j a v a2 s . c om*/ for (TaskState taskState : jobState.getTaskStates()) { try { JobLauncherUtils.cleanTaskStagingData(taskState, LOG, closer, parallelRunners); } catch (IOException e) { LOG.error( String.format("Failed to clean staging data for task %s: %s", taskState.getTaskId(), e), e); } } } finally { try { closer.close(); } catch (IOException e) { LOG.error("Failed to clean staging data", e); } } }
From source file:com.tinspx.util.io.ByteUtils.java
@ThreadLocalArray(8192) static boolean contentEqualsImpl(@NonNull ByteSource source, byte[] bytes, int off, int len) throws IOException { checkPositionIndexes(off, off + len, bytes.length); final Closer closer = Closer.create(); try {/*from www . ja v a2s . co m*/ return contentEquals(closer.register(source.openStream()), bytes, off, len); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.pantsbuild.tools.jar.JarBuilder.java
private void enumerateJarEntries(File jarFile, JarEntryVisitor visitor) throws IOException { Closer jarFileCloser = Closer.create(); JarFile jar = JarFileUtil.openJarFile(jarFileCloser, jarFile); try {//from ww w. ja v a2 s . c o m for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) { visitor.visit(entries.nextElement()); } } catch (IOException e) { throw jarFileCloser.rethrow(e); } finally { jarFileCloser.close(); } }