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:com.splout.db.common.CompressorUtil.java

public static void uncompress(File file, File dest) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(dest, entry.getName());
        entryDestination.getParentFile().mkdirs();
        InputStream in = zipFile.getInputStream(entry);
        OutputStream out = new FileOutputStream(entryDestination);
        IOUtils.copy(in, out);//from   w  ww .java2s  .co m
        in.close();
        out.close();
    }
}

From source file:de.ingrid.interfaces.csw.admin.command.AdminManager.java

/**
 * Read the override configuration and convert the clear text password to a bcrypt-hash,
 * which is used and needed by the base-webapp v3.6.1 
 *///from w w  w.j  a  va 2 s  . c o m
private static void migratePassword() {
    try {
        InputStream is = new FileInputStream("conf/config.properties");
        Properties props = new Properties();
        props.load(is);
        String oldPassword = props.getProperty("ingrid.admin.password");
        is.close();

        props.setProperty("ingrid.admin.password", BCrypt.hashpw(oldPassword, BCrypt.gensalt()));

        OutputStream os = new FileOutputStream("conf/config.override.properties");
        props.store(os, "Override configuration written by the application");
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.publicuhc.pluginframework.util.UUIDFetcher.java

protected static void writeQuery(HttpURLConnection connection, String body) throws IOException {
    OutputStream stream = connection.getOutputStream();
    stream.write(body.getBytes());/*  w w  w. j  a  v  a 2  s .co  m*/
    stream.flush();
    stream.close();
}

From source file:com.nridge.core.base.io.IO.java

public static void closeQuietly(OutputStream aStream) {
    if (aStream != null) {
        try {/*from w ww.  j a  v  a 2  s  .  co m*/
            aStream.close();
        } catch (Exception ignored) {
        }
    }
}

From source file:dk.nsi.minlog.test.utils.TestHelper.java

public static String sendRequest(String url, String action, String docXml, boolean failOnError)
        throws IOException, ServiceException {
    URL u = new URL(url);
    HttpURLConnection uc = (HttpURLConnection) u.openConnection();
    uc.setDoOutput(true);/*from  w  ww . j  a  va  2  s  .c  o m*/
    uc.setDoInput(true);
    uc.setRequestMethod("POST");
    uc.setRequestProperty("SOAPAction", "\"" + action + "\"");
    uc.setRequestProperty("Content-Type", "text/xml; charset=utf-8;");
    OutputStream os = uc.getOutputStream();

    IOUtils.write(docXml, os, "UTF-8");
    os.flush();
    os.close();

    InputStream is;
    if (uc.getResponseCode() != 200) {
        is = uc.getErrorStream();
    } else {
        is = uc.getInputStream();
    }
    String res = IOUtils.toString(is);

    is.close();
    if (uc.getResponseCode() != 200 && (uc.getResponseCode() != 500 || failOnError)) {
        throw new ServiceException(res);
    }
    uc.disconnect();

    return res;
}

From source file:Main.java

/**
 * Issue a POST request to the server./*from ww  w  .j  a v  a 2  s . c  om*/
 *
 * @param endpoint
 *            POST address.
 * @param params
 *            request parameters.
 *
 * @throws java.io.IOException
 *             propagated from POST.
 */
public static String 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);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    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/x-www-form-urlencoded;charset=UTF-8");
        // 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);
        }

        // Get Response
        InputStream is = conn.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\n');
        }
        rd.close();
        return response.toString();

    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:Main.java

public static String imageToLocal(Bitmap bitmap, String name) {
    String path = null;/*from   w  w  w.ja  v  a 2s . c  o m*/
    try {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return null;
        }
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/cache/";
        File file = new File(filePath, name);
        if (file.exists()) {
            path = file.getAbsolutePath();
            return path;
        } else {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        path = file.getAbsolutePath();
    } catch (Exception e) {
    }
    return path;
}

From source file:Main.java

/**
 * Issue a POST request to the server./*from   w  w  w  .  ja v  a  2  s. co  m*/
 *
 * @param endpoint
 *            POST address.
 * @param params
 *            request parameters.
 *
 * @throws IOException
 *             propagated from POST.
 */
public static String post_t(String endpoint, Map<String, Object> params, String contentType)
        throws IOException {
    URL url;
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, Object> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    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", contentType);
        // 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);
        }

        // Get Response
        InputStream is = conn.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\n');
        }
        rd.close();
        return response.toString();

    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:br.gov.jfrj.siga.base.ConexaoHTTP.java

public static String get(String URL, HashMap<String, String> header, Integer timeout, String payload)
        throws AplicacaoException {

    try {// w  ww . ja  va  2s.co  m

        HttpURLConnection conn = (HttpURLConnection) new URL(URL).openConnection();

        if (timeout != null) {
            conn.setConnectTimeout(timeout);
            conn.setReadTimeout(timeout);
        }

        //conn.setInstanceFollowRedirects(true);

        if (header != null) {
            for (String s : header.keySet()) {
                conn.setRequestProperty(s, header.get(s));
            }
        }

        System.setProperty("http.keepAlive", "false");

        if (payload != null) {
            byte ab[] = payload.getBytes("UTF-8");
            conn.setRequestMethod("POST");
            // Send post request
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(ab);
            os.flush();
            os.close();
        }

        //StringWriter writer = new StringWriter();
        //IOUtils.copy(conn.getInputStream(), writer, "UTF-8");
        //return writer.toString();
        return IOUtils.toString(conn.getInputStream(), "UTF-8");

    } catch (IOException ioe) {
        throw new AplicacaoException("No foi possvel abrir conexo", 1, ioe);
    }

}

From source file:com.ALC.SC2BOAserver.util.SC2BOAserverFileUtil.java

/**
 * This method extracts data to a given directory.
 * // w w w .java2 s .  c  o  m
 * @param directory the directory to extract into
 * @param zipIn input stream pointing to the zip file
 * @throws ArchiveException
 * @throws IOException
 * @throws FileNotFoundException
 */

public static void extractZipToDirectory(File directory, InputStream zipIn)
        throws ArchiveException, IOException, FileNotFoundException {

    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", zipIn);
    while (true) {
        ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry();
        if (entry == null) {
            in.close();
            break;
        }
        //Skip empty files
        if (entry.getName().equals("")) {
            continue;
        }

        if (entry.isDirectory()) {
            File file = new File(directory, entry.getName());
            file.mkdirs();
        } else {
            File outFile = new File(directory, entry.getName());
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(outFile);
            IOUtils.copy(in, out);
            out.flush();
            out.close();
        }
    }
}