List of usage examples for java.io BufferedInputStream read
public synchronized int read(byte b[], int off, int len) throws IOException
From source file:Main.java
/** * Zips a file at a location and places the resulting zip file at the toLocation. * <p/>// w w w . ja v a 2s. c o m * Example: * zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip"); * <p/> * http://stackoverflow.com/a/14868161 */ public static void zipFileAtPath(String sourcePath, String toLocation) throws IOException { final int BUFFER = 2048; File sourceFile = new File(sourcePath); FileOutputStream fos = new FileOutputStream(toLocation); ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); if (sourceFile.isDirectory()) { zipSubFolder(zipOut, sourceFile, sourceFile.getParent().length() + 1); // ?? } else { byte data[] = new byte[BUFFER]; FileInputStream fis = new FileInputStream(sourcePath); BufferedInputStream bis = new BufferedInputStream(fis, BUFFER); String lastPathComponent = sourcePath.substring(sourcePath.lastIndexOf("/")); ZipEntry zipEntry = new ZipEntry(lastPathComponent); zipOut.putNextEntry(zipEntry); int count; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipOut.write(data, 0, count); } } zipOut.close(); }
From source file:ImageUtils.java
final public static ImageProperties getJpegProperties(File file) throws FileNotFoundException, IOException { BufferedInputStream in = null; try {/*from w w w . j av a2 s. co m*/ in = new BufferedInputStream(new FileInputStream(file)); // check for "magic" header byte[] buf = new byte[2]; int count = in.read(buf, 0, 2); if (count < 2) { throw new RuntimeException("Not a valid Jpeg file!"); } if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) { throw new RuntimeException("Not a valid Jpeg file!"); } int width = 0; int height = 0; char[] comment = null; boolean hasDims = false; boolean hasComment = false; int ch = 0; while (ch != 0xDA && !(hasDims && hasComment)) { /* Find next marker (JPEG markers begin with 0xFF) */ while (ch != 0xFF) { ch = in.read(); } /* JPEG markers can be padded with unlimited 0xFF's */ while (ch == 0xFF) { ch = in.read(); } /* Now, ch contains the value of the marker. */ int length = 256 * in.read(); length += in.read(); if (length < 2) { throw new RuntimeException("Not a valid Jpeg file!"); } /* Now, length contains the length of the marker. */ if (ch >= 0xC0 && ch <= 0xC3) { in.read(); height = 256 * in.read(); height += in.read(); width = 256 * in.read(); width += in.read(); for (int foo = 0; foo < length - 2 - 5; foo++) { in.read(); } hasDims = true; } else if (ch == 0xFE) { // that's the comment marker comment = new char[length - 2]; for (int foo = 0; foo < length - 2; foo++) comment[foo] = (char) in.read(); hasComment = true; } else { // just skip marker for (int foo = 0; foo < length - 2; foo++) { in.read(); } } } return (new ImageProperties(width, height, comment, "jpeg")); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } }
From source file:Main.java
public static byte[] readContentBytesFromFile(File fileForRead) { if (fileForRead == null) { return null; } else if (fileForRead.exists() && fileForRead.isFile()) { ReentrantReadWriteLock.ReadLock readLock = getLock(fileForRead.getAbsolutePath()).readLock(); readLock.lock();/*from w ww. j a va 2 s .c o m*/ Object data = null; BufferedInputStream input = null; try { byte[] data1 = new byte[(int) fileForRead.length()]; int e = 0; input = new BufferedInputStream(new FileInputStream(fileForRead), 8192); while (e < data1.length) { int bytesRemaining = data1.length - e; int bytesRead = input.read(data1, e, bytesRemaining); if (bytesRead > 0) { e += bytesRead; } } byte[] bytesRemaining1 = data1; return bytesRemaining1; } catch (IOException var10) { } finally { closeQuietly(input); readLock.unlock(); } return null; } else { return null; } }
From source file:Main.java
public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException { int BUFFER = 2048; List zipFiles = new ArrayList(); File sourceZipFile = new File(inputZip); File unzipDestinationDirectory = new File(destinationDirectory); unzipDestinationDirectory.mkdir();//from w w w . j ava 2 s .co m ZipFile zipFile; zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); Enumeration zipFileEntries = zipFile.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(unzipDestinationDirectory, currentEntry); if (currentEntry.endsWith(".zip")) { zipFiles.add(destFile.getAbsolutePath()); } File destinationParent = destFile.getParentFile(); destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry)); int currentByte; // buffer for writing file byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } zipFile.close(); for (Iterator iter = zipFiles.iterator(); iter.hasNext();) { String zipName = (String) iter.next(); unzipEPub(zipName, destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip"))); } }
From source file:com.clov4r.moboplayer.android.nil.codec.SubtitleJni.java
private static String getFilecharset(File sourceFile) { if (!sourceFile.getAbsolutePath().endsWith("srt")) { return "UTF-8"; }//from ww w.j a v a2 s .c o m String charset = "UTF-8"; byte[] first3Bytes = new byte[3]; try { boolean checked = false; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile)); bis.mark(0); int read = bis.read(first3Bytes, 0, 3); if (read == -1) { return charset; } else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) { charset = "UTF-16LE"; checked = true; } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) { charset = "UTF-16BE"; checked = true; } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) { charset = "UTF-8"; checked = true; } // bis.reset(); if (!checked) { int loc = 0; while ((read = bis.read()) != -1) { loc++; if (read >= 0xF0) { charset = "GBK"; break; } if (0x80 <= read && read <= 0xBF) { charset = "GBK"; break; } if (0xC0 <= read && read <= 0xDF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) continue; else { charset = "GBK"; break; } } else if (0xE0 <= read && read <= 0xEF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) { charset = "UTF-8"; break; } else { charset = "GBK"; break; } } else { charset = "GBK"; break; } } } } bis.close(); } catch (Exception e) { e.printStackTrace(); } return charset; }
From source file:com.dnielfe.manager.utils.SimpleUtils.java
/** * /* ww w .j a va 2s. com*/ * @param old * the file to be copied/ moved * @param newDir * the directory to copy/move the file to */ public static void copyToDirectory(String old, String newDir) { File old_file = new File(old); File temp_dir = new File(newDir); byte[] data = new byte[BUFFER]; int read = 0; if (old_file.isFile() && temp_dir.isDirectory() && temp_dir.canWrite()) { String file_name = old.substring(old.lastIndexOf("/"), old.length()); File cp_file = new File(newDir + file_name); try { BufferedOutputStream o_stream = new BufferedOutputStream(new FileOutputStream(cp_file)); BufferedInputStream i_stream = new BufferedInputStream(new FileInputStream(old_file)); while ((read = i_stream.read(data, 0, BUFFER)) != -1) o_stream.write(data, 0, read); o_stream.flush(); i_stream.close(); o_stream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return; } catch (IOException e) { e.printStackTrace(); return; } } else if (old_file.isDirectory() && temp_dir.isDirectory() && temp_dir.canWrite()) { String files[] = old_file.list(); String dir = newDir + old.substring(old.lastIndexOf("/"), old.length()); int len = files.length; if (!new File(dir).mkdir()) return; for (int i = 0; i < len; i++) copyToDirectory(old + "/" + files[i], dir); } else if (old_file.isFile() && !temp_dir.canWrite() && SimpleExplorer.rootAccess) { RootCommands.moveCopyRoot(old, newDir); } else if (!temp_dir.canWrite()) return; return; }
From source file:riddimon.android.asianetautologin.HttpUtils.java
public static String readStreamIntoString(InputStream in) { StringBuilder builder = new StringBuilder(); int buf_size = 50 * 1024; // read in chunks of 50 KB ByteArrayBuffer bab = new ByteArrayBuffer(buf_size); int read = 0; BufferedInputStream bis = new BufferedInputStream(in); if (bis != null) { byte buffer[] = new byte[buf_size]; try {/* w w w . j a v a 2 s .c om*/ while ((read = bis.read(buffer, 0, buf_size)) != -1) { //builder.append(new String(buffer, "utf-8")); bab.append(buffer, 0, read); } builder.append(new String(bab.toByteArray(), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return builder.toString(); }
From source file:com.concursive.connect.web.modules.documents.beans.FileDownload.java
public static void send(OutputStream outputStream, InputStream is) throws Exception { BufferedInputStream inputStream = new BufferedInputStream(is); byte[] buf = new byte[4 * 1024]; // 4K buffer//from www . jav a 2 s . c o m int len; while ((len = inputStream.read(buf, 0, buf.length)) != -1) { outputStream.write(buf, 0, len); } outputStream.flush(); outputStream.close(); inputStream.close(); }
From source file:com.bukanir.android.utils.Utils.java
public static void saveURL(String url, String filename) throws IOException { BufferedInputStream in = null; FileOutputStream fout = null; try {//from w ww .j a va 2s. co m in = new BufferedInputStream(new URL(url).openStream()); fout = new FileOutputStream(filename); int count; final byte data[] = new byte[1024]; while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } }
From source file:org.openmrs.module.report.util.FileUtils.java
public static boolean copyFile(File source, File dest) { try {// www . j ava 2 s . com BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); byte[] data = new byte[2 * 1024]; int count; while ((count = in.read(data, 0, data.length)) != -1) { out.write(data, 0, count); } out.flush(); out.close(); in.close(); return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } }