List of usage examples for com.google.common.io Closer close
@Override public void close() throws IOException
From source file:com.tinspx.util.io.ByteUtils.java
@ThreadLocalArray(8192) public static byte[] toByteArray(@NonNull File file) throws IOException { final int size = Ints.checkedCast(file.length()); Closer closer = Closer.create(); try {/*w w w . j a v a 2 s. com*/ return toByteArray(closer.register(new FileInputStream(file)), size); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:com.tinspx.util.io.ByteUtils.java
@ThreadLocalArray(8192) public static boolean contentEquals(@NonNull ByteSource source, @NonNull @WillNotClose InputStream in) throws IOException { final Closer closer = Closer.create(); try {/* w ww.j a v a 2s .c o m*/ return contentEquals(closer.register(source.openStream()), in); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.tinspx.util.io.ByteUtils.java
@ThreadLocalArray(8192) public static boolean contentEquals(@NonNull ByteSource source1, @NonNull ByteBuffer buffer) throws IOException { final Closer closer = Closer.create(); try {//from w w w . j ava2 s .co m return contentEquals(closer.register(source1.openStream()), buffer); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:tachyon.shell.command.CopyFromLocalCommand.java
private void copyPath(File src, TachyonURI dstPath) throws IOException { try {/* w w w . j a v a 2 s.c o m*/ if (!src.isDirectory()) { // If the dstPath is a directory, then it should be updated to be the path of the file where // src will be copied to if (mTfs.exists(dstPath) && mTfs.getStatus(dstPath).isFolder()) { dstPath = dstPath.join(src.getName()); } Closer closer = Closer.create(); FileOutStream os = null; try { os = closer.register(mTfs.createFile(dstPath)); FileInputStream in = closer.register(new FileInputStream(src)); FileChannel channel = closer.register(in.getChannel()); ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } } catch (IOException e) { // Close the out stream and delete the file, so we don't have an incomplete file lying // around if (os != null) { os.cancel(); if (mTfs.exists(dstPath)) { mTfs.delete(dstPath); } } throw e; } finally { closer.close(); } } else { mTfs.createDirectory(dstPath); List<String> errorMessages = Lists.newArrayList(); String[] fileList = src.list(); for (String file : fileList) { TachyonURI newPath = new TachyonURI(dstPath, new TachyonURI(file)); File srcFile = new File(src, file); try { copyPath(srcFile, newPath); } catch (IOException e) { errorMessages.add(e.getMessage()); } } if (errorMessages.size() != 0) { if (errorMessages.size() == fileList.length) { // If no files were created, then delete the directory if (mTfs.exists(dstPath)) { mTfs.delete(dstPath); } } throw new IOException(Joiner.on('\n').join(errorMessages)); } } } catch (TachyonException e) { throw new IOException(e.getMessage()); } }
From source file:com.tinspx.util.io.ByteUtils.java
/** * Used instead of {@link #toByteArray(ByteSource)} when the size of the * {@code ByteSource} is known and {@link ByteSource#size() size()} should * not be called on {@code source}./*from ww w . j a v a 2 s .co m*/ */ @ThreadLocalArray(8192) public static byte[] toByteArray(ByteSource source, int expectedSize) throws IOException { final Closer closer = Closer.create(); try { return toByteArray(closer.register(source.openStream()), expectedSize); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
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 {// w w w . ja v a 2s . c o m return contentEquals(closer.register(source.openStream()), bytes, off, len); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.android.builder.internal.packaging.OldPackager.java
/** * Creates a new instance./* w ww.j a va 2s . c om*/ * * <p>This creates a new builder that will create the specified output file. * * @param creationData APK creation data * @param resLocation location of the zip with the resources, if any * @param logger the logger * @throws PackagerException failed to create the initial APK * @throws IOException failed to create the APK */ public OldPackager(@NonNull ApkCreatorFactory.CreationData creationData, @Nullable String resLocation, @NonNull ILogger logger) throws PackagerException, IOException { checkOutputFile(creationData.getApkPath()); Closer closer = Closer.create(); try { checkOutputFile(creationData.getApkPath()); File resFile = null; if (resLocation != null) { resFile = new File(resLocation); checkInputFile(resFile); } mLogger = logger; ApkCreatorFactory factory = new SignedJarApkCreatorFactory(); mApkCreator = factory.make(creationData); mLogger.verbose("Packaging %s", creationData.getApkPath().getName()); // add the resources if (resFile != null) { addZipFile(resFile); } } catch (Throwable e) { closer.register(mApkCreator); mApkCreator = null; throw closer.rethrow(e, PackagerException.class); } finally { closer.close(); } }
From source file:com.tinspx.util.io.ByteUtils.java
@ThreadLocalArray(8192) static boolean contentEqualsImpl(@NonNull ByteSource source1, @NonNull ByteSource source2) throws IOException { final Closer closer = Closer.create(); try {/*from w w w. j a v a2s. c o m*/ return contentEquals(closer.register(source1.openStream()), closer.register(source2.openStream())); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.apache.gobblin.metastore.FsStateStore.java
@Override @SuppressWarnings("unchecked") public List<T> getAll(String storeName, String tableName) throws IOException { List<T> states = Lists.newArrayList(); Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName); if (!this.fs.exists(tablePath)) { return states; }// ww w. j av a2s. co m Closer closer = Closer.create(); try { @SuppressWarnings("deprecation") GobblinSequenceFileReader reader = closer .register(new GobblinSequenceFileReader(this.fs, tablePath, this.conf)); try { Text key = new Text(); T state = this.stateClass.newInstance(); while (reader.next(key)) { state = (T) reader.getCurrentValue(state); states.add(state); // We need a new object for each read state state = this.stateClass.newInstance(); } } catch (Exception e) { throw new IOException( "failure retrieving state from storeName " + storeName + " tableName " + tableName, e); } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } return states; }
From source file:org.apache.gobblin.runtime.JobLauncherTestHelper.java
public void runTestWithCancellation(final Properties jobProps) throws Exception { String jobName = jobProps.getProperty(ConfigurationKeys.JOB_NAME_KEY); String jobId = JobLauncherUtils.newJobId(jobName).toString(); jobProps.setProperty(ConfigurationKeys.JOB_ID_KEY, jobId); Closer closer = Closer.create(); try {/*from w w w . java2s . com*/ final JobLauncher jobLauncher = closer .register(JobLauncherFactory.newJobLauncher(this.launcherProps, jobProps)); final AtomicBoolean isCancelled = new AtomicBoolean(false); // This thread will cancel the job after some time Thread thread = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(500); jobLauncher.cancelJob(null); isCancelled.set(true); } catch (Exception je) { // Ignored } } }); thread.start(); jobLauncher.launchJob(null); Assert.assertTrue(isCancelled.get()); } finally { closer.close(); } List<JobState.DatasetState> datasetStateList = this.datasetStateStore.getAll(jobName, sanitizeJobNameForDatasetStore(jobId) + ".jst"); Assert.assertTrue(datasetStateList.isEmpty()); }