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:de.tudarmstadt.ukp.dkpro.core.api.resources.CompressionUtilsTest.java

private static void testCompression(CompressionMethod compressionMethod) throws IOException {
    String text = StringUtils.repeat("This is a test. ", 100000);

    File file = new File("compressed" + compressionMethod.getExtension());

    OutputStream os = CompressionUtils.getOutputStream(file);
    os.write(text.getBytes());/*from w  ww.  j  a v a  2s .c  om*/
    os.close();
    InputStream is = CompressionUtils.getInputStream(file.getPath(), new FileInputStream(file));
    assertEquals(text, IOUtils.toString(is));
    is.close();
    file.delete();
}

From source file:Main.java

public static Bitmap decodeUriAsBitmap(Context context, Uri uri, BitmapFactory.Options options) {

    Bitmap result = null;/*from  ww w .  jav  a 2  s .  com*/

    if (uri != null) {
        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = null;
        try {
            inputStream = cr.openInputStream(uri);
            result = BitmapFactory.decodeStream(inputStream, null, options);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:Main.java

public static void copyAssets(Context context, String assetsName, String destFilePath) throws IOException {
    File file = new File(destFilePath);
    FileOutputStream out = new FileOutputStream(file);

    InputStream in = context.getAssets().open(assetsName);

    byte[] buf = new byte[1024];
    int len;/*from   w w  w .j  av  a  2 s  .c o  m*/
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:com.santiagolizardo.madcommander.util.io.FileOperations.java

public static boolean copy(InputStream is, File file) {
    try {// w  w  w .  j a  v  a 2s  . com
        try (FileOutputStream fos = new FileOutputStream(file)) {
            IOUtils.copy(is, fos);
            is.close();
        }

        return true;
    } catch (IOException e) {
        logger.warning(e.getMessage());
        return false;
    }
}

From source file:Main.java

public static Bitmap getBitmap(Context context, int resId) {
    try {//w  ww  .  j av a 2 s .  co  m
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        InputStream is = context.getResources().openRawResource(resId);
        Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options);
        is.close();
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

/**
 * @param xmlFile is the file to read//from  www.j  a va2s  . c o  m
 * @param compress if true, the file is assumed to be compressed with GZIP
 * @return the Document contained in that file
 */
public static Document readFile(File xmlFile, boolean compress) {
    ensureFactory();
    try {
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        //builder.setEntityResolver(new EntityUtils());
        InputStream is = new FileInputStream(xmlFile);
        if (compress)
            is = new GZIPInputStream(is);
        Document ret = builder.parse(is);
        is.close();
        return ret;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.apache.openejb.arquillian.tests.jaxws.wsrs.WsRsTest.java

public static String asString(final HttpResponse execute) throws IOException {
    final InputStream in = execute.getEntity().getContent();
    try {//ww w  .  j  av a2s  . com
        return IO.slurp(in);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static void saveImage(String imageUrl, String destinationFile) throws Exception {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;//www . java 2  s .  co  m

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

From source file:Main.java

public static void parse(final XMLReader xmlReader, final URL url) throws IOException, SAXException {
    final InputStream inputStream = url.openStream();
    try {/* w  ww  .  jav  a2  s.c  o  m*/
        final InputSource inputSource = new InputSource(inputStream);
        xmlReader.parse(inputSource);
    } finally {
        inputStream.close();
    }

}

From source file:Main.java

public static String SimpleHttp(String urlStr) throws Exception {

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

    int resCode = conn.getResponseCode();
    InputStream input = null;
    String result = null;//from   w w  w.  j av  a 2 s  .com
    if (resCode == 200) {
        input = conn.getInputStream();
        result = toString(input);
        input.close();
        conn.disconnect();
    } else {
        throw new Exception("connect failed");
    }

    return result;
}