Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

In this page you can find the example usage for java.io OutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:Main.java

public static File savebitmap(Bitmap bitmap) {
    OutputStream outStream = null;
    File folder = new File(Environment.getExternalStorageDirectory().toString() + "/InSyte");
    folder.mkdirs();//from  w  w  w  . j  a  v a2  s.  c  o m
    String mDirectory = folder.toString();
    File file = new File(mDirectory, "IMG_" + System.currentTimeMillis() + ".jpg");
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;

}

From source file:com.aliyun.odps.local.common.utils.LocalRunUtils.java

public static String toReadableString(byte[] b) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream printableos = MimeUtility.encode(baos, "quoted-printable");
    printableos.write(b);//w  w w.  j ava2 s .  c o m
    printableos.close();
    return new String(baos.toByteArray(), Charset.forName("UTF-8"));
}

From source file:com.gson.util.HttpKit.java

/**
 * @description /*ww  w  .  j a v a 2  s. co m*/
 * ??: POST 
 * @return       : 
 * @throws IOException 
 * @throws NoSuchProviderException 
 * @throws NoSuchAlgorithmException 
 * @throws KeyManagementException 
 */
public static String post(String url, String params)
        throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
    HttpURLConnection http = null;
    if (isHttps(url)) {
        http = initHttps(url, _POST, null);
    } else {
        http = initHttp(url, _POST, null);
    }
    OutputStream out = http.getOutputStream();
    out.write(params.getBytes(DEFAULT_CHARSET));
    out.flush();
    out.close();

    InputStream in = http.getInputStream();
    BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
    String valueString = null;
    StringBuffer bufferRes = new StringBuffer();
    while ((valueString = read.readLine()) != null) {
        bufferRes.append(valueString);
    }
    in.close();
    if (http != null) {
        http.disconnect();// 
    }
    return bufferRes.toString();
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.io.FileUtils.java

/**
 * Copies all the bytes from one file to another.
 *.//  w  w w  .  j av a2s  .co m
 * @param source the source file
 * @param destination the destination file
 * @throws IOException if an I/O error occurred
 */
public static void copy(File source, File destination) throws IOException {
    Validate.notNull(source, "source is null");
    Validate.notNull(destination, "destination is null");

    if (source.equals(destination)) {
        return;
    }

    InputStream input = null;
    OutputStream output = null;

    try {
        input = new FileInputStream(source);

        try {
            output = new FileOutputStream(destination);

            copy(input, output);
        } finally {
            if (output != null) {
                output.close();
            }
        }
    } finally {
        if (input != null) {
            input.close();
        }
    }
}

From source file:com.bahmanm.karun.Utils.java

/**
 * Extracts a gzip'ed tar archive./*from   ww  w  .  j  a  va2  s . c o  m*/
 * 
 * @param archivePath Path to archive
 * @param destDir Destination directory
 * @throws IOException 
 */
public synchronized static void extractTarGz(String archivePath, File destDir)
        throws IOException, ArchiveException {
    // copy
    File tarGzFile = File.createTempFile("karuntargz", "", destDir);
    copyFile(archivePath, tarGzFile.getAbsolutePath());

    // decompress
    File tarFile = File.createTempFile("karuntar", "", destDir);
    FileInputStream fin = new FileInputStream(tarGzFile);
    BufferedInputStream bin = new BufferedInputStream(fin);
    FileOutputStream fout = new FileOutputStream(tarFile);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin);
    final byte[] buffer = new byte[1024];
    int n = 0;
    while (-1 != (n = gzIn.read(buffer))) {
        fout.write(buffer, 0, n);
    }
    bin.close();
    fin.close();
    gzIn.close();
    fout.close();

    // extract
    final InputStream is = new FileInputStream(tarFile);
    ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) {
        OutputStream out;
        if (entry.isDirectory()) {
            File f = new File(destDir, entry.getName());
            f.mkdirs();
            continue;
        } else
            out = new FileOutputStream(new File(destDir, entry.getName()));
        IOUtils.copy(ain, out);
        out.close();
    }
    ain.close();
    is.close();
}

From source file:com.googlecode.fascinator.common.JsonConfig.java

/**
 * Performs a backup on the system-wide configuration file from the default
 * config dir if it exists. Returns a reference to the backed up file.
 *
 * @return the backed up system JSON file
 * @throws IOException if there was an error reading or writing either file
 *///  w w  w.ja  v  a 2  s .  c  o  m
public static File backupSystemFile() throws IOException {
    File configFile = new File(CONFIG_DIR, SYSTEM_CONFIG_FILE);
    File backupFile = new File(CONFIG_DIR, SYSTEM_CONFIG_FILE + ".old");
    if (!configFile.exists()) {
        throw new IOException("System file does not exist! '" + configFile.getAbsolutePath() + "'");
    } else {
        if (backupFile.exists()) {
            backupFile.delete();
        }
        OutputStream out = new FileOutputStream(backupFile);
        InputStream in = new FileInputStream(configFile);
        IOUtils.copy(in, out);
        in.close();
        out.close();
        log.info("Configuration copied to '{}'", backupFile);
    }
    return backupFile;
}

From source file:eu.project.ttc.tools.cli.TermSuiteCLIUtils.java

/**
 * Unconditionally close an <code>OutputStream</code>. Equivalent to
 * {@link OutputStream#close()}, except any exceptions will be ignored.
 * <p>// w ww  . j  av a  2s . c o m
 * Code from apache IOUtils.
 * 
 * @param output
 *            A (possibly null) OutputStream
 */
public static void closeQuietly(OutputStream output) {
    if (output == null) {
        return;
    }

    try {
        output.close();
    } catch (IOException ioe) {
    }
}

From source file:ie.pars.bnc.preprocess.MainBNCProcess.java

private static void getZippedFile() throws IOException, ArchiveException, Exception {
    String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger";
    String parseModel = LexicalizedParser.DEFAULT_PARSER_LOC;

    InputStream is = new FileInputStream(pathInput);
    TarArchiveInputStream tarStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;/*  w w w.j ava2s  .c o  m*/
    int countfiles = 0;
    while ((entry = (TarArchiveEntry) tarStream.getNextEntry()) != null) {
        //     for(File lf: listFiles){ 
        if (!entry.isDirectory()) {

            byte[] content = new byte[(int) entry.getSize()];
            int offset = 0;
            tarStream.read(content, offset, content.length - offset);
            String id = entry.getName().split("/")[entry.getName().split("/").length - 1].split(".xml")[0];

            if (!filesProcesed.contains(id) && id.startsWith(letter.toUpperCase())) {
                if (countfiles++ % 10 == 0) {
                    tagger = new MaxentTagger(taggerPath);
                    m = new Morphology();
                    parser = ParserGrammar.loadModel(parseModel);
                    parser.loadTagger();
                }
                System.out.print("Entry " + entry.getName());

                InputStream bis = new ByteArrayInputStream(content);
                StringBuilder parseBNCXML = ProcessNLP.parseBNCXML(bis, m, tagger, parser);
                bis.close();
                OutputStream out = new FileOutputStream(pathOutput + File.separatorChar + id + ".vert");
                Writer writer = new OutputStreamWriter(out, "UTF-8");

                writer.write("<text id=\"" + id + "\">\n");
                writer.write(parseBNCXML.toString());
                writer.write("</text>\n");
                writer.close();
                out.close();
            } else {
                System.out.println(">> Bypass Entry " + entry.getName());
            }
            //break;
        }

    }
    is.close();
    System.out.println("There are " + countfiles);
    //    tarStream.close();

}

From source file:Main.java

/**
 * Export the given bitmap to the specified {@code File}
 * @param context the context// ww  w.ja va2s.  c  om
 * @param bitmap the bitmap to export
 * @param file the file in which to save the view
 */
public static void exportBitmapToFile(@NonNull final Context context, @NonNull final Bitmap bitmap,
        @NonNull final File file) {
    OutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file, false));
        bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, outputStream);
        outputStream.flush();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            //No Worries
        }
    }
}