List of usage examples for java.io BufferedOutputStream flush
@Override public synchronized void flush() throws IOException
From source file:com.glaf.core.util.FileUtils.java
public static void save(String filename, InputStream inputStream) { if (filename == null || inputStream == null) { return;/* w w w.j av a2s . c om*/ } String path = ""; String sp = System.getProperty("file.separator"); if (filename.indexOf(sp) != -1) { path = filename.substring(0, filename.lastIndexOf(sp)); } if (filename.indexOf("/") != -1) { path = filename.substring(0, filename.lastIndexOf("/")); } path = getJavaFileSystemPath(path); java.io.File dir = new java.io.File(path + sp); mkdirsWithExistsCheck(dir); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(inputStream); bos = new BufferedOutputStream(new FileOutputStream(filename)); int bytesRead = 0; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = bis.read(buffer, 0, BUFFER_SIZE)) != -1) { bos.write(buffer, 0, bytesRead); } bos.flush(); bis.close(); bos.close(); bis = null; bos = null; } catch (IOException ex) { bis = null; bos = null; throw new RuntimeException(ex); } finally { try { if (bis != null) { bis.close(); bis = null; } if (bos != null) { bos.close(); bos = null; } } catch (IOException ioe) { } } }
From source file:org.apache.storm.utils.ServerUtils.java
private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { File subDir = new File(outputDir, entry.getName()); if (!subDir.mkdirs() && !subDir.isDirectory()) { throw new IOException("Mkdirs failed to create tar internal dir " + outputDir); }/*from w w w. ja va 2s . c o m*/ for (TarArchiveEntry e : entry.getDirectoryEntries()) { unpackEntries(tis, e, subDir); } return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { if (!outputFile.getParentFile().mkdirs()) { throw new IOException("Mkdirs failed to create tar internal dir " + outputDir); } } int count; byte data[] = new byte[2048]; BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); while ((count = tis.read(data)) != -1) { outputStream.write(data, 0, count); } outputStream.flush(); outputStream.close(); }
From source file:net.ytbolg.mcxa.Updater.java
public static String downloadFile(String remoteFilePath) throws IOException { URL urlfile = null;//from www .j a va 2 s . c o m HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(GameInfo.Rundir + GameInfo.tpf + "temp.tmp"); File xxxx = new File(f.getParent()); xxxx.mkdirs(); // f.mkdirs(); try { urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection) urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len = 2048; byte[] b = new byte[len]; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } bos.flush(); bis.close(); httpUrl.disconnect(); } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } String x = ReadFile(GameInfo.Rundir + GameInfo.tpf + "temp.tmp"); f.delete(); return x; }
From source file:forge.quest.io.QuestDataIO.java
@SuppressWarnings("unused") // used only for debug purposes private static void saveUnpacked(final String f, final XStream xStream, final QuestData qd) throws IOException { final BufferedOutputStream boutUnp = new BufferedOutputStream(new FileOutputStream(f)); xStream.toXML(qd, boutUnp);/*from ww w. j a v a 2 s .c o m*/ boutUnp.flush(); boutUnp.close(); }
From source file:com.bristle.javalib.io.FileUtil.java
/************************************************************************** * Copy the binary contents of the specified InputStream to the specified * OutputStream, up to the specified number of bytes, returning the count * of bytes written./*from w ww. j a v a 2 s . c om*/ *@param streamIn InputStream to read from *@param streamOut OutputStream to write to *@param lngMaxBytes Max number of bytes to copy, or lngNO_MAX_BYTES * for unlimited *@return Count of bytes written *@throws TooManyBytesException * When streamIn contains more than intMaxBytes, * resulting in a partially copied binary stream. *@throws IOException When an I/O error occurs reading or writing a * stream. **************************************************************************/ public static long copyBinaryStreamToStream(InputStream streamIn, OutputStream streamOut, long lngMaxBytes) throws TooManyBytesException, IOException { // Buffer the input and output for efficiency, to avoid lots of // small I/O operations. // Note: Don't call the read() and write() methods that could do it // all in a single byte-array because we don't want to consume // that caller-specified amount of memory. Better to do it // one byte at a time, but with buffering for efficiency. BufferedInputStream buffIn = new BufferedInputStream(streamIn); BufferedOutputStream buffOut = new BufferedOutputStream(streamOut); long lngBytesWritten = 0; for (int intByte = buffIn.read(); intByte > -1; intByte = buffIn.read()) { if (lngMaxBytes != lngNO_MAX_BYTES && lngBytesWritten >= lngMaxBytes) { buffOut.flush(); throw new TooManyBytesException("The input stream contains more than " + lngMaxBytes + " bytes. Only " + lngBytesWritten + " bytes were written to the " + "output stream", lngBytesWritten); } buffOut.write(intByte); lngBytesWritten++; } buffOut.flush(); return lngBytesWritten; }
From source file:org.apache.axis2.context.externalize.MessageExternalizeUtils.java
/** * Write out the Message//from ww w.ja va2 s. c o m * @param out * @param mc * @param correlationIDString * @param outputFormat * @throws IOException */ public static void writeExternal(ObjectOutput out, MessageContext mc, String correlationIDString, OMOutputFormat outputFormat) throws IOException { if (log.isDebugEnabled()) { log.debug(correlationIDString + ":writeExternal(): start"); } SOAPEnvelope envelope = mc.getEnvelope(); if (envelope == null) { // Case: No envelope out.writeUTF("NULL_ENVELOPE"); out.writeInt(revisionID); out.writeBoolean(EMPTY_OBJECT); // Not Active out.writeInt(0); // EndBlocks if (log.isDebugEnabled()) { log.debug(correlationIDString + ":writeExternal(): end: msg is Empty"); } return; } // Write Prolog String msgClass = envelope.getClass().getName(); out.writeUTF(msgClass); out.writeInt(revisionID); out.writeBoolean(ACTIVE_OBJECT); if (outputFormat.isOptimized()) { out.writeBoolean(true); // Write out the contentType. out.writeUTF(outputFormat.getContentType()); } else { out.writeBoolean(false); } out.writeUTF(outputFormat.getCharSetEncoding()); out.writeUTF(envelope.getNamespace().getNamespaceURI()); if (log.isDebugEnabled()) { log.debug(correlationIDString + ":writeExternal(): " + "optimized=[" + outputFormat.isOptimized() + "] " + "optimizedContentType " + outputFormat.getContentType() + "] " + "charSetEnc=[" + outputFormat.getCharSetEncoding() + "] " + "namespaceURI=[" + envelope.getNamespace().getNamespaceURI() + "]"); } // Write DataBlocks // MessageOutputStream writes out the DataBlocks in chunks // BufferedOutputStream buffers the data to prevent numerous, small blocks MessageOutputStream mos = new MessageOutputStream(out); BufferedOutputStream bos = new BufferedOutputStream(mos); boolean errorOccurred = false; try { // Write out the message using the same logic as the // transport layer. MessageFormatter msgFormatter = MessageProcessorSelector.getMessageFormatter(mc); msgFormatter.writeTo(mc, outputFormat, bos, true); // Preserve the original message } catch (IOException e) { throw e; } catch (Throwable t) { throw AxisFault.makeFault(t); } finally { bos.flush(); bos.close(); } // Write End of Data Blocks if (errorOccurred) { out.writeInt(-1); } else { out.writeInt(0); } if (log.isDebugEnabled()) { log.debug(correlationIDString + ":writeExternal(): end"); } }
From source file:com.aurel.track.util.emailHandling.MailReader.java
/** * Saves attachment//from w w w .jav a 2s . com */ public static File saveFile(String filename, InputStream input) throws IOException { // in case there is no filename, create temporary file and use its // filename instead String tempDir = HandleHome.getTrackplus_Home() + File.separator + HandleHome.DATA_DIR + File.separator + "temp"; File dirTem = new File(tempDir); if (!dirTem.exists()) { dirTem.mkdirs(); } File file; if (filename == null) { file = File.createTempFile("xxxxxx", ".out", dirTem); } else { // Existing files will not be overwritten file = new File(dirTem, filename); } int i = 0; while (file.exists()) { // Extend filename by number so it is unique file = new File(dirTem, i + filename); i++; } // BufferedOutputStream for writing into the file BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); // BufferedInputStream for reading the parts BufferedInputStream bis = new BufferedInputStream(input); // read data and write to output stream int aByte; while ((aByte = bis.read()) != -1) { bos.write(aByte); } // release resources bos.flush(); bos.close(); bis.close(); return file; }
From source file:org.ancoron.osgi.test.glassfish.GlassfishHelper.java
public static void unzip(String zipFile, String targetPath) throws IOException { log.log(Level.INFO, "Extracting {0} ...", zipFile); log.log(Level.INFO, "...target directory: {0}", targetPath); File path = new File(targetPath); path.delete();/*from w ww .ja v a 2 s.c o m*/ path.mkdirs(); BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(zipFile); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { File f = new File(targetPath + "/" + entry.getName()); f.mkdirs(); continue; } int count; byte data[] = new byte[BUFFER]; // write the files to the disk FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName()); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } zis.close(); }
From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java
/** * Extracts the specified folder from the specified archive, into the supplied output directory. * /*from ww w. j a v a 2 s.co m*/ * @param archivePath * the archive Path * @param folderToExtract * the folder to extract * @param outputDirectory * the output directory * @return the extracted folder path * @throws IOException * if a problem occurs while extracting */ public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract, String outputDirectory) throws IOException { File destinationFolder = new File(outputDirectory); destinationFolder.mkdirs(); ZipFile zip = null; try { zip = new ZipFile(new File(archivePath)); Enumeration<?> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); if (currentEntry.startsWith(folderToExtract)) { File destFile = new File(destinationFolder, currentEntry); destFile.getParentFile().mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER_SIZE]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER_SIZE); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, currentByte); } } finally { if (dest != null) { dest.flush(); } IOUtils.closeQuietly(dest); IOUtils.closeQuietly(is); } } } } } finally { if (zip != null) { zip.close(); } } return new File(destinationFolder, folderToExtract); }
From source file:com.glaf.core.util.ZipUtils.java
public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream) { Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>(); java.util.zip.ZipEntry zipEntry = null; ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; byte tmpByte[] = null; try {/* w w w . j a v a 2 s .co m*/ while ((zipEntry = zipInputStream.getNextEntry()) != null) { tmpByte = new byte[BUFFER]; baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos, BUFFER); int i = 0; while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) { bos.write(tmpByte, 0, i); } bos.flush(); byte[] bytes = baos.toByteArray(); IOUtils.closeStream(baos); IOUtils.closeStream(baos); zipMap.put(zipEntry.getName(), bytes); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeStream(baos); IOUtils.closeStream(baos); } return zipMap; }