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

public static void doEntry(Context context, ZipOutputStream zout, int resId, String dest) throws Exception {
    InputStream in = null;
    try {// www  .j  a  v a2  s.c  o m
        zout.putNextEntry(new ZipEntry(dest));
        copyStream(in = context.getResources().openRawResource(resId), zout);
    } finally {
        zout.closeEntry();
        in.close();
    }
}

From source file:Main.java

public static XMLReader parse(InputStream in, ContentHandler handler) throws SAXException, IOException {

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(handler);/*  w w w .  j av a 2  s .c o m*/
    reader.parse(new InputSource(in));
    in.close();
    return reader;
}

From source file:com.richtodd.android.repository.JSONUtility.java

public static JSONObject loadJSONObject(ContentResolver contentResolver, Uri uri) throws RepositoryException {
    try {/*from w  w w.  ja v a  2 s .c o  m*/
        InputStream in = contentResolver.openInputStream(uri);
        try {
            return loadJSONObject(in);
        } finally {
            in.close();
        }
    } catch (FileNotFoundException ex) {
        throw new RepositoryException(ex);
    } catch (IOException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:Main.java

public static Document getXML(File file) throws Exception {
    InputStream in = new FileInputStream(file);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
    Document xml = docBuilder.parse(in);
    in.close();
    return xml;/* www  . ja va 2  s  . com*/
}

From source file:httputils.HttpUtil.java

public static JsonObject convertResponseToJson(HttpResponse response) throws ParseException, IOException {
    InputStream in = response.getEntity().getContent();
    JsonReader reader = Json.createReader(in);
    JsonObject jObj = reader.readObject();
    reader.close();/*  w  w w . j  ava  2  s .c  om*/
    in.close();

    return jObj;
}

From source file:Main.java

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;//  w  w  w  .  ja  v  a 2s  . co  m
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    return sb.toString();
}

From source file:com.lightboxtechnologies.nsrl.SmallTableLoader.java

protected static void load(FileSystem fs, String filename, LineHandler lh, RecordLoader loader)
        throws IOException {
    InputStream in = null;
    try {/*from  ww w. j a va2 s. c om*/
        in = fs.open(new Path(filename));
        loader.load(in, lh);
        in.close();
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:Main.java

public static void streamCopy(final InputStream bodyIs, final OutputStream out) throws IOException {
    byte[] buffer = new byte[2048];
    int read;/*w ww  .  ja  v a  2 s .co m*/
    while ((read = bodyIs.read(buffer)) > 0) {
        out.write(buffer, 0, read);
    }
    bodyIs.close();
    out.close();
}

From source file:Main.java

/** Loads properties into a file.
 * <P>This assumes the file was written with the
 * <code>Properties.store()</code> method.
 *///www  .  j  a v  a 2 s  .  c  o m
public static void load(Properties p, File file) throws IOException {
    InputStream in = null;
    try {
        in = new FileInputStream(file);
        p.load(in);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
            }
        }
    }
}

From source file:Main.java

/**
 * Get Bitmap from Uri/*www . j av  a  2 s.c  om*/
 *
 * @param uri Uri to get Bitmap
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static Bitmap getImageFromUri(Activity activity, Uri uri, File file) throws IOException {

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;//optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    if (file != null) {
        BitmapFactory.decodeFile(file.getAbsolutePath(), onlyBoundsOptions);
    } else {
        InputStream input = activity.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
        input.close();
    }

    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;

    float scale = activity.getResources().getDisplayMetrics().density;
    int pHeight = (int) (activity.getResources().getConfiguration().screenHeightDp * scale + 0.5f);
    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
            : onlyBoundsOptions.outWidth;

    double ratio = (originalSize > pHeight) ? (originalSize / pHeight) : 1.0;

    int REQUIRED_SIZE = activity.getResources().getDisplayMetrics().heightPixels / 2;

    /**/
    int Scale = 1;
    while (onlyBoundsOptions.outWidth / Scale / 2 >= REQUIRED_SIZE
            && onlyBoundsOptions.outHeight / Scale / 2 >= REQUIRED_SIZE) {
        Scale *= 2;
    }
    /**/

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = Scale;//getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;//optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional

    Bitmap bitmap;
    if (file != null) {
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
    } else {
        InputStream input = activity.getContentResolver().openInputStream(uri);
        bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
    }

    return bitmap;
}