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

private static void copyFile(File source, File destination) throws IOException {
    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(destination);

    byte[] buffer = new byte[1024];
    int length;//  w  ww .ja v  a2  s.c o  m
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:com.google.android.gcm.demo.app.ServerUtilities.java

/**
 * Issue a POST request to the server./*from w ww. j a v  a  2 s .  c  om*/
 *
 * @param endpoint POST address.
 * @param params request parameters.
 *
 * @throws IOException propagated from POST.
 */
private static void post(String endpoint, Map<String, String> params) throws IOException {
    URL url;
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    JSONObject jsonObj = new JSONObject(params);
    String body = jsonObj.toString();
    Log.v(TAG, "Posting '" + body + "' to " + url);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();
        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
            throw new IOException("Post failed with error code " + status);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:com.stealthyone.mcb.stbukkitlib.utils.FileUtils.java

public static File copyFileFromJar(JavaPlugin plugin, String fileName, File destination) throws IOException {
    Validate.notNull(plugin, "Plugin cannot be null.");
    Validate.notNull(fileName, "File name cannot be null.");
    Validate.notNull(destination, "Destination cannot be null.");

    InputStream in = plugin.getResource(fileName);
    if (in == null)
        throw new FileNotFoundException(
                "Unable to find file '" + fileName + "' in jar for plugin: '" + plugin.getName() + "'");

    OutputStream out = new FileOutputStream(destination);
    byte[] buf = new byte[1024];
    int len;//from  w  w w . j a v a2  s .c  om
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    out.close();
    in.close();
    return destination;
}

From source file:Main.java

/**
 * <pre>//from  w ww  .j  a v a2  s  .co m
 * Convenient method to copy file.
 * </pre>
 * @param in {@link InputStream} of source file
 * @param out {@link OutputStream} of destination file
 */
public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }

    in.close();
    out.flush();
    out.close();
}

From source file:com.picklecode.popflix.App.java

private static void loadLib(String name) {

    try {/*from w w  w .  j  a  v  a  2  s . c  o  m*/

        if (isWindows()) {
            name = name.substring("lib".length());
        }

        String ext = getExtension();
        name = name + ext;

        LOG.info(System.getProperty("os.arch"));
        LOG.info(System.getProperty("os.name"));
        Path tmp = Files.createTempDirectory("popflix");
        setLibraryPath(tmp.toString());
        LOG.info(tmp.toString() + "/" + name);
        File fileOut = new File(tmp.toString() + "/" + name);

        LOG.info(System.getProperty("java.library.path"));

        System.out.println("/lib/" + getFolder() + "/" + name);
        InputStream in = Popflix.class.getResourceAsStream("/lib/" + getFolder() + "/" + name);
        if (in != null) {

            OutputStream out = FileUtils.openOutputStream(fileOut);
            IOUtils.copy(in, out);
            in.close();
            out.close();

        }
        System.load(fileOut.getAbsolutePath());//loading goes here
    } catch (Exception e) {
        LOG.error(e.getMessage());
        System.exit(-1);
    }
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

private static void writeBody(HttpURLConnection connection, List<String> names) throws IOException {
    OutputStream stream = connection.getOutputStream();
    String body = JSONArray.toJSONString(names);
    stream.write(body.getBytes());/*from  www  .j  a v  a2  s. c  o  m*/
    stream.flush();
    stream.close();
}

From source file:Main.java

public static void writeObjectToFile(Object oObject, File destDir, String filename) throws IOException {
    File dest = new File(destDir, filename);
    if (dest.exists())
        dest.delete();/*from   www.j av a2s .c o  m*/

    OutputStream outStream = null;
    try {
        outStream = new BufferedOutputStream(new FileOutputStream(dest));
        outStream.write((byte[]) oObject);
    } finally {
        if (outStream != null) {
            outStream.close();
        }
    }
}

From source file:com.googlecode.t7mp.util.ZipUtil.java

public static void unzip(InputStream warInputStream, File destination) {
    try {/*  ww w . ja v a 2s .c o  m*/
        ZipArchiveInputStream in = null;
        try {
            in = new ZipArchiveInputStream(warInputStream);

            ZipArchiveEntry entry = null;
            while ((entry = in.getNextZipEntry()) != null) {
                File outfile = new File(destination.getCanonicalPath() + "/" + entry.getName());
                outfile.getParentFile().mkdirs();
                if (entry.isDirectory()) {
                    outfile.mkdir();
                    continue;
                }
                OutputStream o = new FileOutputStream(outfile);
                try {
                    IOUtils.copy(in, o);
                } finally {
                    o.close();
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        warInputStream.close();
    } catch (FileNotFoundException e) {
        throw new TomcatSetupException(e.getMessage(), e);
    } catch (IOException e) {
        throw new TomcatSetupException(e.getMessage(), e);
    }
}

From source file:Main.java

/**
 * Executes a post request using {@link HttpURLConnection}.
 *
 * @param url The request URL./*from w  w  w.ja va  2  s .  c o m*/
 * @param data The request body, or null.
 * @param requestProperties Request properties, or null.
 * @return The response body.
 * @throws IOException If an error occurred making the request.
 */
// TODO: Remove this and use HttpDataSource once DataSpec supports inclusion of a POST body.
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        // Write the request body, if there is one.
        if (data != null) {
            OutputStream out = urlConnection.getOutputStream();
            try {
                out.write(data);
            } finally {
                out.close();
            }
        }
        // Read and return the response body.
        InputStream inputStream = urlConnection.getInputStream();
        try {
            return toByteArray(inputStream);
        } finally {
            inputStream.close();
        }
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T//  w  w  w .  j  a v a 2 s  .com
 * @param object the object to be transformed into XML
 * @param file the output file
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, File file) throws JAXBException, IOException {
    OutputStream stream = new FileOutputStream(file);

    try {
        marshal(element, object, stream);

        stream.flush();
        stream.close();
    } catch (JAXBException e) {
        IOUtils.closeQuietly(stream);

        throw e;
    } catch (IOException e) {
        IOUtils.closeQuietly(stream);

        throw e;
    }
}