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:de.ingrid.iplug.csw.dsc.TestUtil.java

public static void copy(InputStream inputStream, OutputStream outputStream, boolean close) throws IOException {
    byte[] buf = new byte[1024];
    int len;/*from   w w  w . j a  v a  2  s  . c  om*/
    while ((len = inputStream.read(buf)) > 0) {
        outputStream.write(buf, 0, len);
    }
    if (close) {
        inputStream.close();
        outputStream.close();
    }
}

From source file:Utils.java

public static void download(String urlStr, File destFile) throws IOException {
    URL srcUrl = new URL(urlStr);

    InputStream input = null;//w ww.ja v  a2 s .  c  om
    OutputStream output = null;
    try {
        input = srcUrl.openStream();
        FileOutputStream fos = new FileOutputStream(destFile);
        output = new BufferedOutputStream(fos);
        copyStreams(input, output);
    } finally {
        if (input != null) {
            input.close();
        }
        if (output != null) {
            output.flush();
            output.close();
        }
    }
}

From source file:gridool.db.partitioning.phihash.csv.distmm.InMemoryIndexHelper.java

/**
 * Synchronization is required./*from www .j a v  a  2s . com*/
 */
public static void writeToFile(final InputStream in) throws IOException {
    final byte[] recordBuf = new byte[2048]; // big buffer enough for a record
    final Map<String, OutputStream> outputMap = new HashMap<String, OutputStream>(12);
    while (in.available() > 0) {
        String fkIdxName = IOUtils.readString(in);
        int bucket = IOUtils.readInt(in);
        int recordlen = IOUtils.readInt(in);
        IOUtils.readFully(in, recordBuf, 0, recordlen);

        OutputStream out = prepareFkOutputStream(fkIdxName, bucket, outputMap);
        out.write(recordBuf, 0, recordlen);
    }
    for (OutputStream out : outputMap.values()) {
        out.flush();
        out.close();
    }
}

From source file:in.goahead.apps.util.URLUtils.java

public static void DownloadStream(InputStream inputStream, String outputFile, long skipLength)
        throws IOException {
    File outputFileObj = new File(outputFile);
    boolean appendStreamData = false;
    if (skipLength > 0) {
        appendStreamData = true;/*from   w  w w.j a va2  s. c  o m*/
        skipLength = outputFileObj.length();
    }
    OutputStream outputStream = new FileOutputStream(outputFile, appendStreamData);
    //   if(inputStream.skip(skipLength) == skipLength) {
    DownloadStream(inputStream, outputStream);
    //   }
    outputStream.flush();
    outputStream.close();
}

From source file:Main.java

public static void writeToOutputStream(OutputStream os, Document document)
        throws TransformerConfigurationException, TransformerException, FileNotFoundException,
        UnsupportedEncodingException, IOException {

    StreamResult result = new StreamResult(os);

    try {//  w w w . j a  v a2s . co  m
        writeTo(result, document);
    } finally {
        if (os != null) {
            os.close();
        }
    }
}

From source file:FileUtils.java

public static void writeFile(File file, byte[] data) throws IOException {

    OutputStream stream = new FileOutputStream(file);
    try {//from   w  w  w  .ja v a2s.c o m
        stream.write(data);
    } finally {
        try {
            stream.flush();
        } finally {
            stream.close();
        }
    }
}

From source file:Main.java

private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[4096];

    try {/*w w  w .  j  av  a  2s  . co  m*/
        while (true) {
            int bytes = in.read(buffer);
            if (bytes == -1)
                break;

            out.write(buffer, 0, bytes);
        }

        out.flush();
    } finally {
        try {
            in.close();
        } finally {
            out.close();
        }
    }
}

From source file:com.github.egonw.rrdf.RJenaHelper.java

public static void saveRdf(Model model, String filename, String format) throws Exception {
    File file = new File(filename);
    OutputStream stream = new FileOutputStream(file);
    model.write(stream, format);//from ww w . jav  a 2 s .  c  om
    stream.close();
}

From source file:com.ery.ertc.estorm.util.IOUtils.java

/**
 * Copies from one stream to another./*from www.  jav a  2 s  . co m*/
 * 
 * @param in
 *            InputStrem to read from
 * @param out
 *            OutputStream to write to
 * @param buffSize
 *            the size of the buffer
 * @param close
 *            whether or not close the InputStream and OutputStream at the
 *            end. The streams are closed in the finally clause.
 */
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException {

    try {
        copyBytes(in, out, buffSize);
    } finally {
        if (close) {
            out.close();
            in.close();
        }
    }
}

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

/**
 * Gets the system-wide configuration file from the default config dir. If
 * the file doesn't exist, a default is copied to the config dir.
 * /*ww w  . j  av a2 s .com*/
 * @return the system JSON file
 * @throws IOException if there was an error reading or writing the system
 *             configuration file
 */
public static File getSystemFile() throws IOException {
    File configFile = new File(CONFIG_DIR, SYSTEM_CONFIG_FILE);
    if (!configFile.exists()) {
        configFile.getParentFile().mkdirs();
        OutputStream out = new FileOutputStream(configFile);
        IOUtils.copy(JsonConfig.class.getResourceAsStream("/" + SYSTEM_CONFIG_FILE), out);
        out.close();
        log.info("Default configuration copied to '{}'", configFile);
    }
    return configFile;
}