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:com.android.builder.sdk.DefaultSdkLoader.java
@Nullable private FullRevision getPlatformToolsRevision(@NonNull File platformToolsFolder) { if (!platformToolsFolder.isDirectory()) { return null; }/*from www. ja va 2 s . c o m*/ Reader reader = null; try { reader = new InputStreamReader(new FileInputStream(new File(platformToolsFolder, FN_SOURCE_PROP)), Charsets.UTF_8); Properties props = new Properties(); props.load(reader); String value = props.getProperty(PkgProps.PKG_REVISION); return FullRevision.parseRevision(value); } catch (FileNotFoundException ignore) { // return null below. } catch (IOException ignore) { // return null below. } catch (NumberFormatException ignore) { // return null below. } finally { try { Closeables.close(reader, true /* swallowIOException */); } catch (IOException e) { // cannot happen } } return null; }
From source file:org.kitesdk.tools.CopyTask.java
public PipelineResult run() throws IOException { boolean runInParallel = true; if (isLocal(from.getDataset()) || isLocal(to.getDataset())) { runInParallel = false;/*w w w . j ava 2s. com*/ } if (runInParallel) { TaskUtil.configure(getConf()).addJarPathForClass(HiveConf.class) .addJarForClass(AvroKeyInputFormat.class); Pipeline pipeline = new MRPipeline(getClass(), getConf()); // TODO: add transforms PCollection<E> collection = pipeline.read(CrunchDatasets.asSource(from, entityClass)); if (compact) { collection = partition(collection, to.getDataset().getDescriptor(), numWriters); } pipeline.write(collection, CrunchDatasets.asTarget(to)); PipelineResult result = pipeline.done(); StageResult sr = Iterables.getFirst(result.getStageResults(), null); if (sr != null && MAP_INPUT_RECORDS != null) { this.count = sr.getCounterValue(MAP_INPUT_RECORDS); } return result; } else { Pipeline pipeline = MemPipeline.getInstance(); // TODO: add transforms PCollection<E> collection = pipeline.read(CrunchDatasets.asSource(from, entityClass)); boolean threw = true; DatasetWriter<E> writer = null; try { writer = to.newWriter(); for (E entity : collection.materialize()) { writer.write(entity); count += 1; } threw = false; } finally { Closeables.close(writer, threw); } return pipeline.done(); } }
From source file:com.complexible.common.openrdf.repository.Repositories.java
public static void add(final Repository theRepo, final Reader theStream, final RDFFormat theFormat, final Resource theContext, final String theBase) throws RDFParseException, IOException { RepositoryConnection aConn = null;// ww w . java 2s . co m try { aConn = theRepo.getConnection(); RepositoryConnections.add(aConn, theStream, theFormat, theContext, theBase); } catch (Exception e) { throw new IOException(e); } finally { RepositoryConnections.closeQuietly(aConn); Closeables.close(theStream, false); } }
From source file:com.google.dart.compiler.DeltaAnalyzer.java
private DartParser getParser(Source source) throws IOException { Reader r = source.getSourceReader(); String sourceString = CharStreams.toString(r); Closeables.close(r, false); return new DartParser(new DartScannerParserContext(source, sourceString, listener), false); }
From source file:org.apache.mahout.cf.taste.impl.model.jdbc.GenericJDBCDataModel.java
private static Properties getPropertiesFromStream(InputStream is) throws TasteException { try {// w w w. j a v a 2 s . co m try { Properties props = new Properties(); props.load(is); return props; } finally { Closeables.close(is, true); } } catch (IOException ioe) { throw new TasteException(ioe); } }
From source file:eu.interedition.text.repository.JdbcStore.java
@Override public void text(long id, final Segment segment, final TextCallback cb) { withTextContent(id, new TextContentCallback<Void>() { @Override// w w w . j a v a2 s. c om public Void withContent(Clob text) throws IOException, SQLException { Reader content = text.getCharacterStream(); if (segment != null) { content = new Segment.Reader(content, segment); } try { cb.text(content); } finally { Closeables.close(content, false); } return null; } }); }
From source file:com.sdw.dream.spark.examples.streaming.JavaCustomReceiver.java
/** Create a socket connection and receive data until receiver is stopped */ private void receive() { try {//from w ww .ja v a 2s. c om Socket socket = null; BufferedReader reader = null; String userInput = null; try { // connect to the server socket = new Socket(host, port); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Until stopped or connection broken continue reading while (!isStopped() && (userInput = reader.readLine()) != null) { System.out.println("Received data '" + userInput + "'"); store(userInput); } } finally { Closeables.close(reader, /* swallowIOException = */ true); Closeables.close(socket, /* swallowIOException = */ true); } // Restart in an attempt to connect again when server is active again restart("Trying to connect again"); } catch (ConnectException ce) { // restart if could not connect to server restart("Could not connect", ce); } catch (Throwable t) { restart("Error receiving data", t); } }
From source file:io.druid.indexer.hadoop.DatasourceRecordReader.java
@Override public void close() throws IOException { Closeables.close(firehose, true); for (QueryableIndex qi : indexes) { Closeables.close(qi, true);/*from w w w. j a v a 2s .co m*/ } for (File dir : tmpSegmentDirs) { FileUtils.deleteDirectory(dir); } }
From source file:org.apache.james.util.scanner.SpamAssassinInvoker.java
/** * Scan a MimeMessage for spam by passing it to spamd. * //www. j a v a2 s . co m * @param message * The MimeMessage to scan * @return true if spam otherwise false * @throws MessagingException * if an error on scanning is detected */ public boolean scanMail(MimeMessage message) throws MessagingException { Socket socket = null; OutputStream out = null; BufferedReader in = null; try { socket = new Socket(spamdHost, spamdPort); out = socket.getOutputStream(); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.write("CHECK SPAMC/1.2\r\n\r\n".getBytes()); // pass the message to spamd message.writeTo(out); out.flush(); socket.shutdownOutput(); String s; while ((s = in.readLine()) != null) { if (s.startsWith("Spam:")) { StringTokenizer t = new StringTokenizer(s, " "); boolean spam; try { t.nextToken(); spam = Boolean.valueOf(t.nextToken()); } catch (Exception e) { // On exception return flase return false; } t.nextToken(); hits = t.nextToken(); t.nextToken(); required = t.nextToken(); if (spam) { // message was spam headers.put(FLAG_MAIL_ATTRIBUTE_NAME, "YES"); headers.put(STATUS_MAIL_ATTRIBUTE_NAME, "Yes, hits=" + hits + " required=" + required); // spam detected return true; } else { // add headers headers.put(FLAG_MAIL_ATTRIBUTE_NAME, "NO"); headers.put(STATUS_MAIL_ATTRIBUTE_NAME, "No, hits=" + hits + " required=" + required); return false; } } } return false; } catch (UnknownHostException e1) { throw new MessagingException("Error communicating with spamd. Unknown host: " + spamdHost); } catch (IOException e1) { throw new MessagingException( "Error communicating with spamd on " + spamdHost + ":" + spamdPort + " Exception: " + e1); } catch (MessagingException e1) { throw new MessagingException( "Error communicating with spamd on " + spamdHost + ":" + spamdPort + " Exception: " + e1); } finally { try { Closeables.close(in, true); Closeables.close(out, true); socket.close(); } catch (Exception e) { // Should never happen } } }
From source file:com.andado.spark.examples.streaming.JavaCustomReceiver.java
/** * Create a socket connection and receive data until receiver is stopped *//*from ww w . j a va 2 s. com*/ private void receive() { try { Socket socket = null; BufferedReader reader = null; String userInput = null; try { // connect to the server socket = new Socket(host, port); reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); // Until stopped or connection broken continue reading while (!isStopped() && (userInput = reader.readLine()) != null) { System.out.println("Received data '" + userInput + "'"); store(userInput); } } finally { Closeables.close(reader, /* swallowIOException = */ true); Closeables.close(socket, /* swallowIOException = */ true); } // Restart in an attempt to connect again when server is active again restart("Trying to connect again"); } catch (ConnectException ce) { // restart if could not connect to server restart("Could not connect", ce); } catch (Throwable t) { restart("Error receiving data", t); } }