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

/**
 * Marshall the xml to a document//from  ww w. j  ava  2 s . com
 *
 * @param xml the xml
 * @return the xml document parsed from the
 * @throws Exception if any
 */
public static Document xmlToDocument(String xml) throws Exception {
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputStream incomingIs = new ByteArrayInputStream(xml.getBytes());
    Document doc = db.parse(incomingIs);
    incomingIs.close();

    return doc;
}

From source file:Main.java

/**
 * Writes the given byte array as a local file.
 *
 * @param array the byte array to read from
 * @param outputFileName the name of the file to write to
 * @return the URL for the local file// ww  w . j av  a 2  s .c  om
 */
public static String writeFile(byte[] array, String outputFileName) throws IOException {
    InputStream in = new ByteArrayInputStream(array);
    try {
        return writeStreamToFile(in, outputFileName);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static java.awt.image.BufferedImage decodeToImage(String imageString) {

    java.awt.image.BufferedImage image = null;
    try {/*from  w ww  .jav a  2s.c  o m*/
        String imgString = imageString.startsWith("data:image/")
                ? imageString.substring(imageString.indexOf(',') + 1)
                : imageString;
        java.io.InputStream stream = new java.io.ByteArrayInputStream(
                java.util.Base64.getDecoder().decode(imgString));
        image = javax.imageio.ImageIO.read(stream);
        stream.close();
    } catch (Exception e) {
    }
    return image;
}

From source file:Main.java

/**
 * Copies the contents of one local file to another local file.
 *
 * @param inputFileName the name of the file to read to
 * @param outputFileName the name of the file to write to
 * @return the URL for the local file/*from  w w w  .  j  a  va 2s  .  co  m*/
 */
public static String copyFile(String inputFileName, String outputFileName) throws IOException {
    InputStream in = new FileInputStream(inputFileName);
    try {
        return writeStreamToFile(in, outputFileName);
    } finally {
        in.close();
    }
}

From source file:ch.ethz.coss.nervousnet.hub.ui.ShowcaseActivity.java

public static String parseJSONFile(String res, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(res);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);/*www.  j  a v a  2  s  . c  o  m*/
    file.close();

    return new String(formArray);
}

From source file:com.newrelic.agent.deps.org.apache.http.conn.util.PublicSuffixMatcherLoader.java

public static PublicSuffixMatcher load(final File file) throws IOException {
    Args.notNull(file, "File");
    final InputStream in = new FileInputStream(file);
    try {/*w w w .  j a  va  2s.  com*/
        return load(in);
    } finally {
        in.close();
    }
}

From source file:com.rogiel.httpchannel.util.HttpClientUtils.java

public static String toString(HttpResponse response) throws IOException {
    final InputStream in = response.getEntity().getContent();
    try {// w  w w .j  a  v a  2 s .c o m
        return IOUtils.toString(in);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String filename) {
    String json = null;//from   ww  w  . j a va2s.com
    try {

        InputStream is = context.getAssets().open(filename);

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

From source file:Main.java

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

    byte[] buf = new byte[1024];
    int len;//ww w .  ja v a  2 s .  com
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static Bitmap getGoogleMapThumbnail(double latitude, double longitude) {
    String URL = "http://maps.google.com/maps/api/staticmap?center=" + latitude + "," + longitude
            + "&zoom=15&size=600x600&sensor=false";

    Bitmap bmp = null;//  w w  w .  ja va2  s  .co  m
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);

    InputStream in = null;
    try {
        in = httpclient.execute(request).getEntity().getContent();
        bmp = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bmp;
}