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:jp.co.opentone.bsol.linkbinder.util.AttachmentUtil.java
/** * ?????.// w w w. j a va2 s . c om * @param randomId ????????? * @param baseName ?????. ????? * @param in ???? * @throws IOException ?? * @return ???? */ public static String createTempporaryFile(String randomId, String baseName, InputStream in) throws IOException { String result = createFileName(randomId, baseName); InputStream bin = null; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(result)); try { bin = new BufferedInputStream(in); //CHECKSTYLE:OFF byte[] buf = new byte[1024 * 4]; //CHECKSTYLE:ON int len; while ((len = bin.read(buf, 0, buf.length)) != -1) { out.write(buf, 0, len); } return result; } finally { if (bin != null) { bin.close(); } out.close(); } }
From source file:org.openmrs.module.report.util.FileUtils.java
public static void copyFile(InputStream in, File dest) throws Exception { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); byte[] data = new byte[2 * 1024]; int count;//from w w w. j a v a2s . c om while ((count = in.read(data, 0, data.length)) != -1) { out.write(data, 0, count); } out.flush(); out.close(); in.close(); }
From source file:com.seleniumtests.util.FileUtility.java
public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException { File firefoxProfile = new File(storeLocation); String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile(); try (JarFile jar = new JarFile(location);) { logger.info("Extracting jar file::: " + location); firefoxProfile.mkdir();// w w w .j av a 2 s. c o m Enumeration<?> jarFiles = jar.entries(); while (jarFiles.hasMoreElements()) { ZipEntry entry = (ZipEntry) jarFiles.nextElement(); String currentEntry = entry.getName(); File destinationFile = new File(storeLocation, currentEntry); File destinationParent = destinationFile.getParentFile(); // create the parent directory structure if required destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry)); int currentByte; // buffer for writing file byte[] data = new byte[BUFFER]; // write the current file to disk try (FileOutputStream fos = new FileOutputStream(destinationFile);) { BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER); // read and write till last byte while ((currentByte = is.read(data, 0, BUFFER)) != -1) { destination.write(data, 0, currentByte); } destination.flush(); destination.close(); is.close(); } } } } FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF")); if (OSUtility.isWindows()) { new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete(); } else { new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete(); } }
From source file:com.priocept.jcr.server.UploadServlet.java
public static void writeToFile(String fileName, InputStream iStream, boolean createDir, String servletRealPath) throws IOException { String me = "FileUtils.WriteToFile"; if (fileName == null) { throw new IOException(me + ": filename is null"); }/*from w w w . j a v a 2 s . c o m*/ if (iStream == null) { throw new IOException(me + ": InputStream is null"); } File theFile = new File(servletRealPath + "temp_files/" + fileName); // Check if a file exists. if (theFile.exists()) { String msg = theFile.isDirectory() ? "directory" : (!theFile.canWrite() ? "not writable" : null); if (msg != null) { throw new IOException(me + ": file '" + fileName + "' is " + msg); } } // Create directory for the file, if requested. if (createDir && theFile.getParentFile() != null) { theFile.getParentFile().mkdirs(); } // Save InputStream to the file. BufferedOutputStream fOut = null; try { fOut = new BufferedOutputStream(new FileOutputStream(theFile)); byte[] buffer = new byte[32 * 1024]; int bytesRead = 0; while ((bytesRead = iStream.read(buffer)) != -1) { fOut.write(buffer, 0, bytesRead); } } catch (Exception e) { throw new IOException(me + " failed, got: " + e.toString()); } finally { close(iStream, fOut); } }
From source file:JarUtils.java
/** Given a URL check if its a jar url(jar:<url>!/archive) and if it is, extract the archive entry into the given dest directory and return a file URL to its location. If jarURL is not a jar url then it is simply returned as the URL for the jar./* w w w . ja v a 2s . co m*/ @param jarURL the URL to validate and extract the referenced entry if its a jar protocol URL @param dest the directory into which the nested jar will be extracted. @return the file: URL for the jar referenced by the jarURL parameter. * @throws IOException */ public static URL extractNestedJar(URL jarURL, File dest) throws IOException { // This may not be a jar URL so validate the protocol if (jarURL.getProtocol().equals("jar") == false) return jarURL; String destPath = dest.getAbsolutePath(); URLConnection urlConn = jarURL.openConnection(); JarURLConnection jarConn = (JarURLConnection) urlConn; // Extract the archive to dest/jarName-contents/archive String parentArchiveName = jarConn.getJarFile().getName(); // Find the longest common prefix between destPath and parentArchiveName int length = Math.min(destPath.length(), parentArchiveName.length()); int n = 0; while (n < length) { char a = destPath.charAt(n); char b = parentArchiveName.charAt(n); if (a != b) break; n++; } // Remove any common prefix from parentArchiveName parentArchiveName = parentArchiveName.substring(n); File archiveDir = new File(dest, parentArchiveName + "-contents"); if (archiveDir.exists() == false && archiveDir.mkdirs() == false) throw new IOException( "Failed to create contents directory for archive, path=" + archiveDir.getAbsolutePath()); String archiveName = jarConn.getEntryName(); File archiveFile = new File(archiveDir, archiveName); File archiveParentDir = archiveFile.getParentFile(); if (archiveParentDir.exists() == false && archiveParentDir.mkdirs() == false) throw new IOException( "Failed to create parent directory for archive, path=" + archiveParentDir.getAbsolutePath()); InputStream archiveIS = jarConn.getInputStream(); FileOutputStream fos = new FileOutputStream(archiveFile); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] buffer = new byte[4096]; int read; while ((read = archiveIS.read(buffer)) > 0) { bos.write(buffer, 0, read); } archiveIS.close(); bos.close(); // Return the file url to the extracted jar return archiveFile.toURL(); }
From source file:org.openmrs.module.report.util.FileUtils.java
public static boolean copyFile(File source, File dest) { try {/*from ww w. j a v a2s. co m*/ 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; } }
From source file:com.byraphaelmedeiros.autosubdown.AutoSubDown.java
private static void downloadTo(Rule rule, String link) { LOGGER.log(Level.INFO, "Downloading " + link + "..."); try {/* w w w . j ava2 s.c om*/ String extension = FilenameUtils.getExtension(link); BufferedInputStream bis = new BufferedInputStream(new URL(link).openStream()); FileOutputStream fos = new FileOutputStream( rule.getDownloadTo() + File.separator + rule.getFileName() + "." + extension); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); byte data[] = new byte[1024]; int x = 0; while ((x = bis.read(data, 0, 1024)) >= 0) { bos.write(data, 0, x); } bos.close(); bis.close(); } catch (MalformedURLException e) { //e.printStackTrace(); LOGGER.log(Level.WARNING, "Invalid URL! (" + link + ")"); } catch (IOException e) { //e.printStackTrace(); LOGGER.log(Level.WARNING, "Unable to access the URL (" + link + ")."); } }
From source file:fm.last.commons.io.LastFileUtils.java
/** * Writes the contents of the passed input stream to the passed file and closes the InputStream when done. * /* w ww.ja va 2 s .c om*/ * @param inputStream Input stream to write to file. * @param file The file to write to. * @param makeDirs Whether to create the files parent dir(s) or not. * @throws IOException If an error occurs writing the InputStream to the file. */ public static void writeToFile(InputStream inputStream, File file, boolean makeDirs) throws IOException { if (makeDirs && file.getParentFile() != null && !file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { throw new IOException("Error creating '" + file.getParentFile().getAbsolutePath() + "'"); } } BufferedOutputStream outputStream = null; try { outputStream = new BufferedOutputStream(new FileOutputStream(file)); byte[] inBuffer = new byte[32 * 1024]; int bytesRead = 0; while ((bytesRead = inputStream.read(inBuffer)) != -1) { outputStream.write(inBuffer, 0, bytesRead); } } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } }
From source file:com.appspresso.api.fs.FileSystemUtils.java
/** * InputStream path? ? .//from ww w . j av a 2 s . c o m * * @param inputStream ? InputStream * @param destFilePath ?? path * @param overwrite path? ? ?? ? * @return ? {@literal true}, {@literal false} * @throws IOException ?? ?. */ public static boolean copy(InputStream inputStream, String destFilePath, boolean overwrite) throws IOException { File destFile = new File(destFilePath); File parent = destFile.getParentFile(); if (!parent.exists() && !parent.mkdirs() && !parent.mkdir()) { return false; } if (destFile.exists()) { if (!overwrite) { return false; } if (!destFile.delete()) { return false; } } if (!destFile.createNewFile()) { return false; } FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(destFile); BufferedOutputStream bOutputStream = new BufferedOutputStream(outputStream); byte[] buffer = new byte[BUFFER_SIZE]; int length = -1; while ((length = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1) { bOutputStream.write(buffer, 0, length); } bOutputStream.flush(); return true; } finally { closeQuietly(inputStream); closeQuietly(outputStream); } }
From source file:JarMaker.java
/** * Unpack the jar file to a directory, till teaf files * @param file The source file name//from ww w . j a va 2s . c om * @param file1 The target directory * @author wangxp * @version 2003/11/07 */ public static void unpackAppJar(String file, String file1) throws IOException { // m_log.debug("unpackAppJar begin. file="+file+"|||file1="+file1); BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream); byte abyte0[] = new byte[1024]; for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream .getNextEntry()) { File file2 = new File(file1, zipentry.getName()); if (zipentry.isDirectory()) { if (!file2.exists() && !file2.mkdirs()) throw new IOException("Could not make directory " + file2.getPath()); } else { File file3 = file2.getParentFile(); if (file3 != null && !file3.exists() && !file3.mkdirs()) throw new IOException("Could not make directory " + file3.getPath()); BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file2)); int i; try { while ((i = zipinputstream.read(abyte0, 0, abyte0.length)) != -1) bufferedoutputstream.write(abyte0, 0, i); } catch (IOException ie) { ie.printStackTrace(); // m_logger.error(ie); throw ie; } bufferedoutputstream.close(); } } zipinputstream.close(); bufferedinputstream.close(); // m_log.debug("unpackAppJar end."); }