List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:org.apache.mahout.classifier.BayesFileFormatter.java
/** * Collapse all the files in the inputDir into a single file in the proper Bayes format, 1 document per line * /*from w ww . j av a 2 s .co m*/ * @param label * The label * @param analyzer * The analyzer to use * @param inputDir * The input Directory * @param charset * The charset of the input files * @param outputFile * The file to collapse to */ public static void collapse(String label, Analyzer analyzer, File inputDir, Charset charset, File outputFile) throws IOException { Writer writer = Files.newWriter(outputFile, charset); try { inputDir.listFiles(new FileProcessor(label, analyzer, charset, writer)); // listFiles() is called here as a way to recursively visit files, // actually } finally { Closeables.closeQuietly(writer); } }
From source file:co.cask.cdap.common.io.Locations.java
/** * Creates a new {@link InputSupplier} that can provides {@link SeekableInputStream} of the given path. * * @param fs The {@link org.apache.hadoop.fs.FileSystem} for the given path. * @param path The path to create {@link co.cask.cdap.common.io.SeekableInputStream} when requested. * @return A {@link InputSupplier}./* w w w . ja va 2 s .co m*/ */ public static InputSupplier<? extends SeekableInputStream> newInputSupplier(final FileSystem fs, final Path path) { return new InputSupplier<SeekableInputStream>() { @Override public SeekableInputStream getInput() throws IOException { FSDataInputStream input = fs.open(path); try { return new DFSSeekableInputStream(input, createDFSStreamSizeProvider(fs, path, input)); } catch (Throwable t) { Closeables.closeQuietly(input); Throwables.propagateIfInstanceOf(t, IOException.class); throw new IOException(t); } } }; }
From source file:org.eclipselabs.tycho.installer.plugin.win.MsiInstallerCreator.java
private StringTemplateGroup initTemplate(String template) { final InputStream inputStream = getClass().getResourceAsStream(template); if (inputStream == null) { throw new IllegalStateException("Error: " + template + " not found"); }//from w w w . j a va 2s . co m try { final Reader reader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8)); try { return new StringTemplateGroup(reader, DefaultTemplateLexer.class); } finally { Closeables.closeQuietly(reader); } } finally { Closeables.closeQuietly(inputStream); } }
From source file:org.sonar.plugins.swift.issues.SwiftProfile.java
@Override public RulesProfile createProfile(ValidationMessages messages) { LOGGER.info("Creating Swift Profile"); Reader config = null;/*from www . j av a 2 s . co m*/ final RulesProfile profile = RulesProfile.create("Swift", Swift.KEY); profile.setDefaultProfile(true); try { config = new InputStreamReader(getClass().getResourceAsStream(SwiftLintProfile.PROFILE_PATH)); RulesProfile ocLintRulesProfile = swiftLintProfileImporter.importProfile(config, messages); for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) { profile.addActiveRule(rule); } return profile; } finally { Closeables.closeQuietly(config); } }
From source file:com.google.dart.tools.ui.internal.intro.IntroEditor.java
/** * Reads the existing text file with give name in the package of {@link IntroEditor}. *///from www . j a v a 2 s .co m private static String readTemplate(String name) throws IOException { InputStream welcomeStream = IntroEditor.class.getResourceAsStream(name); try { return CharStreams.toString(new InputStreamReader(welcomeStream)); } finally { Closeables.closeQuietly(welcomeStream); } }
From source file:org.apache.mahout.classifier.rbm.model.RBMModel.java
/** * Materialize./*from www. j av a2 s. c om*/ * * @param input the path to the model * @param conf the hadoop configuration * @return the model * @throws IOException Signals that an I/O exception has occurred. */ public static RBMModel materialize(Path input, Configuration conf) throws IOException { FileSystem fs = input.getFileSystem(conf); String rbmType = ""; FSDataInputStream in = fs.open(input); try { char chr; while ((chr = in.readChar()) != ' ') rbmType += chr; } finally { Closeables.closeQuietly(in); } try { return (RBMModel) Class.forName(rbmType).getMethod("materialize", Path.class, Configuration.class) .invoke(null, input, conf); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.enonic.cms.core.xslt.XsltProcessorHelper.java
public String process() { final StringWriter out = new StringWriter(); try {// w w w .j a v a 2s . c o m process(out); } finally { Closeables.closeQuietly(out); } return out.toString(); }
From source file:com.jasonmheim.rollout.station.StationDataDownloader.java
@Override public StationList get() { InputStream input = null;/* w w w. ja v a 2 s . c o m*/ try { input = urlConnectionProvider.get().getInputStream(); StationList stationList = gson.fromJson(new InputStreamReader(input), StationList.class); Log.i("Rollout", "Providing " + stationList.stationBeanList.size() + " stations."); stationList.timestamp = dateProvider.get().getTime(); return stationList; } catch (RuntimeException ex) { Log.w("Rollout", "Failed to deserialize station list data from service", ex); return null; } catch (IOException ex) { Log.w("Rollout", "Failed to download station list data.", ex); return null; } finally { Closeables.closeQuietly(input); } }
From source file:com.netflix.exhibitor.core.config.none.NoneConfigProvider.java
@Override public LoadedInstanceConfig loadConfig() throws Exception { File propertiesFile = new File(directory, FILE_NAME); Properties properties = new Properties(); if (propertiesFile.exists()) { BufferedInputStream in = new BufferedInputStream(new FileInputStream(propertiesFile)); try {/* w w w . jav a 2 s .c o m*/ properties.load(in); } finally { Closeables.closeQuietly(in); } } PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, defaultProperties); return new LoadedInstanceConfig(config, propertiesFile.lastModified()); }
From source file:co.cask.cdap.internal.app.runtime.service.http.LocationHttpContentProducer.java
@Override public void onError(Throwable failureCause) { Closeables.closeQuietly(inputStream); LOG.warn("Failure in producing http content from location {}", location, failureCause); }