List of usage examples for com.google.common.io Closer register
public <C extends Closeable> C register(@Nullable C closeable)
From source file:alluxio.client.block.stream.BlockInStream.java
/** * Creates an instance of {@link BlockInStream}. * * This method keeps polling the block worker until the block is cached to Alluxio or * it successfully acquires a UFS read token with a timeout. * (1) If the block is cached to Alluxio after polling, it returns {@link BlockInStream} * to read the block from Alluxio storage. * (2) If a UFS read token is acquired after polling, it returns {@link BlockInStream} * to read the block from an Alluxio worker that reads the block from UFS. * (3) If the polling times out, an {@link IOException} with cause * {@link alluxio.exception.UfsBlockAccessTokenUnavailableException} is thrown. * * @param context the file system context * @param ufsPath the UFS path/* www . j av a 2 s . c o m*/ * @param blockId the block ID * @param blockSize the block size * @param blockStart the position at which the block starts in the file * @param workerNetAddress the worker network address * @param options the options * @throws IOException if it fails to create an instance * @return the {@link BlockInStream} created */ // TODO(peis): Use options idiom (ALLUXIO-2579). public static BlockInStream createUfsBlockInStream(FileSystemContext context, String ufsPath, long blockId, long blockSize, long blockStart, WorkerNetAddress workerNetAddress, InStreamOptions options) throws IOException { Closer closer = Closer.create(); try { BlockWorkerClient blockWorkerClient = closer .register(context.createBlockWorkerClient(workerNetAddress)); LockBlockOptions lockBlockOptions = LockBlockOptions.defaults().setUfsPath(ufsPath) .setOffset(blockStart).setBlockSize(blockSize) .setMaxUfsReadConcurrency(options.getMaxUfsReadConcurrency()); LockBlockResult lockBlockResult = closer .register(blockWorkerClient.lockUfsBlock(blockId, lockBlockOptions)).getResult(); PacketInStream inStream; if (lockBlockResult.getLockBlockStatus().blockInAlluxio()) { boolean local = blockWorkerClient.getDataServerAddress().getHostName() .equals(NetworkAddressUtils.getClientHostName()); if (local && Configuration.getBoolean(PropertyKey.USER_SHORT_CIRCUIT_ENABLED)) { inStream = closer.register(PacketInStream .createLocalPacketInStream(lockBlockResult.getBlockPath(), blockId, blockSize)); } else { inStream = closer.register(PacketInStream.createNettyPacketInStream(context, blockWorkerClient.getDataServerAddress(), blockId, lockBlockResult.getLockId(), blockWorkerClient.getSessionId(), blockSize, false, Protocol.RequestType.ALLUXIO_BLOCK)); } blockWorkerClient.accessBlock(blockId); } else { Preconditions.checkState(lockBlockResult.getLockBlockStatus().ufsTokenAcquired()); inStream = closer.register( PacketInStream.createNettyPacketInStream(context, blockWorkerClient.getDataServerAddress(), blockId, lockBlockResult.getLockId(), blockWorkerClient.getSessionId(), blockSize, !options.getAlluxioStorageType().isStore(), Protocol.RequestType.UFS_BLOCK)); } return new BlockInStream(inStream, blockWorkerClient, closer, options); } catch (AlluxioException | IOException e) { CommonUtils.closeQuietly(closer); throw CommonUtils.castToIOException(e); } }
From source file:tachyon.client.file.FileSystemUtils.java
/** * Persist the given file to the under file system. * * @param fs {@link FileSystem} to carry out Tachyon operations * @param uri the uri of the file to persist * @param status the status info of the file * @param tachyonConf TachyonConf object * @return the size of the file persisted * @throws IOException if an I/O error occurs * @throws FileDoesNotExistException if the given file does not exist * @throws TachyonException if an unexpected Tachyon error occurs *///from ww w . j a v a 2 s .c om public static long persistFile(FileSystem fs, TachyonURI uri, URIStatus status, TachyonConf tachyonConf) throws IOException, FileDoesNotExistException, TachyonException { // TODO(manugoyal) move this logic to the worker, as it deals with the under file system Closer closer = Closer.create(); long ret; try { OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE); FileInStream in = closer.register(fs.openFile(uri, options)); TachyonURI dstPath = new TachyonURI(status.getUfsPath()); UnderFileSystem ufs = UnderFileSystem.get(dstPath.toString(), tachyonConf); String parentPath = dstPath.getParent().toString(); if (!ufs.exists(parentPath) && !ufs.mkdirs(parentPath, true)) { throw new IOException("Failed to create " + parentPath); } OutputStream out = closer.register(ufs.create(dstPath.getPath())); ret = IOUtils.copyLarge(in, out); } catch (Exception e) { throw closer.rethrow(e); } finally { closer.close(); } // Tell the master to mark the file as persisted fs.setAttribute(uri, SetAttributeOptions.defaults().setPersisted(true)); return ret; }
From source file:com.fullcontact.sstable.index.SSTableIndexIndex.java
/** * Read an existing index. Reads and returns the index index, which is a list of chunks defined by the Cassandra * Index.db file along with the configured split size. * * @param fileSystem Hadoop file system. * @param sstablePath SSTable Index.db.//from www. j ava2 s. c om * @return Index of chunks. * @throws IOException */ public static SSTableIndexIndex readIndex(final FileSystem fileSystem, final Path sstablePath) throws IOException { final Closer closer = Closer.create(); final Path indexPath = sstablePath.suffix(SSTABLE_INDEX_SUFFIX); // Detonate if we don't have an index. final FSDataInputStream inputStream = closer.register(fileSystem.open(indexPath)); final SSTableIndexIndex indexIndex = new SSTableIndexIndex(); try { while (inputStream.available() != 0) { indexIndex.add(inputStream.readLong(), inputStream.readLong()); } } finally { closer.close(); } return indexIndex; }
From source file:zipkin.collector.zookeeper.ZooKeeperCollectorSampler.java
/** read-only */ static AtomicInteger targetStoreRate(CuratorFramework client, Builder builder, Closer closer) { String targetStoreRatePath = ensureExists(client, builder.basePath + "/targetStoreRate"); NodeCache cache = closer.register(new NodeCache(client, targetStoreRatePath)); try {/*from w w w . j av a 2s . c o m*/ cache.start(); } catch (Exception e) { throw new IllegalStateException("Error starting cache for " + targetStoreRatePath, e); } AtomicInteger targetStoreRate = new AtomicInteger(); cache.getListenable().addListener(() -> { byte[] bytes = cache.getCurrentData().getData(); if (bytes.length == 0) return; try { targetStoreRate.set(Integer.valueOf(new String(bytes, Util.UTF_8))); } catch (NumberFormatException e) { log.warn("Error parsing target store rate {}", e.getMessage()); return; } }); return targetStoreRate; }
From source file:com.google.devtools.build.android.desugar.scan.KeepScanner.java
/** * Transform a list of Path to a list of InputFileProvider and register them with the given * closer./*from ww w . j a v a2 s . c o m*/ */ @SuppressWarnings("MustBeClosedChecker") private static ImmutableList<InputFileProvider> toRegisteredInputFileProvider(Closer closer, List<Path> paths) throws IOException { ImmutableList.Builder<InputFileProvider> builder = new ImmutableList.Builder<>(); for (Path path : paths) { builder.add(closer.register(InputFileProvider.open(path))); } return builder.build(); }
From source file:com.facebook.buck.rules.MergeAndroidResourcesStep.java
public static String generateJavaCodeForPackageAndResources(String packageName, SortedSet<Resource> resources) { StringBuilder b = new StringBuilder(); Closer closer = Closer.create(); PrintWriter writer = closer.register(new PrintWriter(CharStreams.asWriter(b))); try {// w ww.j a v a 2 s.c om writeJavaCodeForPackageAndResources(writer, packageName, resources); } catch (IOException e) { // Impossible. throw new RuntimeException(e); } finally { try { closer.close(); } catch (IOException e) { Throwables.propagate(e); } } return b.toString(); }
From source file:com.android.builder.files.IncrementalRelativeFileSets.java
/** * Reads a zip file and adds all files in the file in a new incremental relative set. The * status of each file is set to {@code status}. * * @param zip the zip file to read, must be a valid, existing zip file * @param status the status to set the files to * @return the file set/*from w w w . j a va 2 s .co m*/ * @throws IOException failed to read the zip file */ @NonNull public static ImmutableMap<RelativeFile, FileStatus> fromZip(@NonNull File zip, FileStatus status) throws IOException { Preconditions.checkArgument(zip.isFile(), "!zip.isFile()"); Set<RelativeFile> files = Sets.newHashSet(); Closer closer = Closer.create(); try { ZFile zipReader = closer.register(new ZFile(zip)); for (StoredEntry entry : zipReader.entries()) { if (entry.getType() == StoredEntryType.FILE) { File file = new File(zip, FileUtils.toSystemDependentPath(entry.getCentralDirectoryHeader().getName())); files.add(new RelativeFile(zip, file)); } } } catch (Throwable t) { throw closer.rethrow(t, IOException.class); } finally { closer.close(); } Map<RelativeFile, FileStatus> map = Maps.asMap(files, Functions.constant(status)); return ImmutableMap.copyOf(map); }
From source file:net.usikkert.kouchat.android.util.FileUtils.java
private static void copyFileToDevice(final File fileToStore, final Instrumentation instrumentation) { final Closer closer = Closer.create(); final AssetManager assets = instrumentation.getContext().getResources().getAssets(); try {/*from w w w . jav a2s . com*/ final InputStream inputStream = closer.register(assets.open(fileToStore.getName())); final FileOutputStream outputStream = closer.register(new FileOutputStream(fileToStore)); ByteStreams.copy(inputStream, outputStream); outputStream.flush(); assertTrue("Should exist: " + fileToStore, fileToStore.exists()); } catch (IOException e) { throw new RuntimeException(e); } finally { try { closer.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.glowroot.agent.live.JvmTool.java
private static <T> T runExternalAttach(long pid, String methodName, InputStreamProcessor<T> processor, @Nullable File glowrootJarFile) throws Exception { List<String> command = buildCommand(pid, methodName, glowrootJarFile); ProcessBuilder processBuilder = new ProcessBuilder(command); Process process = processBuilder.start(); Closer closer = Closer.create(); ErrorStreamReader errorStreamReader; T result = null;//from www. j a v a 2 s . co m Exception processingException = null; try { InputStream in = closer.register(process.getInputStream()); InputStream err = closer.register(process.getErrorStream()); errorStreamReader = new ErrorStreamReader(err); Thread errorStreamReaderThread = new Thread(errorStreamReader); errorStreamReaderThread.setName("Glowroot-JVM-Tool-Error-Stream-Reader"); errorStreamReaderThread.setDaemon(true); errorStreamReaderThread.start(); try { result = processAndClose(in, processor); } catch (Exception e) { processingException = e; } catch (Throwable t) { processingException = new RuntimeException(t); } errorStreamReaderThread.join(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } int status = process.waitFor(); if (status == UNAVAILABLE_DUE_TO_RUNNING_IN_JRE_STATUS) { throw new UnavailableDueToRunningInJreException(); } else if (status == UNAVAILABLE_PROBABLY_DUE_TO_DOCKER_PID_ONE_STATUS) { throw new UnavailableDueToDockerAlpinePidOneException(); } else if (status != 0) { logger.error("error occurred while trying to run jvm tool:\n{}\n{}", Joiner.on(' ').join(command), errorStreamReader.getOutput().trim()); throw new IllegalStateException("Error occurred while trying to run jvm tool"); } if (result == null) { throw checkNotNull(processingException); } return result; }
From source file:com.complexible.common.io.Files2.java
public static void copyDirectory(final File theSource, final File theDest) throws IOException { if (theSource.isDirectory()) { if (!theDest.exists()) { if (!theDest.mkdir()) { throw new IOException("Could not create destination"); }//from w ww . j ava2 s . c o m } String[] aChildren = theSource.list(); for (String aChild : aChildren) { copyDirectory(new File(theSource, aChild), new File(theDest, aChild)); } } else { Closer aCloser = Closer.create(); InputStream in = new FileInputStream(theSource); OutputStream out = new FileOutputStream(theDest); aCloser.register(in); aCloser.register(out); ByteStreams.copy(in, out); aCloser.close(); } }