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 Bitmap getBitmapFromURL(String urlString) { Bitmap bitmap = null;/*from w ww. j a v a2s .c om*/ InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(urlString).openStream(), 1024 * 4); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, 1024 * 4); byte[] buffer = new byte[1024]; int len = -1; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.flush(); byte[] data = dataStream.toByteArray(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); data = null; return bitmap; } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return bitmap; }
From source file:Main.java
public static boolean downloadUrlToStream(String urlString, OutputStream outputStream) { HttpURLConnection urlConnection = null; BufferedOutputStream out = null; BufferedInputStream in = null; try {/*from ww w . ja va2 s .c om*/ final URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024); out = new BufferedOutputStream(outputStream, 8 * 1024); int b; while ((b = in.read()) != -1) { out.write(b); } return true; } catch (final IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (final IOException e) { e.printStackTrace(); } } return false; }
From source file:Main.java
public static byte[] loadImageDataFromWeb(String url) { byte[] imageData = null; try {/*from www . j a va 2 s. c om*/ URLConnection connection = new URL(url).openConnection(); InputStream stream = connection.getInputStream(); //BufferedInputStream in=new BufferedInputStream(stream);//default 8k buffer BufferedInputStream in = new BufferedInputStream(stream, 10240);//YG: 10k=10240, 2x8k=16384 ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int read; byte[] b = new byte[4096]; while ((read = in.read(b)) != -1) { out.write(b, 0, read); } out.flush(); out.close(); imageData = out.toByteArray(); } catch (Exception e) { Log.w(TAG, "Exc=" + e); return null; } return imageData; }
From source file:Main.java
public static Bitmap getAndSetBitmapFromNet(String urlPath) { Bitmap bm = null;//from w w w. j a va 2 s . co m if (urlPath != null) { try { BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024); copy(bis, out); out.flush(); final byte[] data = dataStream.toByteArray(); bm = BitmapFactory.decodeByteArray(data, 0, data.length); Log.i(I, "data.length: " + data.length); out.close(); dataStream.close(); bis.close(); bm = processBitmap(bm); } catch (IOException e) { Log.i(I, "URL Connection or Bitmap processing Exception"); e.printStackTrace(); } } return bm; }
From source file:Main.java
/** * Determines if the given stream contains XML content. The stream will be * buffered and reset if necessary./* ww w . ja va2s .com*/ * * @param stream * The InputStream to read. * @return true if the stream contains XML content; false otherwise. */ public static boolean isXML(InputStream stream) { if (!stream.markSupported()) { stream = new BufferedInputStream(stream, 1024); } stream.mark(1024); byte[] bytes = new byte[1024]; try { try { stream.read(bytes); } finally { stream.reset(); } } catch (IOException iox) { throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage()); } try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes)); // If XML, now in START_DOCUMENT state; seek document element. reader.nextTag(); } catch (XMLStreamException xse) { return false; } return true; }
From source file:com.digitallizard.bbcnewsreader.resource.web.ImageDownloader.java
public static byte[] getImage(URL url) throws Exception { URLConnection connection = url.openConnection(); InputStream stream = connection.getInputStream(); BufferedInputStream inputbuffer = new BufferedInputStream(stream, 8000); ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50); int current = 0; while ((current = inputbuffer.read()) != -1) { arraybuffer.append((byte) current); }//w ww .j a v a2 s. co m byte[] image = arraybuffer.toByteArray(); return image; }
From source file:Main.java
/** * Extracts the whole contents of in_file to out_dir. * @param in_file A file object pointing to a zip file. * @param out_dir A directory where the contents of the file should be placed. * @throws IOException if there's a problem with one of the files *///from ww w. j av a 2 s.co m public static void zipExtract(File in_file, File out_dir) throws IOException { InputStream is = new FileInputStream(in_file); BufferedInputStream bis = new BufferedInputStream(is, BUFFER_SIZE); zipExtract(bis, out_dir); }
From source file:Main.java
public static boolean bitmapToOutPutStream(final Bitmap bitmap, OutputStream outputStream) { BufferedOutputStream out = null; BufferedInputStream in = null; try {/*w w w .j av a2 s . co m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); InputStream inputStream = new ByteArrayInputStream(baos.toByteArray()); in = new BufferedInputStream(inputStream, 8 * 1024); out = new BufferedOutputStream(outputStream, 8 * 1024); int b; while ((b = in.read()) != -1) { out.write(b); } return true; } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (final IOException e) { e.printStackTrace(); } } return false; }
From source file:Main.java
private static void zipFile(ZipOutputStream zipOutputStream, String sourcePath) throws IOException { java.io.File files = new java.io.File(sourcePath); java.io.File[] fileList = files.listFiles(); String entryPath = ""; BufferedInputStream input = null; for (java.io.File file : fileList) { if (file.isDirectory()) { zipFile(zipOutputStream, file.getPath()); } else {/*from w w w. ja v a2s .c o m*/ byte data[] = new byte[BUFFER_SIZE]; FileInputStream fileInputStream = new FileInputStream(file.getPath()); input = new BufferedInputStream(fileInputStream, BUFFER_SIZE); entryPath = file.getAbsolutePath().replace(parentPath, ""); ZipEntry entry = new ZipEntry(entryPath); zipOutputStream.putNextEntry(entry); int count; while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) { zipOutputStream.write(data, 0, count); } input.close(); } } }
From source file:Main.java
private static void copy(InputStream in, OutputStream out) throws IOException { out = new BufferedOutputStream(out, 0x1000); in = new BufferedInputStream(in, 0x1000); // Copy the contents from the input stream to the output stream. while (true) { int b = in.read(); if (b == -1) { break; }//from ww w .ja v a 2 s . c o m out.write(b); } out.flush(); }