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:Utils.java
/** * Unpack an archive from a URL/*w w w.ja v a 2 s .co m*/ * * @param url * @param targetDir * @return the file to the url * @throws IOException */ public static File unpackArchive(URL url, File targetDir) throws IOException { if (!targetDir.exists()) { targetDir.mkdirs(); } InputStream in = new BufferedInputStream(url.openStream(), 1024); // make sure we get the actual file File zip = File.createTempFile("arc", ".zip", targetDir); OutputStream out = new BufferedOutputStream(new FileOutputStream(zip)); copyInputStream(in, out); out.close(); return unpackArchive(zip, targetDir); }
From source file:Main.java
/** * Load Bitmap from a url/*from ww w .java 2 s. co m*/ * @param url bitmap url to be loaded * @return Bitmap object, null if could not be loaded */ public static Bitmap loadBitmap(String url) { Bitmap bitmap = null; InputStream is = null; BufferedOutputStream bos = null; try { is = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos, IO_BUFFER_SIZE); copyStream(is, bos); bos.flush(); final byte[] data = baos.toByteArray(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } 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); closeStream(bos); } return bitmap; }
From source file:Main.java
/** * Liefert das {@link Document} aus dem Stream. * /*w w w . j a va2 s . c om*/ * @param inputStream {@link InputStream} * @return {@link Document} * @throws Exception Falls was schief geht. */ public static Document getDocument(final InputStream inputStream) throws Exception { InputStream is = inputStream; if (!(is instanceof BufferedInputStream)) { is = new BufferedInputStream(is, 8192 * 4); } return getDocument(new InputSource(is)); }
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./* ww w . j a v a 2 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(); // BitmapFactory.Options options = new BitmapFactory.Options(); // options.inSampleSize = 2; 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:com.mondospider.android.lib.LibHTTP.java
public static String get(String url) { String ReturnHTML = ""; // Log.d("LibHTTP->get->url",url); try {/*from w w w.j a va2 s.c o m*/ URLConnection urlConn = new URL(url).openConnection(); InputStream is = urlConn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is, 16000); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } ReturnHTML = new String(baf.toByteArray()); } catch (Exception e) { ReturnHTML = e.getMessage(); } // Log.d("LibHTTP->get->ReturnHTML", ReturnHTML); return ReturnHTML; }
From source file:com.textocat.textokit.morph.opencorpora.resource.DictionaryDeserializer.java
public static MorphDictionaryImpl from(InputStream in, String srcLabel) throws Exception { log.info("About to deserialize MorphDictionary from InputStream of {}...", srcLabel); long timeBefore = currentTimeMillis(); InputStream is = new BufferedInputStream(in, DICTIONARY_READING_BUFFER_SIZE); ObjectInputStream ois = new ObjectInputStream(is); MorphDictionaryImpl dict;//from ww w . java2s .c o m try { // skip gram model @SuppressWarnings("unused") GramModel gm = (GramModel) ois.readObject(); dict = (MorphDictionaryImpl) ois.readObject(); } finally { IOUtils.closeQuietly(ois); } log.info("Deserialization of MorphDictionary finished in {} ms", currentTimeMillis() - timeBefore); return dict; }
From source file:Main.java
public static void copyFile(InputStream is, OutputStream os, boolean closeInputStream) throws IOException { try {//from ww w .ja v a 2 s . co m if (!(is instanceof BufferedInputStream)) { is = new BufferedInputStream(is, 8192); } byte[] buffer = new byte[65536 * 2]; while (true) { int len = is.read(buffer); if (len == -1) { break; } os.write(buffer, 0, len); } } finally { try { if (closeInputStream) { is.close(); } } catch (IOException e) { } os.close(); } }
From source file:Main.java
/** * Extract a zip resource into real files and directories * /* w ww .j av a 2 s .co m*/ * @param in typically given as getResources().openRawResource(R.raw.something) * @param directory target directory * @param overwrite indicates whether to overwrite existing files * @return list of files that were unpacked (if overwrite is false, this list won't include files * that existed before) * @throws IOException */ public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite) throws IOException { final int BUFSIZE = 2048; byte buffer[] = new byte[BUFSIZE]; ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE)); List<File> files = new ArrayList<File>(); ZipEntry entry; directory.mkdirs(); while ((entry = zin.getNextEntry()) != null) { File file = new File(directory, entry.getName()); files.add(file); if (overwrite || !file.exists()) { if (entry.isDirectory()) { file.mkdirs(); } else { file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE); int nRead; while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) { bos.write(buffer, 0, nRead); } bos.flush(); bos.close(); } } } zin.close(); return files; }
From source file:most.voip.api.Utils.java
/** * Load UTF8withBOM or any ansi text file. * @param filename/*from w ww. j a va 2 s .com*/ * @return * @throws java.io.IOException */ public static String loadFileAsString(String filename) throws java.io.IOException { final int BUFLEN = 1024; BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename), BUFLEN); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFLEN); byte[] bytes = new byte[BUFLEN]; boolean isUTF8 = false; int read, count = 0; while ((read = is.read(bytes)) != -1) { if (count == 0 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) { isUTF8 = true; baos.write(bytes, 3, read - 3); // drop UTF8 bom marker } else { baos.write(bytes, 0, read); } count += read; } return isUTF8 ? new String(baos.toByteArray(), "UTF-8") : new String(baos.toByteArray()); } finally { try { is.close(); } catch (Exception ex) { } } }
From source file:de.nava.informa.utils.FormatDetector.java
/** * Guess the format of the specified news channel. For performance * reason it is wise to minimize the number of format guesses. * * @param url a url to the news channel. * @return The news channel synatx format, currently only RSS 0.91 * ({@link de.nava.informa.core.ChannelFormat#RSS_0_91}) * and RSS/RDF 1.0/* w w w. j a v a2s . c o m*/ * ({@link de.nava.informa.core.ChannelFormat#RSS_1_0}) * are recognized. * @throws UnsupportedFormatException in case a news channel format * could not be guessed. * @throws IOException if the given url cannot be read in. */ public static ChannelFormat getFormat(URL url) throws IOException, UnsupportedFormatException { logger.info("Trying to retrieve stream from " + url); BufferedInputStream in = new BufferedInputStream(url.openStream(), NR_FIRST_BYTES); return getFormat(in); }