List of usage examples for java.io BufferedInputStream BufferedInputStream
public BufferedInputStream(InputStream in, int size)
BufferedInputStream
with the specified buffer size, and saves its argument, the input stream in
, for later use. From source file:Main.java
public static int[] readInts(String file) throws IOException { DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024)); try {//from w w w . j a va2s . c o m int len = in.readInt(); int[] ints = new int[len]; for (int i = 0; i < len; i++) { ints[i] = in.readInt(); } return ints; } finally { in.close(); } }
From source file:Main.java
public static float[] readFloats(String file) throws IOException { DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024)); try {/*from w ww.j a v a2 s . co m*/ int len = in.readInt(); float[] floats = new float[len]; for (int i = 0; i < len; i++) { floats[i] = in.readFloat(); } return floats; } finally { in.close(); } }
From source file:Main.java
/** * Loads a bitmap from the specified url. This can take a while, so it should not * be called from the UI thread.//from w w w . j a v a2 s . com * * @param url The location of the bitmap asset * * @return The bitmap, or null if it could not be loaded */ public static Bitmap loadBitmap(String url) { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); copy(in, out); out.flush(); final byte[] data = dataStream.toByteArray(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } catch (IOException e) { Log.e(TAG, "Could not load Bitmap from: " + url); } finally { closeStream(in); closeStream(out); } return bitmap; }
From source file:Main.java
public static byte[] readContentBytesFromFile(File fileForRead) { if (fileForRead == null) { return null; } else if (fileForRead.exists() && fileForRead.isFile()) { ReentrantReadWriteLock.ReadLock readLock = getLock(fileForRead.getAbsolutePath()).readLock(); readLock.lock();/*w ww .java2 s . c o m*/ Object data = null; BufferedInputStream input = null; try { byte[] data1 = new byte[(int) fileForRead.length()]; int e = 0; input = new BufferedInputStream(new FileInputStream(fileForRead), 8192); while (e < data1.length) { int bytesRemaining = data1.length - e; int bytesRead = input.read(data1, e, bytesRemaining); if (bytesRead > 0) { e += bytesRead; } } byte[] bytesRemaining1 = data1; return bytesRemaining1; } catch (IOException var10) { } finally { closeQuietly(input); readLock.unlock(); } return null; } else { return null; } }
From source file:Main.java
static public InputStream createInputStream(File file) throws IOException { String filename = file.getName().toLowerCase(); if (filename.endsWith(".gz")) { // a buffered output stream sped processing up by 4X! in prod GZIPInputStream gis = new GZIPInputStream(new FileInputStream(file), 1024 * 1024); // 1 MB buffer return new BufferedInputStream(gis, 1024 * 1024); // 1 MB buffer /** } else if (filename.endsWith(".lzf")) { // a buffered output stream is not necessary return new LZFInputStream(new FileInputStream(file)); *///from www. ja v a 2 s . c o m } else { return null; } }
From source file:Main.java
public static Bitmap loadBitmapDirectStream(String url, BitmapFactory.Options opt) { Bitmap bitmap = null;//from w ww . j a va 2 s. c om InputStream is = null; try { is = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); bitmap = BitmapFactory.decodeStream(is, null, opt); } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAG, "IOException in loadBitmap"); e.printStackTrace(); } catch (OutOfMemoryError e) { // TODO Auto-generated catch block Log.e(TAG, "OutOfMemoryError in loadBitmap"); e.printStackTrace(); } finally { closeStream(is); } return bitmap; }
From source file:Main.java
public static String getSha1(FileDescriptor fd) { MessageDigest md;// www .j a va 2 s .c o m try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } byte[] b = new byte[4096]; FileInputStream fis = new FileInputStream(fd); InputStream is = new BufferedInputStream(fis, 4096); try { for (int n; (n = is.read(b)) != -1;) { md.update(b, 0, n); } } catch (IOException e) { Log.w(TAG, "IOException while computing SHA-1"); return null; } byte[] sha1hash = new byte[40]; sha1hash = md.digest(); return getHex(sha1hash); }
From source file:Main.java
public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException { Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm()); String filename = url.getFile(); int lastSlashPos = filename.lastIndexOf('/'); String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1)); File file = new File(cacheDir, fileNameNoPath); if (file.exists()) { if (file.length() > 0) { Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath()); return file; } else {// www. j av a 2s .c o m Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath()); file.delete(); } } Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists."); URLConnection ucon = url.openConnection(); ucon.setReadTimeout(5000); ucon.setConnectTimeout(30000); InputStream is = ucon.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); FileOutputStream outStream = new FileOutputStream(file); byte[] buff = new byte[5 * 1024]; // Read bytes (and store them) until there is nothing more to read(-1) int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff, 0, len); } // Clean up outStream.flush(); outStream.close(); inStream.close(); return file; }
From source file:Main.java
public static String zip(String filename) throws IOException { BufferedInputStream origin = null; Integer BUFFER_SIZE = 20480;//from ww w . java 2s .c om if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { File flockedFilesFolder = new File( Environment.getExternalStorageDirectory() + File.separator + "FlockLoad"); System.out.println("FlockedFileDir: " + flockedFilesFolder); String uncompressedFile = flockedFilesFolder.toString() + "/" + filename; String compressedFile = flockedFilesFolder.toString() + "/" + "flockZip.zip"; ZipOutputStream out = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(compressedFile))); out.setLevel(9); try { byte data[] = new byte[BUFFER_SIZE]; FileInputStream fi = new FileInputStream(uncompressedFile); System.out.println("Filename: " + uncompressedFile); System.out.println("Zipfile: " + compressedFile); origin = new BufferedInputStream(fi, BUFFER_SIZE); try { ZipEntry entry = new ZipEntry( uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { out.write(data, 0, count); } } finally { origin.close(); } } finally { out.close(); } return "flockZip.zip"; } return null; }
From source file:Main.java
/** * Decodes image from inputstream into a new Bitmap of specified dimensions. * * This is a long-running operation that must run in a background thread. * * @param is InputStream containing the image. * @param maxWidth target width of the output Bitmap. * @param maxHeight target height of the output Bitmap. * @return new Bitmap containing the image. * @throws IOException//from w w w .ja v a2 s . c om */ public static Bitmap decodeBitmapBounded(InputStream is, int maxWidth, int maxHeight) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(is, STREAM_BUFFER_SIZE); try { bufferedInputStream.mark(STREAM_BUFFER_SIZE); // should be enough to read image dimensions. // TODO(mattfrazier): fail more gracefully if mark isn't supported, but it should always be // by bufferedinputstream. BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(bufferedInputStream, null, bmOptions); bufferedInputStream.reset(); bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = calculateInSampleSize(bmOptions.outWidth, bmOptions.outHeight, maxWidth, maxHeight); // TODO(mattfrazier): Samsung devices yield a rotated bitmap no matter what orientation is // captured. Read Exif data and rotate in place or communicate Exif data and rotate display // with matrix. return BitmapFactory.decodeStream(bufferedInputStream, null, bmOptions); } finally { bufferedInputStream.close(); } }