List of usage examples for java.io BufferedOutputStream write
@Override public synchronized void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this buffered output stream. From source file:gov.nasa.ensemble.common.io.RemotableFile.java
private void getRemoteFileFromStream(InputStream is) throws IOException, FileNotFoundException { // create localFile that we will write to // - create directories if necessary // - overwrite if already exists String urlPath = remoteFile.getPath(); urlPath = urlPath.substring(getRemoteRootDirectory().length()); try {//from www. jav a2 s . c o m int urlPathLength = urlPath.lastIndexOf('/'); if (urlPathLength == -1) urlPathLength = 0; String dir = getLocalRootPath(); if (!dir.endsWith("/") && !dir.endsWith("\\")) dir += File.separator; dir += urlPath.substring(0, urlPathLength); File dirs = new File(dir); dirs.mkdirs(); if (!dirs.exists()) { trace.warn("Could not create directory" + dirs.getAbsolutePath()); } localFile.createNewFile(); } catch (IOException e) { trace.error("Error creating the file " + localFile + " on the local file system: " + e); return; } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFile)); try { byte[] buffer = new byte[4096]; int bytesRead = 0; while (bytesRead != -1) { bytesRead = is.read(buffer, 0, 4096); if (bytesRead == -1) continue; bos.write(buffer, 0, bytesRead); } } finally { bos.close(); closeRemoteFileStream(is); } remoteFileRetrieved(); }
From source file:com.glaf.core.util.ZipUtils.java
public static void unzip(java.io.InputStream inputStream, String path, List<String> excludes) throws Exception { File file = new File(path); FileUtils.mkdirsWithExistsCheck(file); ZipInputStream zipInputStream = null; FileOutputStream fileoutputstream = null; BufferedOutputStream bufferedoutputstream = null; try {//from w w w. j av a 2 s .co m zipInputStream = new ZipInputStream(inputStream); java.util.zip.ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { boolean isDirectory = zipEntry.isDirectory(); byte abyte0[] = new byte[BUFFER]; String s1 = zipEntry.getName(); String ext = FileUtils.getFileExt(s1); if (excludes.contains(ext) || excludes.contains(ext.toLowerCase())) { continue; } s1 = convertEncoding(s1); String s2 = path + sp + s1; s2 = FileUtils.getJavaFileSystemPath(s2); if (s2.indexOf('/') != -1 || isDirectory) { String s4 = s2.substring(0, s2.lastIndexOf('/')); File file2 = new File(s4); FileUtils.mkdirsWithExistsCheck(file2); } if (isDirectory) { continue; } fileoutputstream = new FileOutputStream(s2); bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER); int i = 0; while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) { bufferedoutputstream.write(abyte0, 0, i); } bufferedoutputstream.flush(); IOUtils.closeStream(fileoutputstream); IOUtils.closeStream(bufferedoutputstream); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeStream(zipInputStream); IOUtils.closeStream(fileoutputstream); IOUtils.closeStream(bufferedoutputstream); } }
From source file:com.glaf.core.util.ZipUtils.java
public static void unzip(File zipFile, String dir) throws Exception { File file = new File(dir); FileUtils.mkdirsWithExistsCheck(file); FileInputStream fileInputStream = null; ZipInputStream zipInputStream = null; FileOutputStream fileoutputstream = null; BufferedOutputStream bufferedoutputstream = null; try {/*from w w w. ja v a 2s . com*/ fileInputStream = new FileInputStream(zipFile); zipInputStream = new ZipInputStream(fileInputStream); fileInputStream = new FileInputStream(zipFile); zipInputStream = new ZipInputStream(fileInputStream); java.util.zip.ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { boolean isDirectory = zipEntry.isDirectory(); byte abyte0[] = new byte[BUFFER]; String s1 = zipEntry.getName(); s1 = convertEncoding(s1); String s2 = dir + "/" + s1; s2 = FileUtils.getJavaFileSystemPath(s2); if (s2.indexOf('/') != -1 || isDirectory) { String s4 = s2.substring(0, s2.lastIndexOf('/')); File file2 = new File(s4); FileUtils.mkdirsWithExistsCheck(file2); } if (isDirectory) { continue; } fileoutputstream = new FileOutputStream(s2); bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER); int i = 0; while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) { bufferedoutputstream.write(abyte0, 0, i); } bufferedoutputstream.flush(); IOUtils.closeStream(fileoutputstream); IOUtils.closeStream(bufferedoutputstream); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeStream(fileInputStream); IOUtils.closeStream(zipInputStream); IOUtils.closeStream(fileoutputstream); IOUtils.closeStream(bufferedoutputstream); } }
From source file:com.alphabetbloc.accessmrs.services.SyncAdapter.java
/** * Downloads a stream of Patient Table and Obs Table from OpenMRS, stores it * to temp file/*from ww w . j a v a 2s.c o m*/ * * @param httpclient * * @return the temporary file */ private File downloadObsStream(HttpClient client, SyncResult syncResult) { // No accurate download size on stream, so hack periodic update SyncManager.sLoopCount.set(10); SyncManager.sLoopProgress.set(0); mExecutor.schedule(new Runnable() { public void run() { // increase 1%/7s (i.e. slower than 1min timeout) SyncManager.sLoopProgress.getAndIncrement(); if (SyncManager.sLoopProgress.get() < 9) mExecutor.schedule(this, 7000, TimeUnit.MILLISECONDS); } }, 1000, TimeUnit.MILLISECONDS); // Download File File tempFile = null; try { tempFile = File.createTempFile(".omrs", "-stream", mContext.getFilesDir()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempFile)); DataInputStream dis = NetworkUtils.getOdkStream(client, NetworkUtils.getPatientDownloadUrl()); if (App.DEBUG) Log.v("SYNC BENCHMARK", "Download with buffer size=\n" + 8192); if (dis != null) { byte[] buffer = new byte[8192]; // increasing this from 4096 to // improve performance (testing) int count = 0; while ((count = dis.read(buffer)) > 0) { bos.write(buffer, 0, count); } bos.close(); dis.close(); } } catch (Exception e) { FileUtils.deleteFile(tempFile.getAbsolutePath()); e.printStackTrace(); ++syncResult.stats.numIoExceptions; return null; } return tempFile; }