Example usage for com.google.common.io Closeables close

List of usage examples for com.google.common.io Closeables close

Introduction

In this page you can find the example usage for com.google.common.io Closeables close.

Prototype

public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException 

Source Link

Document

Closes a Closeable , with control over whether an IOException may be thrown.

Usage

From source file:com.metamx.common.guava.Yielders.java

public static <T> Yielder<T> done(final T finalVal, final Closeable closeable) {
    return new Yielder<T>() {
        @Override//from  w w  w.j  a v  a  2s  . c  om
        public T get() {
            return finalVal;
        }

        @Override
        public Yielder<T> next(T initValue) {
            return null;
        }

        @Override
        public boolean isDone() {
            return true;
        }

        @Override
        public void close() throws IOException {
            Closeables.close(closeable, false);
        }
    };
}

From source file:sg.atom.utils._beta.functional.Yielders.java

public static <T> Yielder<T> done(final T finalVal, final Closeable closeable) {
    return new Yielder<T>() {
        @Override//from   w  w  w. ja v  a  2 s. c  o  m
        public T get() {
            return finalVal;
        }

        @Override
        public Yielder<T> next(T initValue) {
            return null;
        }

        @Override
        public boolean isDone() {
            return true;
        }

        @Override
        public void close() throws IOException {
            Closeables.close(closeable, true);
            //Closeables.closeQuietly(closeable);
        }
    };
}

From source file:org.trancecode.io.TcByteStreams.java

public static long copy(final InputStream in, final OutputStream out, final boolean close) {
    try {//from   w  w  w  . ja v a 2  s  .  c  om
        final long bytes = ByteStreams.copy(in, out);
        Closeables.close(in, false);
        Closeables.close(out, false);
        return bytes;
    } catch (final IOException e) {
        throw new RuntimeIOException(e);
    } finally {
        if (close) {
            Closeables.closeQuietly(in);
            Closeables.closeQuietly(out);
        }
    }
}

From source file:org.openqa.selenium.io.IOUtils.java

public static void closeQuietly(Closeable closeable) {
    try {//from  ww  w  .j a  va2 s  . c  o m
        Closeables.close(closeable, true);
    } catch (IOException ignoted) {
    }
}

From source file:org.trancecode.xml.Jaxp.java

public static void closeQuietly(final Source source, final Logger logger) {
    if (source instanceof StreamSource) {
        final StreamSource streamSource = (StreamSource) source;
        try {//from   w ww. j a  v a  2 s.c  om
            Closeables.close(streamSource.getInputStream(), false);
            Closeables.close(streamSource.getReader(), false);
        } catch (final IOException e) {
            if (logger != null) {
                logger.warn(e.toString(), e);
            }
        }
    }
}

From source file:org.apache.mahout.classifier.sgd.ModelSerializer.java

public static void writeBinary(String path, CrossFoldLearner model) throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(path));
    try {//  w  w  w  .  j a  v a  2 s .  c o  m
        PolymorphicWritable.write(out, model);
    } finally {
        Closeables.close(out, false);
    }
}

From source file:org.moe.designer.gradle.PropertiesUtil.java

@NotNull
public static Properties getProperties(@NotNull File filePath) throws IOException {
    if (filePath.isDirectory()) {
        throw new IllegalArgumentException(
                String.format("The path '%1$s' belongs to a directory!", filePath.getPath()));
    }/*from   ww  w .  ja  v a  2s.co  m*/
    if (!filePath.exists()) {
        return new Properties();
    }
    Properties properties = new Properties();
    Reader reader = null;
    try {
        //noinspection IOResourceOpenedButNotSafelyClosed
        reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(filePath)), Charsets.UTF_8);
        properties.load(reader);
    } finally {
        Closeables.close(reader, true);
    }
    return properties;
}

From source file:com.google.idea.blaze.base.util.SerializationUtil.java

public static void saveToDisk(@NotNull File file, @NotNull Serializable serializable) throws IOException {
    ensureExists(file.getParentFile());/*  w  w w  .j  av  a  2 s  .co  m*/
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        try {
            oos.writeObject(serializable);
        } finally {
            Closeables.close(oos, false);
        }
    } finally {
        Closeables.close(fos, false);
    }
}

From source file:com.boundary.zoocreeper.Restore.java

public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    RestoreOptions options = new RestoreOptions();
    CmdLineParser parser = new CmdLineParser(options);
    try {/*from   w  w w. ja v  a 2 s .c  o m*/
        parser.parseArgument(args);
        if (options.help) {
            usage(parser, 0);
        }
    } catch (CmdLineException e) {
        if (!options.help) {
            System.err.println(e.getLocalizedMessage());
        }
        usage(parser, options.help ? 0 : 1);
    }
    if (options.verbose) {
        LoggingUtils.enableDebugLogging(Restore.class.getPackage().getName());
    }
    InputStream is = null;
    try {
        if ("-".equals(options.inputFile)) {
            LOGGER.info("Restoring from stdin");
            is = new BufferedInputStream(System.in);
        } else {
            is = new BufferedInputStream(new FileInputStream(options.inputFile));
        }
        if (options.compress) {
            is = new GZIPInputStream(is);
        }
        Restore restore = new Restore(options);
        restore.restore(is);
    } finally {
        Closeables.close(is, true);
    }
}

From source file:org.apache.mahout.text.wikipedia.WikipediaXmlSplitter.java

public static void main(String[] args) throws IOException {
    DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
    ArgumentBuilder abuilder = new ArgumentBuilder();
    GroupBuilder gbuilder = new GroupBuilder();

    Option dumpFileOpt = obuilder.withLongName("dumpFile").withRequired(true)
            .withArgument(abuilder.withName("dumpFile").withMinimum(1).withMaximum(1).create())
            .withDescription("The path to the wikipedia dump file (.bz2 or uncompressed)").withShortName("d")
            .create();//from w  w  w.  j  av  a  2  s.  c o m

    Option outputDirOpt = obuilder.withLongName("outputDir").withRequired(true)
            .withArgument(abuilder.withName("outputDir").withMinimum(1).withMaximum(1).create())
            .withDescription("The output directory to place the splits in:\n"
                    + "local files:\n\t/var/data/wikipedia-xml-chunks or\n\tfile:///var/data/wikipedia-xml-chunks\n"
                    + "Hadoop DFS:\n\thdfs://wikipedia-xml-chunks\n"
                    + "AWS S3 (blocks):\n\ts3://bucket-name/wikipedia-xml-chunks\n"
                    + "AWS S3 (native files):\n\ts3n://bucket-name/wikipedia-xml-chunks\n")

            .withShortName("o").create();

    Option s3IdOpt = obuilder.withLongName("s3ID").withRequired(false)
            .withArgument(abuilder.withName("s3Id").withMinimum(1).withMaximum(1).create())
            .withDescription("Amazon S3 ID key").withShortName("i").create();
    Option s3SecretOpt = obuilder.withLongName("s3Secret").withRequired(false)
            .withArgument(abuilder.withName("s3Secret").withMinimum(1).withMaximum(1).create())
            .withDescription("Amazon S3 secret key").withShortName("s").create();

    Option chunkSizeOpt = obuilder.withLongName("chunkSize").withRequired(true)
            .withArgument(abuilder.withName("chunkSize").withMinimum(1).withMaximum(1).create())
            .withDescription("The Size of the chunk, in megabytes").withShortName("c").create();
    Option numChunksOpt = obuilder.withLongName("numChunks").withRequired(false)
            .withArgument(abuilder.withName("numChunks").withMinimum(1).withMaximum(1).create())
            .withDescription(
                    "The maximum number of chunks to create.  If specified, program will only create a subset of the chunks")
            .withShortName("n").create();
    Group group = gbuilder.withName("Options").withOption(dumpFileOpt).withOption(outputDirOpt)
            .withOption(chunkSizeOpt).withOption(numChunksOpt).withOption(s3IdOpt).withOption(s3SecretOpt)
            .create();

    Parser parser = new Parser();
    parser.setGroup(group);
    CommandLine cmdLine;
    try {
        cmdLine = parser.parse(args);
    } catch (OptionException e) {
        log.error("Error while parsing options", e);
        CommandLineUtil.printHelp(group);
        return;
    }

    Configuration conf = new Configuration();
    String dumpFilePath = (String) cmdLine.getValue(dumpFileOpt);
    String outputDirPath = (String) cmdLine.getValue(outputDirOpt);

    if (cmdLine.hasOption(s3IdOpt)) {
        String id = (String) cmdLine.getValue(s3IdOpt);
        conf.set("fs.s3n.awsAccessKeyId", id);
        conf.set("fs.s3.awsAccessKeyId", id);
    }
    if (cmdLine.hasOption(s3SecretOpt)) {
        String secret = (String) cmdLine.getValue(s3SecretOpt);
        conf.set("fs.s3n.awsSecretAccessKey", secret);
        conf.set("fs.s3.awsSecretAccessKey", secret);
    }
    // do not compute crc file when using local FS
    conf.set("fs.file.impl", "org.apache.hadoop.fs.RawLocalFileSystem");
    FileSystem fs = FileSystem.get(URI.create(outputDirPath), conf);

    int chunkSize = 1024 * 1024 * Integer.parseInt((String) cmdLine.getValue(chunkSizeOpt));

    int numChunks = Integer.MAX_VALUE;
    if (cmdLine.hasOption(numChunksOpt)) {
        numChunks = Integer.parseInt((String) cmdLine.getValue(numChunksOpt));
    }

    String header = "<mediawiki xmlns=\"http://www.mediawiki.org/xml/export-0.3/\" "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
            + "xsi:schemaLocation=\"http://www.mediawiki.org/xml/export-0.3/ "
            + "http://www.mediawiki.org/xml/export-0.3.xsd\" " + "version=\"0.3\" " + "xml:lang=\"en\">\n"
            + "  <siteinfo>\n" + "<sitename>Wikipedia</sitename>\n"
            + "    <base>http://en.wikipedia.org/wiki/Main_Page</base>\n"
            + "    <generator>MediaWiki 1.13alpha</generator>\n" + "    <case>first-letter</case>\n"
            + "    <namespaces>\n" + "      <namespace key=\"-2\">Media</namespace>\n"
            + "      <namespace key=\"-1\">Special</namespace>\n" + "      <namespace key=\"0\" />\n"
            + "      <namespace key=\"1\">Talk</namespace>\n" + "      <namespace key=\"2\">User</namespace>\n"
            + "      <namespace key=\"3\">User talk</namespace>\n"
            + "      <namespace key=\"4\">Wikipedia</namespace>\n"
            + "      <namespace key=\"5\">Wikipedia talk</namespace>\n"
            + "      <namespace key=\"6\">Image</namespace>\n"
            + "      <namespace key=\"7\">Image talk</namespace>\n"
            + "      <namespace key=\"8\">MediaWiki</namespace>\n"
            + "      <namespace key=\"9\">MediaWiki talk</namespace>\n"
            + "      <namespace key=\"10\">Template</namespace>\n"
            + "      <namespace key=\"11\">Template talk</namespace>\n"
            + "      <namespace key=\"12\">Help</namespace>\n"
            + "      <namespace key=\"13\">Help talk</namespace>\n"
            + "      <namespace key=\"14\">Category</namespace>\n"
            + "      <namespace key=\"15\">Category talk</namespace>\n"
            + "      <namespace key=\"100\">Portal</namespace>\n"
            + "      <namespace key=\"101\">Portal talk</namespace>\n" + "    </namespaces>\n"
            + "  </siteinfo>\n";

    StringBuilder content = new StringBuilder();
    content.append(header);
    NumberFormat decimalFormatter = new DecimalFormat("0000");
    File dumpFile = new File(dumpFilePath);

    // If the specified path for the input file is incorrect, return immediately
    if (!dumpFile.exists()) {
        log.error("Input file path {} doesn't exist", dumpFilePath);
        return;
    }

    FileLineIterator it;
    if (dumpFilePath.endsWith(".bz2")) {
        // default compression format from http://download.wikimedia.org
        CompressionCodec codec = new BZip2Codec();
        it = new FileLineIterator(codec.createInputStream(new FileInputStream(dumpFile)));
    } else {
        // assume the user has previously de-compressed the dump file
        it = new FileLineIterator(dumpFile);
    }
    int fileNumber = 0;
    while (it.hasNext()) {
        String thisLine = it.next();
        if (thisLine.trim().startsWith("<page>")) {
            boolean end = false;
            while (!thisLine.trim().startsWith("</page>")) {
                content.append(thisLine).append('\n');
                if (it.hasNext()) {
                    thisLine = it.next();
                } else {
                    end = true;
                    break;
                }
            }
            content.append(thisLine).append('\n');

            if (content.length() > chunkSize || end) {
                content.append("</mediawiki>");
                fileNumber++;
                String filename = outputDirPath + "/chunk-" + decimalFormatter.format(fileNumber) + ".xml";
                BufferedWriter chunkWriter = new BufferedWriter(
                        new OutputStreamWriter(fs.create(new Path(filename)), "UTF-8"));
                try {
                    chunkWriter.write(content.toString(), 0, content.length());
                } finally {
                    Closeables.close(chunkWriter, false);
                }
                if (fileNumber >= numChunks) {
                    break;
                }
                content = new StringBuilder();
                content.append(header);
            }
        }
    }
}