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:io.druid.sql.calcite.util.SpecificSegmentsQuerySegmentWalker.java
@Override public void close() throws IOException { for (Closeable closeable : closeables) { Closeables.close(closeable, true); } }
From source file:org.apache.mahout.clustering.classify.ClusterClassifier.java
public static void writePolicy(ClusteringPolicy policy, Path path) throws IOException { Path policyPath = new Path(path, POLICY_FILE_NAME); Configuration config = new Configuration(); FileSystem fs = FileSystem.get(policyPath.toUri(), config); SequenceFile.Writer writer = new SequenceFile.Writer(fs, config, policyPath, Text.class, ClusteringPolicyWritable.class); writer.append(new Text(), new ClusteringPolicyWritable(policy)); Closeables.close(writer, false); }
From source file:org.apache.mahout.cf.taste.hadoop.item.IDReader.java
private FastIDSet readIDList(String pathString) throws IOException { FastIDSet result = null;/*from w w w . j a va2s .c om*/ if (pathString != null) { result = new FastIDSet(); InputStream in = openFile(pathString); try { for (String line : new FileLineIterable(in)) { try { result.add(Long.parseLong(line)); } catch (NumberFormatException nfe) { log.warn("line ignored: {}", line); } } } finally { Closeables.close(in, true); } } return result; }
From source file:eu.interedition.text.Text.java
public static Text create(Session session, Annotation layer, XMLStreamReader xml) throws IOException, XMLStreamException { final FileBackedOutputStream xmlBuf = createBuffer(); XMLEventReader xmlEventReader = null; XMLEventWriter xmlEventWriter = null; try {//from ww w.ja va 2s. c o m xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(xml); xmlEventWriter = XML_OUTPUT_FACTORY.createXMLEventWriter(new OutputStreamWriter(xmlBuf, Text.CHARSET)); xmlEventWriter.add(xmlEventReader); } finally { XML.closeQuietly(xmlEventWriter); XML.closeQuietly(xmlEventReader); Closeables.close(xmlBuf, false); } Reader xmlBufReader = null; try { xmlBufReader = new InputStreamReader(xmlBuf.getSupplier().getInput(), Text.CHARSET); return create(session, layer, Text.Type.XML).write(session, xmlBufReader); } finally { Closeables.close(xmlBufReader, false); } }
From source file:data.models.Data.java
/** * Tries and locate a model in the current class' classpath. * //from www .jav a2 s. c om * @param string * Relative path to the model we seek (relative to this class). * @param resourceSet * the resource set in which to load the resource. * @return The loaded resource. * @throws IOException * Thrown if we could not access either this class' resource, or the file towards which * <code>path</code> points. */ // Suppressing the warning until bug 376938 is fixed protected Resource loadFromClassLoader(String path, ResourceSet resourceSet) { final URL fileURL = getClass().getResource(path); final URI uri = URI.createURI(fileURL.toString()); final Resource existing = resourceSet.getResource(uri, false); if (existing != null) { return existing; } InputStream stream = null; Resource resource = null; try { resource = resourceSet.createResource(uri); stream = fileURL.openStream(); resource.load(stream, Collections.emptyMap()); } catch (IOException e) { // return null } catch (WrappedException e) { // return null } finally { try { Closeables.close(stream, true); } catch (IOException e) { // already swallowed } } return resource; }
From source file:com.replaymod.replaystudio.replay.ZipReplayFile.java
@Override public void saveTo(File target) throws IOException { for (OutputStream out : outputStreams.values()) { Closeables.close(out, false); }// ww w. jav a2 s . c o m outputStreams.clear(); try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)))) { if (zipFile != null) { for (ZipEntry entry : Collections.list(zipFile.entries())) { if (!changedEntries.containsKey(entry.getName()) && !removedEntries.contains(entry.getName())) { out.putNextEntry(entry); Utils.copy(zipFile.getInputStream(entry), out); } } } for (Map.Entry<String, File> e : changedEntries.entrySet()) { out.putNextEntry(new ZipEntry(e.getKey())); Utils.copy(new BufferedInputStream(new FileInputStream(e.getValue())), out); } } }
From source file:org.nll.hbase.ui.util.HbaseUtil.java
public static void getResultScann(String tableName, String start_rowkey, String stop_rowkey) throws Exception { Scan scan = new Scan(Bytes.toBytes(start_rowkey)); ResultScanner rs = null;/*from w w w. j a v a2s.c o m*/ HTableInterface table = getTable(defaultConnection, tableName); try { rs = table.getScanner(scan); for (Result r : rs) { for (KeyValue kv : r.list()) { System.out.println("row:" + Bytes.toString(kv.getRow())); System.out.println("family:" + Bytes.toString(kv.getFamily())); System.out.println("qualifier:" + Bytes.toString(kv.getQualifier())); System.out.println("value:" + Bytes.toString(kv.getValue())); System.out.println("timestamp:" + kv.getTimestamp()); System.out.println("-------------------------------------------"); } } } finally { Closeables.close(rs, true); Closeables.close(table, true); } }
From source file:be.wimsymons.intellij.polopolyimport.PPImporter.java
private void addToJar(JarOutputStream jarOS, VirtualFile file) throws IOException { JarEntry entry = new JarEntry(file.getCanonicalPath()); entry.setTime(file.getTimeStamp());/*ww w . j av a 2 s . c om*/ jarOS.putNextEntry(entry); Reader reader = wrapWithReplacements(file.getInputStream(), file.getCharset()); Writer writer = new OutputStreamWriter(jarOS); try { CharStreams.copy(reader, writer); } finally { Closeables.close(reader, true); Closeables.close(writer, true); } }
From source file:com.stratio.ingestion.sink.mongodb.MappingDefinition.java
public static MappingDefinition load(final String path) { InputStream mappingInputstream = null; try {// w ww . j ava 2 s . c om File mappingFile = new File(path); if (mappingFile.exists()) { mappingInputstream = new FileInputStream(mappingFile); log.debug("Loaded mapping definition from file: {}", path); } else { mappingInputstream = MappingDefinition.class.getResourceAsStream(path); if (mappingInputstream == null) { throw new MongoSinkException("Cannot find file or resource for mapping definition: " + path); } log.debug("Loaded mapping definition from resource: {}", path); } return new MappingDefinition(new ObjectMapper().readTree(mappingInputstream)); } catch (IOException ex) { throw new MongoSinkException(ex); } catch (IllegalArgumentException ex) { throw new MongoSinkException(ex); } finally { try { Closeables.close(mappingInputstream, true); } catch (IOException ex) { // Ignore } } }
From source file:com.proofpoint.event.collector.S3Uploader.java
@VisibleForTesting public void enqueueLocalFileForUpload(final File file) { retryExecutor.submit(new Runnable() { @Override/*from w ww . j a v a 2 s .c o m*/ public void run() { BufferedReader in = null; FileInputStream filein = null; try { filein = new FileInputStream(file); in = new BufferedReader(new InputStreamReader(new SnappyInputStream(filein), Charsets.UTF_8)); Event event = codec.fromJson(in.readLine()); EventPartition partition = partitioner.getPartition(event); enqueueUpload(partition, file); } catch (Exception e) { log.error(e, "Error while reading file %s before upload. Marking as failed", file.getName()); handleFailure(file); } finally { try { Closeables.close(filein, true); Closeables.close(in, true); } catch (IOException e) { log.error(e, "Error closing event file"); } } } }); }