List of usage examples for java.io BufferedInputStream BufferedInputStream
public BufferedInputStream(InputStream in)
BufferedInputStream
and saves its argument, the input stream in
, for later use. 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 a 2 s . co m*/ } bis.close(); fis.close(); return data; }
From source file:Main.java
public static BufferedInputStream toBufferedInputStream(InputStream inputStream) { return inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); }
From source file:Main.java
public static String readStreamToString(InputStream inputStream) throws IOException { BufferedInputStream bufferedInputStream = null; InputStreamReader reader = null; try {/*from ww w. ja v a2 s. c o m*/ bufferedInputStream = new BufferedInputStream(inputStream); reader = new InputStreamReader(bufferedInputStream); StringBuilder stringBuilder = new StringBuilder(); final int bufferSize = 1024 * 2; char[] buffer = new char[bufferSize]; int n = 0; while ((n = reader.read(buffer)) != -1) { stringBuilder.append(buffer, 0, n); } return stringBuilder.toString(); } finally { closeQuietly(bufferedInputStream); closeQuietly(reader); } }
From source file:Main.java
static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w, int h) {/*from ww w . j a v a 2 s. co m*/ if (str_url == null || str_url.length() <= 0) return null; BitmapDrawable thumb = null; try { URL url = new URL(str_url); URLConnection connection = url.openConnection(); connection.connect(); InputStream stream = connection.getInputStream(); BufferedInputStream data = new BufferedInputStream(stream); Bitmap bitmap = BitmapFactory.decodeStream(data); if (bitmap != null) { if (scale) thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true)); else thumb = new BitmapDrawable(resources, bitmap); } } catch (MalformedURLException e) { //e.printStackTrace(); } catch (IOException e) { //e.printStackTrace(); } return thumb; }
From source file:jenkins.uvision.CoverageParser.java
public static TestRun parse(File inFile) throws IOException { FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; try {/*from www. j av a2 s . c om*/ fileInputStream = new FileInputStream(inFile); bufferedInputStream = new BufferedInputStream(fileInputStream); CoverageParser parser = new CoverageParser(); TestRun result = parse(bufferedInputStream); if (result == null) throw new NullPointerException(); return result; } finally { try { if (bufferedInputStream != null) bufferedInputStream.close(); if (fileInputStream != null) fileInputStream.close(); } catch (IOException e) { } } }
From source file:Main.java
public static void compressAFileToZip(File zip, File source) throws IOException { Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length()); Log.d("zip:", zip.getAbsolutePath()); zip.getParentFile().mkdirs();//ww w .ja va 2 s. c o m File tempFile = new File(zip.getAbsolutePath() + ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); ZipEntry zipEntry = new ZipEntry(source.getName()); zipEntry.setTime(source.lastModified()); zos.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(source); BufferedInputStream bis = new BufferedInputStream(fis); byte[] bArr = new byte[4096]; int byteRead = 0; while ((byteRead = bis.read(bArr)) != -1) { zos.write(bArr, 0, byteRead); } zos.flush(); zos.closeEntry(); zos.close(); Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize()); bos.flush(); fos.flush(); bos.close(); fos.close(); bis.close(); fis.close(); zip.delete(); tempFile.renameTo(zip); }
From source file:Main.java
public static void writeUrlToFile(String urlToRead, String folderToWrite, String fileName) throws MalformedURLException, IOException { URL urlIn = new URL(urlToRead); File folderOut = new File(folderToWrite); if (!(folderOut.exists() || folderOut.mkdirs())) { throw new RuntimeException("could not create folder " + folderToWrite); }//from ww w.j av a 2 s .c om File fileOut = new File(folderOut, fileName); try (InputStream in = new BufferedInputStream(urlIn.openStream()); OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));) { transfer(in, out); } }
From source file:Main.java
public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK"); // determine the image size and allocate a buffer int fileSize = connection.getContentLength(); byte[] imageData = new byte[fileSize]; // download the file Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")"); if (fileSize > 0) { BufferedInputStream istream = new BufferedInputStream(connection.getInputStream()); int bytesRead = 0; int offset = 0; while (bytesRead != -1 && offset < fileSize) { bytesRead = istream.read(imageData, offset, fileSize - offset); offset += bytesRead;/* www . ja va 2s.c o m*/ } istream.close(); } else Log.d(TAG, "fileSize is 0! skipping"); // clean up connection.disconnect(); return imageData; }
From source file:PipedBytes.java
public static void readStuff(InputStream rawIn) { try {/* w w w. ja v a 2 s.c o m*/ DataInputStream in = new DataInputStream(new BufferedInputStream(rawIn)); boolean eof = false; while (!eof) { try { int i = in.readInt(); System.out.println("just read: " + i); } catch (EOFException eofx) { eof = true; } } System.out.println("Read all data from the pipe"); } catch (IOException x) { x.printStackTrace(); } }
From source file:Main.java
public static void downloadImage(String imageUrl) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Log.d("TAG", "monted sdcard"); } else {//w w w .j a va 2 s . c om Log.d("TAG", "has no sdcard"); } HttpURLConnection con = null; FileOutputStream fos = null; BufferedOutputStream bos = null; BufferedInputStream bis = null; File imageFile = null; try { URL url = new URL(imageUrl); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setReadTimeout(15 * 1000); con.setDoInput(true); con.setDoOutput(true); bis = new BufferedInputStream(con.getInputStream()); imageFile = new File(getImagePath(imageUrl)); fos = new FileOutputStream(imageFile); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int length; while ((length = bis.read(b)) != -1) { bos.write(b, 0, length); bos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } if (con != null) { con.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } if (imageFile != null) { } }