List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:it.nicola_amatucci.util.JsonAndroidLocalIO.java
public static <T> T loadData(Context context, String filename, Class<T> obj) { StringBuilder strContent = new StringBuilder(""); try {/* www. java 2 s . c o m*/ BufferedInputStream in = new BufferedInputStream(context.openFileInput(filename)); int ch; while ((ch = in.read()) != -1) strContent.append((char) ch); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (strContent.length() > 0) { try { Log.i("TAG", strContent.toString()); JSONObject json = new JSONObject(strContent.toString()); return Json.object_from_json(json, obj); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:Main.java
/** * parse a certificate file into ArrayList of certificates *//* ww w . j a v a2s .c o m*/ public static ArrayList<Certificate> readCertificate(File f) throws CertificateException { ArrayList<Certificate> certs = new ArrayList<Certificate>(); CertificateFactory cf = CertificateFactory.getInstance("X.509"); BufferedInputStream in; try { in = new BufferedInputStream(new FileInputStream(f)); while (in.available() > 0) { Certificate cert = cf.generateCertificate(in); certs.add(cert); } in.close(); return certs; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Bitmap revitionImageSize(String path, int width, int height) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//from www . j a va 2s . c o m BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; Bitmap bitmap = null; while (true) { if ((options.outWidth >> i <= width) && (options.outHeight >> i <= height)) { in = new BufferedInputStream(new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } return bitmap; }
From source file:com.wishlist.Utility.java
public static Bitmap getBitmap(String url) { Bitmap bm = null;//w w w . j a va2s . co m try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(new FlushedInputStream(is)); bis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (httpclient != null) { httpclient.close(); } } return bm; }
From source file:Main.java
public static byte[] readFileToMemory(File file) throws IOException { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); byte[] data = new byte[(int) file.length()]; int start = 0; int read = 0; while ((read = bis.read(data, start, data.length - start)) > 0) { start += read;/*from w w w . j a v a2s . c o m*/ } bis.close(); fis.close(); return data; }
From source file:com.cloudhopper.commons.io.demo.FileServerMain.java
private static void saveFileFromUrl(URL url, String path) throws Exception { URLConnection urlc = url.openConnection(); BufferedInputStream bis = new BufferedInputStream(urlc.getInputStream()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path))); int i;//from ww w. j ava2 s. co m while ((i = bis.read()) != -1) { bos.write(i); } bis.close(); bos.close(); }
From source file:com.example.facebook_photo.Utility.java
public static Bitmap getBitmap(String url) { Bitmap bm = null;/* ww w.j ava 2s . c o m*/ try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(new FlushedInputStream(is)); bis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (httpclient != null) { httpclient.close(); } } return bm; }
From source file:com.salsaberries.narchiver.Writer.java
private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException { // Create an entry for the file taos.putArchiveEntry(new TarArchiveEntry(file, dir + FILE_SEPARATOR + file.getName())); if (file.isFile()) { // Add the file to the archive BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, taos);/*ww w . ja va 2s . c o m*/ taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { // Close the archive entry taos.closeArchiveEntry(); // Go through all the files in the directory and using recursion, add them to the archive for (File childFile : file.listFiles()) { addFilesToCompression(taos, childFile, file.getName()); } } }
From source file:Main.java
public static Bitmap revitionImageSize(String path) { Bitmap bitmap = null;/* ww w . j a v a2s. c om*/ try { BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; while (true) { if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) { in = new BufferedInputStream(new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } } catch (Exception e) { e.printStackTrace(); } return bitmap; }
From source file:brut.directory.ZipUtils.java
private static void processFolder(final File folder, final ZipOutputStream zipOutputStream, final int prefixLength) throws BrutException, IOException { for (final File file : folder.listFiles()) { if (file.isFile()) { final String cleanedPath = BrutIO.sanitizeUnknownFile(folder, file.getPath().substring(prefixLength)); final ZipEntry zipEntry = new ZipEntry(BrutIO.normalizePath(cleanedPath)); // aapt binary by default takes in parameters via -0 arsc to list extensions that shouldn't be // compressed. We will replicate that behavior final String extension = FilenameUtils.getExtension(file.getAbsolutePath()); if (mDoNotCompress != null && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) { zipEntry.setMethod(ZipEntry.STORED); zipEntry.setSize(file.length()); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file)); CRC32 crc = BrutIO.calculateCrc(unknownFile); zipEntry.setCrc(crc.getValue()); unknownFile.close(); } else { zipEntry.setMethod(ZipEntry.DEFLATED); }//from w w w . j av a 2 s . c om zipOutputStream.putNextEntry(zipEntry); try (FileInputStream inputStream = new FileInputStream(file)) { IOUtils.copy(inputStream, zipOutputStream); } zipOutputStream.closeEntry(); } else if (file.isDirectory()) { processFolder(file, zipOutputStream, prefixLength); } } }