Example usage for org.apache.commons.io IOUtils closeQuietly

List of usage examples for org.apache.commons.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(OutputStream output) 

Source Link

Document

Unconditionally close an OutputStream.

Usage

From source file:net.socket.bio.TimeClient.java

/**
 * @param args/*from   w  w w  . ja v a2 s .  c  o m*/
 */
public static void main(String[] args) {

    int port = 8089;
    if (args != null && args.length > 0) {

        try {
            port = Integer.valueOf(args[0]);
        } catch (NumberFormatException e) {
            // 
        }

    }
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    try {
        socket = new Socket("127.0.0.1", port);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
        out.println("QUERY TIME ORDER");
        out.println("QUERY TIME ORDER");
        String test = StringUtils.repeat("hello tcp", 1000);
        out.println(test);
        System.out.println("Send order 2 server succeed.");
        String resp = in.readLine();
        System.out.println("Now is : " + resp);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(socket);
    }
}

From source file:com.textocat.textokit.segmentation.GenerateBasicAggregateDescriptor.java

/**
 * @param args/*from w w w . ja  v  a2  s .  co  m*/
 * @throws ResourceInitializationException
 */
public static void main(String[] args) throws UIMAException, IOException, SAXException {
    Map<String, MetaDataObject> aeDescriptions = Maps.newLinkedHashMap();
    aeDescriptions.put("tokenizer", TokenizerAPI.getAEImport());

    aeDescriptions.put("sentenceSplitter", SentenceSplitterAPI.getAEImport());

    String outputPath = "desc/basic-aggregate.xml";
    AnalysisEngineDescription desc = PipelineDescriptorUtils.createAggregateDescription(aeDescriptions);
    FileOutputStream out = FileUtils.openOutputStream(new File(outputPath));
    try {
        desc.toXML(out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryParserLauncher.java

public static void main(String[] args) throws Exception {
    XmlDictionaryParserLauncher cfg = new XmlDictionaryParserLauncher();
    new JCommander(cfg, args);

    MorphDictionaryImpl dict = new MorphDictionaryImpl();
    DictionaryExtension ext = cfg.dictExtensionClass.newInstance();
    FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile);
    try {/*from w  w  w .  j  a  v a 2 s  .c  o  m*/
        new XmlDictionaryParser(dict, ext, fis).run();
    } finally {
        IOUtils.closeQuietly(fis);
    }

    log.info("Preparing to serialization...");
    long timeBefore = currentTimeMillis();
    OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputFile), 8192 * 8);
    ObjectOutputStream out = new ObjectOutputStream(fout);
    try {
        out.writeObject(dict.getGramModel());
        out.writeObject(dict);
    } finally {
        out.close();
    }
    log.info("Serialization finished in {} ms.\nOutput size: {} bytes", currentTimeMillis() - timeBefore,
            cfg.outputFile.length());
}

From source file:de.tudarmstadt.ukp.csniper.resbuild.stuff.BncCheckDocuments.java

public static void main(String[] args) throws IOException {
    boolean enabled = false;

    for (File f : FileUtils.listFiles(new File(BASE), new String[] { "xml" }, true)) {
        BufferedInputStream bis = null;
        try {//from ww  w .j av a  2  s  .c  o  m
            bis = new BufferedInputStream(new FileInputStream(f));
            int c;
            StringBuilder sb = new StringBuilder();
            while ((c = bis.read()) != '>') {
                if (enabled) {
                    if (c == '"') {
                        enabled = false;
                        break;
                    } else {
                        sb.append((char) c);
                    }
                }
                if (c == '"') {
                    enabled = true;
                }
            }
            String name = f.getName().substring(0, 3);
            String id = sb.toString();
            if (!name.equals(id)) {
                System.out.println("Name is [" + name + "], but ID is [" + id + "].");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(bis);
        }
    }
}

From source file:hd3gtv.tools.Hexview.java

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.err.println("Usage file1.ext file2.ext ...");
        System.exit(1);//from w w w  . j a v a 2s .  co  m
    }

    Arrays.asList(args).forEach(f -> {
        try {
            File file = new File(f);

            InputStream in = new BufferedInputStream(new FileInputStream(file), 0xFFFF);

            byte[] buffer = new byte[COLS * ROWS];
            int len;
            Hexview hv = null;

            while ((len = in.read(buffer)) != -1) {
                if (hv == null) {
                    hv = new Hexview(buffer, 0, len);
                    hv.setSize(file.length());
                } else {
                    hv.update(buffer, 0, len);
                }
                System.out.println(hv.getView());
            }

            IOUtils.closeQuietly(in);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    });
}

From source file:com.googlecode.shutdownlistener.ShutdownUtility.java

public static void main(String[] args) throws Exception {
    final ShutdownConfiguration config = ShutdownConfiguration.getInstance();

    final String command;
    if (args.length > 0) {
        command = args[0];//from   www.  j  a v  a  2 s.  com
    } else {
        command = config.getStatusCommand();
    }

    System.out.println("Calling " + config.getHost() + ":" + config.getPort() + " with command: " + command);

    final InetAddress hostAddress = InetAddress.getByName(config.getHost());
    final Socket shutdownConnection = new Socket(hostAddress, config.getPort());
    try {
        shutdownConnection.setSoTimeout(5000);
        final BufferedReader reader = new BufferedReader(
                new InputStreamReader(shutdownConnection.getInputStream()));
        final PrintStream writer = new PrintStream(shutdownConnection.getOutputStream());
        try {
            writer.println(command);
            writer.flush();

            while (true) {
                final String line = reader.readLine();
                if (line == null) {
                    break;
                }

                System.out.println(line);
            }
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }
    } finally {
        try {
            shutdownConnection.close();
        } catch (IOException ioe) {
        }
    }

}

From source file:de.tudarmstadt.ukp.experiments.argumentation.clustering.ClusterCentroidsMain.java

public static void main(String[] args) throws Exception {
    //        String clutoVectors = args[0];
    //        String clutoOuputClusters = args[1];
    //        String outputClusterCentroids = args[2];

    File[] files = new File("//home/user-ukp/data2/debates-ranked.100-xmi").listFiles(new FilenameFilter() {
        @Override/* w  w w.j a v a  2  s .  co m*/
        public boolean accept(File dir, String name) {
            //                        return name.startsWith("arg") && name.endsWith(".mat");
            return name.startsWith("sent") && name.endsWith(".mat");
        }
    });
    for (File matFile : files) {
        String clutoVectors = matFile.getAbsolutePath();
        //            String clutoOuputClusters = matFile.getAbsolutePath() + ".clustering.100";
        String clutoOuputClusters = matFile.getAbsolutePath() + ".clustering.1000";
        String outputClusterCentroids = matFile.getAbsolutePath() + ".bin";

        TreeMap<Integer, Vector> centroids = computeClusterCentroids(clutoVectors, clutoOuputClusters);

        // and serialize
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream(outputClusterCentroids));
        objectOutputStream.writeObject(centroids);
        IOUtils.closeQuietly(objectOutputStream);
    }

    //        System.out.println(centroids);
    //        embeddingsToDistance(args[0], centroids, args[2]);
}

From source file:cache.PathCacheExample.java

public static void main(String[] args) throws Exception {
    TestingServer server = new TestingServer();
    CuratorFramework client = null;//from   w  w w. j  a  v  a  2  s .  c o m
    PathChildrenCache cache = null;
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(),
                new ExponentialBackoffRetry(1000, 3));
        client.start();

        // in this example we will cache data. Notice that this is optional.
        cache = new PathChildrenCache(client, PATH, true);
        cache.start();

        processCommands(client, cache);
    } finally {
        IOUtils.closeQuietly(cache);
        IOUtils.closeQuietly(client);
        IOUtils.closeQuietly(server);
    }
}

From source file:com.xinge.sample.samples.docx4j.org.docx4j.samples.OpenUnzippedAndSaveZipped.java

public static void main(String[] args) throws Exception {

    try {/*w  w w  . ja va 2  s  .c om*/
        getInputFilePath(args);
    } catch (IllegalArgumentException e) {
        inputfilepath = System.getProperty("user.dir") + "/OUT";
    }
    System.out.println(inputfilepath);

    // Load the docx
    File baseDir = new File(inputfilepath);
    UnzippedPartStore partLoader = new UnzippedPartStore(baseDir);
    final Load3 loader = new Load3(partLoader);
    OpcPackage opc = loader.get();

    // Save it zipped
    File docxFile = new File(System.getProperty("user.dir") + "/zip.docx");

    ZipPartStore zps = new ZipPartStore();
    zps.setSourcePartStore(opc.getSourcePartStore());

    Save saver = new Save(opc, zps);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(docxFile);
        saver.save(fos);
    } catch (FileNotFoundException e) {
        throw new Docx4JException("Couldn't save " + docxFile.getPath(), e);
    } finally {
        IOUtils.closeQuietly(fos);
    }

}

From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryPSP.java

public static void main(String[] args) throws Exception {
    XmlDictionaryPSP cfg = new XmlDictionaryPSP();
    new JCommander(cfg, args);

    MorphDictionaryImpl dict = new MorphDictionaryImpl();
    DictionaryExtension ext = cfg.dictExtensionClass.newInstance();
    FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile);
    try {//from   w w  w. j  a  v  a 2 s  . co m
        new XmlDictionaryParser(dict, ext, fis).run();
    } finally {
        IOUtils.closeQuietly(fis);
    }

    System.out.println("Preparing to serialization...");
    long timeBefore = currentTimeMillis();
    OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputJarFile), 8192 * 8);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VERSION,
            dict.getVersion());
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_REVISION,
            dict.getRevision());
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VARIANT,
            cfg.variant);
    String dictEntryName = String.format(
            OpencorporaMorphDictionaryAPI.FILENAME_PATTERN_OPENCORPORA_SERIALIZED_DICT, dict.getVersion(),
            dict.getRevision(), cfg.variant);
    JarOutputStream jarOut = new JarOutputStream(fout, manifest);
    jarOut.putNextEntry(new ZipEntry(dictEntryName));
    ObjectOutputStream serOut = new ObjectOutputStream(jarOut);
    try {
        serOut.writeObject(dict.getGramModel());
        serOut.writeObject(dict);
    } finally {
        serOut.flush();
        jarOut.closeEntry();
        serOut.close();
    }
    System.out.println(String.format("Serialization finished in %s ms.\nOutput size: %s bytes",
            currentTimeMillis() - timeBefore, cfg.outputJarFile.length()));
}