List of usage examples for java.io InputStream close
public void close() throws IOException
From source file:com.eryansky.common.utils.io.IoUtils.java
/** * Closes the given stream. The same as calling {@link java.io.InputStream#close()}, but * errors while closing are silently ignored. *///from w ww . ja v a 2 s.co m public static void closeSilently(InputStream inputStream) { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ignore) { // Exception is silently ignored } }
From source file:Main.java
public static String readAssetsFileString(Context context, String fileName) { String str = null;/*from w ww . j av a 2 s .c o m*/ try { InputStream is = context.getAssets().open(fileName); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); str = new String(buffer); } catch (IOException e) { e.printStackTrace(); } return str; }
From source file:Main.java
public static Bitmap decodeSampledBitmapFromStream(Context context, Uri uri, int reqWidth, int reqHeight) throws IOException { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/*from w w w .java 2 s. com*/ InputStream stream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(stream, null, options); stream.close(); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; stream = context.getContentResolver().openInputStream(uri); Bitmap bmp = BitmapFactory.decodeStream(stream, null, options); stream.close(); return bmp; }
From source file:Main.java
public static Document parseXmlInputStream(InputStream inputStream) { Document document = null;/*ww w . ja v a2 s . c o m*/ try { document = getDocumentBuilder().parse(inputStream); inputStream.close(); } catch (IOException e) { throw new RuntimeException("could not read xml stream", e); } catch (SAXException e) { throw new RuntimeException("could not parse xml document", e); } return document; }
From source file:Main.java
public static String loadJSONFromAsset(Context context, String jsonFile) { String json = null;//from w w w . j a v a 2 s.c o m try { InputStream is = context.getAssets().open(jsonFile); 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:com.voa.weixin.utils.WeixinUtils.java
public static Document getDocumentResource(String sourceName) throws Exception { String repertoryPath = Carp.ROOTPATH + sourceName; SAXReader reader = new SAXReader(); Document doc = null;// w ww . j a va 2 s . c om File repertoryFile = new File(repertoryPath); if (repertoryFile.exists() && repertoryFile.isFile()) { doc = reader.read(repertoryFile); } else { InputStream in = Carp.class.getResourceAsStream("/" + sourceName); doc = reader.read(in); in.close(); } return doc; }
From source file:Main.java
public static byte[] readAsset(Context context, String filename) throws IOException { InputStream in = context.getResources().getAssets().open(filename); try {// w ww. ja v a2 s . co m return readAllBytes(in); } finally { in.close(); } }
From source file:Main.java
public static String loadJSONFromAsset(Context context, String fileName) { String json;/*from w ww .j av a 2 s . c om*/ 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
/** * A method to load x.json files from assets folder and convert them * into a string.// w ww . j a va 2s .c om * * @param c A contect * @param fileName The json file in assets folder which to load json data from * @return */ public static String loadJSONFromAsset(Context c, String fileName) { String json = null; try { InputStream is = c.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
/** * Extract a resource into a real file//w ww.ja va 2s. c o m * * @param in typically given as getResources().openRawResource(R.raw.something) * @param name of the resulting file * @param directory target directory * @return the resulting file * @throws IOException */ public static File extractResource(InputStream in, String filename, File directory) throws IOException { int n = in.available(); byte[] buffer = new byte[n]; in.read(buffer); in.close(); File file = new File(directory, filename); FileOutputStream out = new FileOutputStream(file); out.write(buffer); out.close(); return file; }