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
/** * Move the file in oldLocation to newLocation. *///from w w w .ja v a2 s . c o m public static void moveFile(String tempLocation, String newLocation) throws IOException { File oldLocation = new File(tempLocation); if (oldLocation != null && oldLocation.exists()) { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); try { byte[] buff = new byte[8192]; int numChars; while ((numChars = reader.read(buff, 0, buff.length)) != -1) { writer.write(buff, 0, numChars); } } catch (IOException ex) { } finally { try { if (reader != null) { writer.close(); reader.close(); } } catch (IOException ex) { } } } else { } }
From source file:Main.java
public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException { File srcFile = new File(srcFilePath); if (!srcFile.exists() || srcFile.isDirectory()) { return;// www . j a v a 2 s. c om } BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile)); FileOutputStream fos = context.openFileOutput(dictFileName, 0); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] b = new byte[1024 * 4]; int len; while ((len = inBufferedInputStream.read(b)) != -1) { bos.write(b, 0, len); bos.flush(); } inBufferedInputStream.close(); bos.close(); }
From source file:Main.java
public static long downloadFileFromUrl(String urlPath, File file) { long size = 0; try {/*from w w w . java 2s . c o m*/ URL url = new URL(urlPath); HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection(); BufferedInputStream bufferedinputstream = new BufferedInputStream(httpurlconnection.getInputStream()); BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file)); int i; while ((i = bufferedinputstream.read()) != -1) { bufferedoutputstream.write(i); } bufferedinputstream.close(); bufferedoutputstream.close(); httpurlconnection.disconnect(); size = file.length(); } catch (Exception e) { e.printStackTrace(); } return size; }
From source file:Main.java
public static String smallFileSha1(File file) { InputStream inputStream = null; try {// www.j a v a 2 s .c o m MessageDigest md = MessageDigest.getInstance("SHA1"); inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; int nRead = 0; while ((nRead = inputStream.read(buffer)) != -1) { md.update(buffer, 0, nRead); } byte[] digest = md.digest(); byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(1).array(); byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, 4) .put(digest, 0, digest.length).array(); return Base64.encodeToString(result, Base64.URL_SAFE | Base64.NO_WRAP); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
public static byte[] readBytes(InputStream is) throws IOException { try {//from w w w. j a va2 s. c o m BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int ch; while ((ch = bis.read()) >= 0) { bos.write(ch); } bos.flush(); return bos.toByteArray(); } finally { is.close(); } }
From source file:Main.java
public static void doCompressFile(String inFileName) { try {/* w ww . ja va 2s .c om*/ File file = new File(inFileName); FileOutputStream fos = new FileOutputStream(file + ".gz"); GZIPOutputStream gzos = new GZIPOutputStream(fos); FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); byte[] buffer = new byte[1024]; int i; while ((i = in.read(buffer)) >= 0) { gzos.write(buffer, 0, i); } in.close(); gzos.close(); } catch (IOException e) { System.out.println("Exception is" + e); } }
From source file:Main.java
/** Gets Bitmap from inputStream downsampled. * * @param bis/* w w w . j a v a 2 s .c o m*/ * @param reqWidth * @param reqHeight * @return */ public static Bitmap decodeSampledBitmapFromStream(InputStream bis, int reqWidth, int reqHeight) { InputStream is = new BufferedInputStream(bis); try { is.mark(is.available()); } catch (IOException e) { e.printStackTrace(); } // First decode with inJustDecodeBounds=true to check dimensions BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bmp0 = BitmapFactory.decodeStream(is, null, options); try { is.reset(); } catch (IOException e) { e.printStackTrace(); } // Calculate inSampleSize //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bmp = BitmapFactory.decodeStream(is, null, options); return bmp; }
From source file:Main.java
public static void unzip(String zipFileName, String unzipdir) { try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)))) { ZipEntry entry = null;//from w ww .java 2s . com while ((entry = zis.getNextEntry()) != null) { // Extract teh entry's contents extractEntryContent(zis, entry, unzipdir); } } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static boolean prepareDex(Context context, File dexInternalStoragePath, String dexFile) { BufferedInputStream bis = null; OutputStream dexWriter = null; try {//from w w w . j a v a2 s. c o m bis = new BufferedInputStream(context.getAssets().open(dexFile)); dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) { dexWriter.write(buf, 0, len); } dexWriter.close(); bis.close(); return true; } catch (IOException e) { if (dexWriter != null) { try { dexWriter.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (bis != null) { try { bis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return false; } }
From source file:Main.java
/** * Compress a String to a zip file that has only one entry like zipName * The name shouldn't have .zip/* ww w.ja v a 2 s. co m*/ * * @param content * @param fName * @throws IOException */ public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset)); BufferedInputStream bis = new BufferedInputStream(bais); File f = new File(fName); File parentFile = f.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip"))); ZipEntry entry = new ZipEntry(f.getName()); zf.putNextEntry(entry); byte[] barr = new byte[8192]; int len = 0; while ((len = bis.read(barr)) != -1) { zf.write(barr, 0, len); } zf.flush(); zf.close(); bis.close(); bais.close(); }