List of usage examples for java.io InputStream close
public void close() throws IOException
From source file:Main.java
/** @see http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue */ public static int getSampleSize(Context context, Uri uri, float maxSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//from w ww .j a v a 2 s . c om if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { try { InputStream stream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(stream, null, options); stream.close(); } catch (Exception e) { e.printStackTrace(); } } else BitmapFactory.decodeFile(uri.getPath(), options); int longSide = Math.max(options.outHeight, options.outWidth); return getSampleSize(longSide, maxSize); }
From source file:Main.java
public static Bitmap decodeFileFromDrawable(int id, int maxSize, Context context) { Bitmap b = null;/* w w w .ja v a 2 s . com*/ try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; InputStream inputStream = context.getResources().openRawResource(id); BitmapFactory.decodeStream(inputStream, null, o); inputStream.close(); int scale = 1; if (o.outHeight > maxSize || o.outWidth > maxSize) { scale = (int) Math.pow(2, (int) Math .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; inputStream = context.getResources().openRawResource(id); b = BitmapFactory.decodeStream(inputStream, null, o2); inputStream.close(); } catch (IOException e) { } return b; }
From source file:Main.java
/** * Creates a copy of the file/* w ww .j a v a 2s . co m*/ * @param in Source file * @param out Destination file * @throws IOException if the operation fails. * */ public static void copyFile(File in, File out) throws IOException { InputStream in_stream = new FileInputStream(in); OutputStream out_stream = new FileOutputStream(out); copyStream(in_stream, out_stream); in_stream.close(); out_stream.close(); }
From source file:jfix.zk.Medias.java
public static File asFile(Media media) { try {/* w ww . j av a 2 s .co m*/ File directory = Files.createTempDirectory("jfix-media").toFile(); directory.deleteOnExit(); File file = new File(directory.getPath() + File.separator + media.getName()); OutputStream output = new FileOutputStream(file); if (media.isBinary()) { InputStream input = Medias.asStream(media); IOUtils.copy(input, output); input.close(); } else { Reader input = Medias.asReader(media); IOUtils.copy(input, output); input.close(); } output.close(); return file; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:Main.java
public static Object getObject(ByteBuffer byteBuffer) throws ClassNotFoundException, IOException { InputStream input = new ByteArrayInputStream(byteBuffer.array()); ObjectInputStream oi = new ObjectInputStream(input); Object obj = oi.readObject(); input.close(); oi.close();/*ww w .ja v a 2 s . co m*/ byteBuffer.clear(); return obj; }
From source file:com.codingrhemes.steamsalesmobile.HttpThumbnails.java
public static Bitmap readPictureFromTheWeb(String URL) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(URL); Bitmap thePicture = null;/*from w ww .ja v a 2 s . c om*/ try { HttpResponse response = httpClient.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); thePicture = BitmapFactory.decodeStream(inputStream); inputStream.close(); } else { Log.d("JSON", "Failed to download file"); } } catch (Exception e) { Log.d("HttpThumbnails", e.getLocalizedMessage()); } return thePicture; }
From source file:org.devdom.commons.util.Utils.java
public static void close(InputStream... inputStreams) { for (InputStream input : inputStreams) { try {// ww w .j ava 2 s . c o m input.close(); } catch (Exception e) { //ignore } } }
From source file:com.gs.obevo.util.IOUtilsDA.java
public static String toString(URL url) { // This method overload was added after 2.0. We have this here for backwards-compatibility with 2.0 for some // clients/*from w ww. j a v a 2s . co m*/ // return IOUtils.toString(url); try { InputStream inputStream = url.openStream(); try { return toString(inputStream); } finally { inputStream.close(); } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String extractAssetToString(Context context, String file) { String json;//from w w w. j ava2 s .c o m try { InputStream is = context.getAssets().open(file); 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
public static Document parseFileToXML(File in) { try {//from w ww. ja v a2s . c o m InputStream defXML = new FileInputStream(in); Document defDoc = parseStreamToXML(defXML); defXML.close(); return defDoc; } catch (IOException errIO) { throw new RuntimeException("Error parsing XML files", errIO); } }