Example usage for java.io InputStream close

List of usage examples for java.io InputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:com.dragome.callbackevictor.serverside.utils.RewritingUtils.java

public static byte[] toByteArray(File f) throws IOException {
    InputStream in = new FileInputStream(f);
    try {//from  ww  w .  j  av  a2s. co  m
        return toByteArray(in);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static boolean copyFile(File srcFile, File destFile) {
    boolean result = false;
    try {/*w  w  w.j av a2 s  . c o m*/
        InputStream in = new FileInputStream(srcFile);
        try {
            result = copyToFile(in, destFile);
        } finally {
            in.close();
        }
    } catch (IOException e) {
        result = false;
    }
    return result;
}

From source file:com.webkruscht.wmt.DownloadFiles.java

private static void getProperties() throws IOException {
    Properties p = new Properties();
    String propFile = "wmt.properties";
    InputStream propStream = DownloadFiles.class.getClassLoader().getResourceAsStream(propFile);
    p.load(propStream);/*  ww  w  .  ja  va  2s  .com*/
    propStream.close();
    username = p.getProperty("username");
    password = p.getProperty("password");
    filePath = p.getProperty("filePath");
}

From source file:facturatron.facturacion.PAC.finkok.ClassicKeyLoader.java

private static byte[] getBytes(InputStream in) throws IOException {
    try {/* www . j a  v a 2s.c o  m*/
        return ByteStreams.toByteArray(in);
    } finally {
        in.close();
    }
}

From source file:io.helixservice.feature.configuration.cloudconfig.CloudConfigDecrypt.java

/**
 * Decrypt an encrypted string//from w  ww .jav  a 2  s. c o m
 * <p>
 * This method blocks on a HTTP request.
 *
 * @param name property or filename for reference/logging
 * @param encryptedValue Encrypted string
 * @param cloudConfigUri URI of the Cloud Config server
 * @param httpBasicHeader HTTP Basic header containing username and password for Cloud Config server
 * @return
 */
public static String decrypt(String name, String encryptedValue, String cloudConfigUri,
        String httpBasicHeader) {
    String result = encryptedValue;

    // Remove prefix if needed
    if (encryptedValue.startsWith(CIPHER_PREFIX)) {
        encryptedValue = encryptedValue.substring(CIPHER_PREFIX.length());
    }

    String decryptUrl = cloudConfigUri + "/decrypt";

    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(decryptUrl).openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.setRequestMethod("POST");
        connection.addRequestProperty(AUTHORIZATION_HEADER, httpBasicHeader);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Content-Length", Integer.toString(encryptedValue.getBytes().length));
        connection.setRequestProperty("Accept", "*/*");

        // Write body
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(encryptedValue.getBytes());
        outputStream.close();

        if (connection.getResponseCode() == 200) {
            InputStream inputStream = connection.getInputStream();
            result = IOUtils.toString(inputStream);
            inputStream.close();
        } else {
            LOG.error("Unable to Decrypt name=" + name + " due to httpStatusCode="
                    + connection.getResponseCode() + " for decryptUrl=" + decryptUrl);
        }
    } catch (IOException e) {
        LOG.error("Unable to connect to Cloud Config server at decryptUrl=" + decryptUrl, e);
    }

    return result;
}

From source file:com.hurence.logisland.util.file.FileUtil.java

/**
 * load logs from resources as a string or return null if an error occurs
 *///from w ww .jav a 2  s  . com
public static String loadFileContentAsString(String path, String encoding) {
    try {
        final InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(path);
        assert is != null;
        byte[] encoded = IOUtils.toByteArray(is);
        is.close();
        return new String(encoded, encoding);
    } catch (Exception e) {
        logger.error(String.format("Could not load json file %s and convert to string", path), e);
        return null;
    }
}

From source file:com.hurence.logisland.util.file.FileUtil.java

/**
 * load file from resources as a bytes array or return null if an error occurs
 *//*www. jav a 2  s .c om*/
public static byte[] loadFileContentAsBytes(String path) {
    try {
        final InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(path);
        assert is != null;
        byte[] encoded = IOUtils.toByteArray(is);
        is.close();
        return encoded;
    } catch (Exception e) {
        logger.error(String.format("Could not load file %s and convert it to a bytes array", path), e);
        return null;
    }
}

From source file:edu.jhu.pha.vospace.DbPoolServlet.java

public static void close(InputStream in) {
    if (in != null) {
        try {//w w w .j a  v a  2 s.  c  o  m
            in.close();
        } catch (Exception ignored) {
        }
    }
}

From source file:com.gtcgroup.jped.rest.helper.JprResponseUtilHelper.java

/**
 * @param entityStream/*  www . j ava 2 s.c o  m*/
 * @param scanner
 * @throws IOException
 */
private static void closeResources(final InputStream entityStream) {
    try {
        entityStream.close();
    } catch (@SuppressWarnings("unused") final Exception e) {
        // Ignore
    }
}

From source file:Util.java

public static void copyInputstreamToFile(InputStream in, OutputStream out) {
    try {/*  w ww.j  a  va2 s .  com*/
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException ex) {

    }
}