List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:com.uber.hoodie.common.table.timeline.HoodieArchivedTimeline.java
public HoodieArchivedTimeline(FileSystem fs, String metaPath) { // Read back the commits to make sure Path archiveLogPath = getArchiveLogPath(metaPath); try {/*from w w w .j av a 2 s . co m*/ SequenceFile.Reader reader = new SequenceFile.Reader(fs.getConf(), SequenceFile.Reader.file(archiveLogPath)); try { Text key = new Text(); Text val = new Text(); while (reader.next(key, val)) { // TODO - limit the number of commits loaded in memory. this could get very large. // This is okay because only tooling will load the archived commit timeline today readCommits.put(key.toString(), Arrays.copyOf(val.getBytes(), val.getLength())); } this.instants = readCommits.keySet().stream() .map(s -> new HoodieInstant(false, HoodieTimeline.COMMIT_ACTION, s)) .collect(Collectors.toList()); } finally { Closeables.closeQuietly(reader); } } catch (IOException e) { throw new HoodieIOException("Could not load archived commit timeline from path " + archiveLogPath, e); } // multiple casts will make this lambda serializable - http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.16 this.details = (Function<HoodieInstant, Optional<byte[]>> & Serializable) this::getInstantDetails; this.fs = fs; this.metaPath = metaPath; }
From source file:com.google.android.testing.nativedriver.client.AdbConnection.java
/** * Performs the {@code ioctl} command on device corresponding to the given * filename. This is equivalent to/*from w w w .j a v a 2s . c om*/ * {@code adb shell ioctl -rl (buffer.length) (filename) (requestCode)}. * * @param filename the name of the device file to perform the request on * @param requestCode the request code to perform * @param length the amount of bytes to expect from the request * @return a byte array the size of {@code length} that contains the results * of the request */ public byte[] doIoctlForReading(String filename, int requestCode, int length) { byte[] buffer = new byte[length]; Process adbProcess = runAdb("shell", "ioctl", "-rl", "" + buffer.length, filename, "" + requestCode); String rawOutput = outputAsString(adbProcess); Closeables.closeQuietly(adbProcess.getInputStream()); confirmExitValueIs(0, adbProcess); int returnedDataIndex = rawOutput.indexOf(IOCTL_RETURNBUFFERHEADER); if (returnedDataIndex == -1) { throw new AdbException("Could not find the return data header in ioctl output."); } rawOutput = rawOutput.substring(returnedDataIndex + IOCTL_RETURNBUFFERHEADER.length()); // Now rawOutput is a string in the form of "xx xx xx ...", where "xx" is a // hexadecimal byte. So we increment stringIndex at each iteration by 3 // because of this format: 2 hex digits, one space, repeated. for (int byteIndex = 0, stringIndex = 0; byteIndex < buffer.length; byteIndex++, stringIndex += 3) { int nextByte = Integer.parseInt(rawOutput.substring(stringIndex, stringIndex + 2), 16); buffer[byteIndex] = (byte) nextByte; } return buffer; }
From source file:org.renjin.primitives.io.serialization.RDataReader.java
public static boolean isRDataFile(ByteSource inputSupplier) throws IOException { InputStream in = inputSupplier.openStream(); try {//from w w w .j av a 2 s . co m byte streamType = readStreamType(in); return streamType != -1; } finally { Closeables.closeQuietly(in); } }
From source file:com.memonews.mahout.sentiment.SentimentModelHelper.java
Vector encodeFeatureVector(final File file, final Multiset<String> overallCounts) throws IOException { final Multiset<String> words = ConcurrentHashMultiset.create(); final BufferedReader reader = Files.newReader(file, Charsets.UTF_8); try {/*ww w. j av a 2s. co m*/ countWords(analyzer, words, reader, overallCounts); } finally { Closeables.closeQuietly(reader); } final Vector v = new RandomAccessSparseVector(FEATURES); bias.addToVector("", 1, v); for (final String word : words.elementSet()) { encoder.addToVector(word, Math.log1p(words.count(word)), v); } return v; }
From source file:com.metamx.druid.utils.CompressionUtils.java
public static void unzip(InputStream in, File outDir) throws IOException { ZipInputStream zipIn = new ZipInputStream(in); ZipEntry entry;/* w w w . j ava 2 s. c om*/ while ((entry = zipIn.getNextEntry()) != null) { FileOutputStream out = null; try { out = new FileOutputStream(new File(outDir, entry.getName())); ByteStreams.copy(zipIn, out); zipIn.closeEntry(); out.close(); } finally { Closeables.closeQuietly(out); } } }
From source file:com.nesscomputing.hbase.spill.SpilledFile.java
public List<Put> load() throws IOException { InputStream is = null;/*from w ww .j a va 2s .c o m*/ final ImmutableList.Builder<Put> builder = ImmutableList.builder(); try { is = new BufferedInputStream(new FileInputStream(file)); final int skippedBytes = 4 + 4 + 8; // int, int, long Preconditions.checkState(skippedBytes == is.skip(skippedBytes), "skipped byte mismatch (you are in trouble...)"); Put put = null; while ((put = BinaryConverter.PUT_READ_FUNCTION.apply(is)) != null) { builder.add(put); } final List<Put> result = builder.build(); Preconditions.checkState(result.size() == elements, "The preamble reported %d elements, but %d were found!", elements, result.size()); return result; } finally { Closeables.closeQuietly(is); } }
From source file:com.netflix.exhibitor.core.index.IndexBuilder.java
@Override public void close() throws IOException { Closeables.closeQuietly(writer); Closeables.closeQuietly(niofsDirectory); }
From source file:org.jclouds.blobstore.GetPath.java
private static void copyDirectoryToDestination(BlobStoreContext context, String container, String directory, File destinationDir) throws FileNotFoundException, IOException { InputStream input = null;/*from ww w . j a va 2s. co m*/ try { checkState(context.getBlobStore().containerExists(container), String.format("source container %s does not exist", directory, container)); checkState(context.getBlobStore().directoryExists(container, directory), String.format("source directory %s does not exist in container %s", directory, container)); String path = container + "/" + directory; InputStreamMap map = context.createInputStreamMap(path); System.out.printf("fetching %d entries from %s %s%n", map.size(), context.getProviderSpecificContext().getIdentity(), path); for (Entry<String, InputStream> entry : map.entrySet()) { System.out.printf("getting file: %s/%s%n", path, entry.getKey()); input = entry.getValue(); File file = new File(destinationDir, entry.getKey()); OutputStream out = new FileOutputStream(file); ByteStreams.copy(input, out); out.flush(); out.close(); } } finally { // Close connection Closeables.closeQuietly(input); } }
From source file:org.sonar.batch.local.DryRunExporter.java
private void exportResults(Collection<Resource> resources) { File exportFile = new File(fileSystem.workingDir(), settings.getString("sonar.dryRun.export.path")); LOG.info("Exporting DryRun results to " + exportFile.getAbsolutePath()); Writer output = null;//from w ww .j a va 2 s . c om try { output = new BufferedWriter(new FileWriter(exportFile)); writeJson(resources, output); } catch (IOException e) { throw new SonarException("Unable to write DryRun results in file " + exportFile.getAbsolutePath(), e); } finally { Closeables.closeQuietly(output); } }
From source file:org.apache.mahout.cf.taste.hadoop.item.UserVectorSplitterMapper.java
@Override protected void setup(Context context) throws IOException { Configuration jobConf = context.getConfiguration(); maxPrefsPerUserConsidered = jobConf.getInt(MAX_PREFS_PER_USER_CONSIDERED, DEFAULT_MAX_PREFS_PER_USER_CONSIDERED); String usersFilePathString = jobConf.get(USERS_FILE); if (usersFilePathString != null) { FSDataInputStream in = null;//from ww w. ja v a 2 s. c o m try { Path unqualifiedUsersFilePath = new Path(usersFilePathString); FileSystem fs = FileSystem.get(unqualifiedUsersFilePath.toUri(), jobConf); usersToRecommendFor = new FastIDSet(); Path usersFilePath = unqualifiedUsersFilePath.makeQualified(fs); in = fs.open(usersFilePath); for (String line : new FileLineIterable(in)) { try { usersToRecommendFor.add(Long.parseLong(line)); } catch (NumberFormatException nfe) { log.warn("usersFile line ignored: {}", line); } } } finally { Closeables.closeQuietly(in); } } }