List of usage examples for java.io BufferedInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:com.manning.androidhacks.hack037.MediaUtils.java
public static void saveRaw(Context context, int raw, String path) { File completePath = new File(Environment.getExternalStorageDirectory(), path); try {// w w w . jav a 2 s .c o m completePath.getParentFile().mkdirs(); completePath.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream((new FileOutputStream(completePath))); BufferedInputStream bis = new BufferedInputStream(context.getResources().openRawResource(raw)); byte[] buff = new byte[32 * 1024]; int len; while ((len = bis.read(buff)) > 0) { bos.write(buff, 0, len); } bos.flush(); bos.close(); } catch (IOException io) { Log.e(TAG, "Error: " + io); } }
From source file:Main.java
public static String[] getUrlInfos(String urlAsString, int timeout) { try {/*from www . j av a2s . c o m*/ URL url = new URL(urlAsString); //using proxy may increase latency HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); hConn.setRequestProperty("User-Agent", "Mozilla/5.0 Gecko/20100915 Firefox/3.6.10"); // on android we got problems because of this // so disable that for now // hConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); hConn.setConnectTimeout(timeout); hConn.setReadTimeout(timeout); // default length of bufferedinputstream is 8k byte[] arr = new byte[K4]; InputStream is = hConn.getInputStream(); if ("gzip".equals(hConn.getContentEncoding())) is = new GZIPInputStream(is); BufferedInputStream in = new BufferedInputStream(is, arr.length); in.read(arr); return getUrlInfosFromText(arr, hConn.getContentType()); } catch (Exception ex) { } return new String[] { "", "" }; }
From source file:Main.java
static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException { URL url = new URL(downloadUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true);//from w w w. j a va 2 s. c o m urlConnection.connect(); int code = urlConnection.getResponseCode(); if (code > 300 || code == -1) { throw new IOException("Cannot read url: " + downloadUrl); } String filePath = prepareFilePath(fileName, dir); String tmpFilePath = prepareTmpFilePath(fileName, dir); FileOutputStream fileOutput = new FileOutputStream(tmpFilePath); BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); byte[] buffer = new byte[1024]; int bufferLength; while ((bufferLength = inputStream.read(buffer)) > 0) { fileOutput.write(buffer, 0, bufferLength); } fileOutput.close(); // move tmp to destination copyFile(new File(tmpFilePath), new File(filePath)); return filePath; }
From source file:ch.descabato.browser.BackupBrowser.java
private static byte[] readBytes(InputStream inputStream, long max) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream((int) max); byte[] buff = new byte[1024]; BufferedInputStream bin = new BufferedInputStream(inputStream); int read = 0; while ((read = bin.read(buff)) > 0 && bout.size() < max) { bout.write(buff, 0, read);/*from www .j av a 2 s.c o m*/ } return bout.toByteArray(); //To change body of created methods use File | Settings | File Templates. }
From source file:IOTest.java
private static String readFileAsString(String filePath) throws java.io.IOException { byte[] buffer = new byte[(int) new File(filePath).length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(filePath)); f.read(buffer); return new String(buffer); }
From source file:com.dahl.brendan.wordsearch.view.IOService.java
private static void readFile(Context context, File file, boolean overwrite) { if (file.canRead()) { if (overwrite) { context.getContentResolver().delete(Word.CONTENT_URI, "1", null); }/*w ww . java 2 s . co m*/ try { byte[] buffer = new byte[(int) file.length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(file)); f.read(buffer); JSONArray array = new JSONArray(new String(buffer)); List<ContentValues> values = new LinkedList<ContentValues>(); for (int i = 0; i < array.length(); i++) { Log.v(LOGTAG, array.getString(i)); ContentValues value = new ContentValues(); value.put(Word.WORD, array.getString(i)); values.add(value); } context.getContentResolver().bulkInsert(Word.CONTENT_URI, values.toArray(new ContentValues[0])); } catch (IOException e) { Log.e(LOGTAG, "IO error", e); } catch (JSONException e) { Log.e(LOGTAG, "bad input", e); } } }
From source file:br.com.manish.ahy.kernel.util.FileUtil.java
public static String readFileAsString(String path) { String ret = null;//from w ww .ja v a2 s. c o m try { byte[] buffer = new byte[(int) new File(path).length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(path)); f.read(buffer); ret = new String(buffer); } catch (IOException e) { throw new OopsException(e, "Error reading: " + path); } return ret; }
From source file:eu.aniketos.ncvm.impl.EncodeSupport.java
/** * This methods read a file into a string. * // ww w. j a v a 2 s. c om * @param filePath * Path of the text file that should be read. * @return File content as string. */ static String readFileAsString(String filePath) throws java.io.IOException { int fileSize = (int) new File(filePath).length(); byte[] buffer = new byte[fileSize]; BufferedInputStream f = null; try { FileInputStream inputStream = new FileInputStream(filePath); f = new BufferedInputStream(inputStream); f.read(buffer); } finally { if (f != null) try { f.close(); } catch (IOException ignored) { } } return new String(buffer); }
From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java
/** * Reads a file./*from w w w. j a va 2s . c o m*/ * * @param filePath The file path. * @return a byte[] The file data. * @throws java.io.IOException if an error occurs reading the file or if the file does not exists. */ public static byte[] readFile(String filePath) throws IOException { byte[] buffer = new byte[(int) new File(filePath).length()]; BufferedInputStream f = null; try { f = new BufferedInputStream(new FileInputStream(filePath)); f.read(buffer); } finally { if (f != null) { try { f.close(); } catch (IOException ignored) { } } } return buffer; }
From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java
public static File buildArchive(File archive, File... files) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream(archive); ZipOutputStream zos = new ZipOutputStream(fos); int bytesRead; byte[] buffer = new byte[1024]; CRC32 crc = new CRC32(); for (File file : files) { if (!file.exists()) { System.err.println("Skipping: " + file); continue; }//from w w w. j a v a 2 s. co m BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); crc.reset(); while ((bytesRead = bis.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); } bis.close(); // Reset to beginning of input stream bis = new BufferedInputStream(new FileInputStream(file)); String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file); ZipEntry entry = new ZipEntry(entryPath); entry.setMethod(ZipEntry.STORED); entry.setCompressedSize(file.length()); entry.setSize(file.length()); entry.setCrc(crc.getValue()); zos.putNextEntry(entry); while ((bytesRead = bis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { IOUtils.closeQuietly(bis); } } IOUtils.closeQuietly(zos); return archive; }