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:Util.java

/**
 * Charge une url et renvoie le contenu/*from  www .  j  a  va 2s . co m*/
 * 
 * @param url
 * @return le contenu de l'url ou "" si erreur
 */
public static String loadUrl(URL url) throws IOException {
    InputStream stream = null;
    try {
        stream = url.openStream();
        return loadStream(stream);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:StreamsUtils.java

public static void inputStream2OutputStream(InputStream stream, OutputStream out) throws IOException {
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        out.write(buf, 0, readedBytes);//from w w w .j a  v  a  2 s . c  o m
    }
    stream.close();
    out.close();
}

From source file:Main.java

/**
 * Executes a post request using {@link HttpURLConnection}.
 *
 * @param url The request URL.//ww w.ja v a2  s .co 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:Main.java

static void copyData(InputStream from, OutputStream to) throws IOException {
    byte[] buffer = new byte[1024];
    int length;//from   w ww . j a  v  a 2 s. c  o  m
    while ((length = from.read(buffer)) > 0) {
        to.write(buffer, 0, length);
    }
    to.flush();
    to.close();
    from.close();
}

From source file:Main.java

@Deprecated
private static void copy(InputStream in, File dst) throws IOException {
    FileOutputStream out = new FileOutputStream(dst);
    byte[] buf = new byte[1024];
    int len;//from   w ww  .  j a  v a2 s  . c  om

    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

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

From source file:Main.java

static void copyFile(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;//from w  ww.j a v  a2s .c o  m
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:com.martinwunderlich.nlp.arg.aifdb.AIFdbArgumentMapFactory.java

public static AIFdbArgumentMap buildFromJsonFile(String jsonFilePath) throws RuntimeException {
    InputStream is = null;
    AIFdbArgumentMap map = null;/* w  ww. j  a  va2  s  .c o  m*/
    try {
        is = new FileInputStream(jsonFilePath);

        map = getJsonObject(is);
        is.close();
    } catch (Exception ex) {
        throw new RuntimeException("Error while trying to build map: " + ex.getMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        }
    }

    map.init();
    map.setSourceFile(jsonFilePath.substring(jsonFilePath.lastIndexOf(File.separator) + 1));

    return map;
}

From source file:hudson.plugins.jobConfigHistory.TUtils.java

static List<String> readResourceLines(final String resourceName) throws IOException {
    final InputStream stream = TUtils.class.getResourceAsStream(resourceName);
    try {//from w  w w.  j av a  2  s .c o  m
        return IOUtils.readLines(stream, "UTF-8");
    } finally {
        stream.close();
    }
}

From source file:Main.java

public static Templates getTemplatesByName(File xslF) {

    Templates templates = null;// w w  w  . j av  a2 s.  co  m
    TransformerFactory tfactory = TransformerFactory.newInstance();
    tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental", java.lang.Boolean.TRUE);

    InputStream is = null;
    try {
        StreamSource ss = new StreamSource(xslF);
        is = ss.getInputStream();
        templates = tfactory.newTemplates(ss);
        if (is != null) {
            is.close();
            is = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
                is = null;
            }
        } catch (Throwable t) {
            System.out.println(t);
        }
    }

    return templates;
}

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 
 *///w  w w  . j av a2 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();
    }
}