List of usage examples for com.google.common.io Closeables close
public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException
From source file:org.apache.mahout.text.SequenceFilesFromMailArchives.java
public void createSequenceFiles(MailOptions options) throws IOException { ChunkedWriter writer = new ChunkedWriter(getConf(), options.getChunkSize(), new Path(options.getOutputDir())); MailProcessor processor = new MailProcessor(options, options.getPrefix(), writer); try {//from w w w . j ava 2 s.com if (options.getInput().isDirectory()) { PrefixAdditionDirectoryWalker walker = new PrefixAdditionDirectoryWalker(processor, writer); walker.walk(options.getInput()); log.info("Parsed {} messages from {}", walker.getMessageCount(), options.getInput().getAbsolutePath()); } else { long start = System.currentTimeMillis(); long cnt = processor.parseMboxLineByLine(options.getInput()); long finish = System.currentTimeMillis(); log.info("Parsed {} messages from {} in time: {}", cnt, options.getInput().getAbsolutePath(), finish - start); } } finally { Closeables.close(writer, false); } }
From source file:com.complexible.common.openrdf.repository.RepositoryConnections.java
public static void add(final RepositoryConnection theConn, Reader theStream, final RDFFormat theFormat, final Resource theContext, final String theBase) throws RDFParseException, IOException { RDFParser aParser = Rio.createParser(theFormat); aParser.getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, false); aParser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, false); aParser.getParserConfig().set(BasicParserSettings.NORMALIZE_DATATYPE_VALUES, false); aParser.getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true); aParser.getParserConfig().set(BasicParserSettings.VERIFY_RELATIVE_URIS, false); try {//from w w w .jav a 2s. co m theConn.begin(); RDFInserter aInserter = new RDFInserter(theConn); if (theContext != null) { aInserter.enforceContext(theContext); } aParser.setRDFHandler(aInserter); aParser.parse(theStream, theBase == null ? (theContext != null ? theContext.stringValue() : "http://openrdf.clarkparsia.com") : theBase); theConn.commit(); } catch (Exception e) { throw new IOException(e); } finally { Closeables.close(theStream, false); } }
From source file:com.github.autermann.sockets.ssl.SSLUtils.java
public static List<X509Certificate> readCertificates(InputSupplier<? extends InputStream> in) throws CertificateException, IOException { InputStream is = null;// w w w .java 2s .c o m try { is = in.getInput(); List<X509Certificate> certs = new LinkedList<X509Certificate>(); CertificateFactory cf = CertificateFactory.getInstance(SSLConstants.CERTIFICATE_TYPE_X509); while (is.available() > 0) { X509Certificate cert = (X509Certificate) cf.generateCertificate(is); certs.add(cert); log.info("Read {}", cert.getSubjectX500Principal().getName()); } return certs; } catch (CertificateParsingException ex) { // FIXME check compatibility of X509CertificateFactory in Java 6/7 if (Java.v6 && ex.getMessage() != null && ex.getMessage().equals("invalid DER-encoded certificate data")) { log.warn("X509CertificateFactory was not able to parse certificate. " + "Consider switching to Java 7 to overcome this issue."); } throw ex; } finally { Closeables.close(is, true); } }
From source file:org.openqa.selenium.os.ProcessUtils.java
private static void closeAllStreamsAndDestroyProcess(Process process) { try {/*w w w. ja v a2 s .c o m*/ Closeables.close(process.getInputStream(), true); Closeables.close(process.getErrorStream(), true); Closeables.close(process.getOutputStream(), true); } catch (IOException ignored) { } process.destroy(); }
From source file:org.kitesdk.data.spi.Schemas.java
public static Schema fromJSON(String name, Configuration conf, URI location) throws IOException { InputStream in = null;/* ww w . j av a 2 s . c o m*/ boolean threw = true; try { in = open(conf, location); Schema schema = fromJSON(name, in); threw = false; return schema; } finally { Closeables.close(in, threw); } }
From source file:org.apache.mahout.classifier.naivebayes.BayesUtils.java
public static int writeLabelIndex(Configuration conf, Path indexPath, Iterable<Pair<Text, IntWritable>> labels) throws IOException { FileSystem fs = FileSystem.get(indexPath.toUri(), conf); SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, indexPath, Text.class, IntWritable.class); Collection<String> seen = Sets.newHashSet(); int i = 0;/*from ww w .j a v a 2 s.c om*/ try { for (Object label : labels) { String theLabel = SLASH.split(((Pair<?, ?>) label).getFirst().toString())[1]; if (!seen.contains(theLabel)) { writer.append(new Text(theLabel), new IntWritable(i++)); seen.add(theLabel); } } } finally { Closeables.close(writer, false); } return i; }
From source file:mlbench.bayes.BayesUtils.java
static List<Path> createDictionaryChunks(Path wordCountPath, Path dictionaryPathBase, Configuration baseConf, int chunkSizeInMegabytes, int[] maxTermDimension) throws IOException { List<Path> chunkPaths = Lists.newArrayList(); Configuration conf = new Configuration(baseConf); FileSystem fs = FileSystem.get(wordCountPath.toUri(), conf); long chunkSizeLimit = chunkSizeInMegabytes * 1024L * 1024L; int chunkIndex = 0; Path chunkPath = new Path(dictionaryPathBase, DICTIONARY_FILE + chunkIndex); chunkPaths.add(chunkPath);//from www . j av a 2s . c om SequenceFile.Writer dictWriter = new SequenceFile.Writer(fs, conf, chunkPath, Text.class, IntWritable.class); try { long currentChunkSize = 0; Path filesPattern = new Path(wordCountPath, OUTPUT_FILES_PATTERN); int i = 0; for (Pair<Writable, Writable> record : new SequenceFileDirIterable<Writable, Writable>(filesPattern, PathType.GLOB, null, null, true, conf)) { if (currentChunkSize > chunkSizeLimit) { Closeables.close(dictWriter, false); chunkIndex++; chunkPath = new Path(dictionaryPathBase, DICTIONARY_FILE + chunkIndex); chunkPaths.add(chunkPath); dictWriter = new SequenceFile.Writer(fs, conf, chunkPath, Text.class, IntWritable.class); currentChunkSize = 0; } Writable key = record.getFirst(); int fieldSize = DICTIONARY_BYTE_OVERHEAD + key.toString().length() * 2 + Integer.SIZE / 8; currentChunkSize += fieldSize; dictWriter.append(key, new IntWritable(i++)); } maxTermDimension[0] = i; } finally { Closeables.close(dictWriter, false); } return chunkPaths; }
From source file:net.pterodactylus.sonitus.data.sink.Icecast2Sink.java
@Override public void close() { try {/* w ww .j av a 2s .c o m*/ Closeables.close(socketOutputStream, true); } catch (IOException e) { /* will never throw. */ } }
From source file:com.b2international.index.compat.SingleDirectoryIndexImpl.java
protected void doDispose() { for (final IndexCommit heldCommit : heldSnapshots.values()) { try {/*w ww .j a v a 2 s.c om*/ releaseSnapshotCommit(heldCommit); } catch (final IOException ignored) { // XXX: dispose should succeed regardless of errors } } try { Closeables.close(manager, true); Closeables.close(writer, true); Closeables.close(directory, true); } catch (IOException e) { throw new RuntimeException(e); } manager = null; writer = null; directory = null; }
From source file:eu.interedition.text.rdbms.RelationalTextRepository.java
public void read(Text text, final TextConsumer consumer) throws IOException { read(new ReaderCallback<Void>(text) { @Override/*from w ww.j a va 2s .co m*/ protected Void read(Clob content) throws SQLException, IOException { Reader contentReader = null; try { consumer.read(contentReader = content.getCharacterStream(), content.length()); } catch (IOException e) { Throwables.propagate(e); } finally { Closeables.close(contentReader, false); } return null; } }); }