List of usage examples for java.io OutputStream write
public void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this output stream. From source file:com.zb.app.common.file.FileUtils.java
public static void unJar(File jarFile, File toDir) throws IOException { JarFile jar = new JarFile(jarFile); try {/*from w w w . j a v a 2 s. c om*/ Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); if (!entry.isDirectory()) { InputStream in = jar.getInputStream(entry); try { File file = new File(toDir, entry.getName()); if (!file.getParentFile().mkdirs()) { if (!file.getParentFile().isDirectory()) { throw new IOException("Mkdirs failed to create " + file.getParentFile().toString()); } } OutputStream out = new FileOutputStream(file); try { byte[] buffer = new byte[8192]; int i; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { out.close(); } } finally { in.close(); } } } } finally { jar.close(); } }
From source file:hivemall.xgboost.NativeLibLoader.java
/** * Create a temp file that copies the resource from current JAR archive. * * @param libName Library name with a suffix * @param is Input stream to the native library * @return The created temp file//from w ww . j av a 2 s.co m * @throws IOException * @throws IllegalArgumentException */ static File createTempFileFromResource(String libName, InputStream is) throws IOException, IllegalArgumentException { // Create a temporary folder with a random number for the native lib final String uuid = UUID.randomUUID().toString(); final File tempFolder = new File(System.getProperty("java.io.tmpdir"), String.format("%s-%s", getPrefix(libName), uuid)); if (!tempFolder.exists()) { boolean created = tempFolder.mkdirs(); if (!created) { throw new IOException("Failed to create a temporary folder for the native lib"); } } // Prepare buffer for data copying final byte[] buffer = new byte[8192]; int readBytes; // Open output stream and copy the native library into the temporary one File extractedLibFile = new File(tempFolder.getAbsolutePath(), libName); final OutputStream os = new FileOutputStream(extractedLibFile); try { while ((readBytes = is.read(buffer)) != -1) { os.write(buffer, 0, readBytes); } } finally { // If read/write fails, close streams safely before throwing an exception os.close(); is.close(); } return extractedLibFile; }
From source file:Main.java
public static void write(InputStream inputStream, OutputStream outputStream) throws IOException { int len;/*w ww . j av a2 s .c om*/ byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) != -1) outputStream.write(buffer, 0, len); }
From source file:Main.java
public static void CopyStream(InputStream is, OutputStream os) { final int buffer_size = 1024; int total = 0; try {/*from w w w . ja v a2s.c o m*/ byte[] bytes = new byte[buffer_size]; for (;;) { int count = is.read(bytes, 0, buffer_size); total += count; if (count == -1) break; os.write(bytes, 0, count); } } catch (Exception ex) { } }
From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java
private static void copy(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[8192]; int n = 0;/* w ww. j a va 2 s. c om*/ while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } }
From source file:gobblin.util.io.StreamUtils.java
/** * Reads the full contents of a ByteBuffer and writes them to an OutputStream. The ByteBuffer is * consumed by this operation; eg in.remaining() will be 0 after it completes successfully. * @param in ByteBuffer to write into the OutputStream * @param out Destination stream/* w w w .ja v a 2s .co m*/ * @throws IOException If there is an error writing into the OutputStream */ public static void byteBufferToOutputStream(ByteBuffer in, OutputStream out) throws IOException { final int BUF_SIZE = 8192; if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + in.position(), in.remaining()); } else { final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)]; while (in.remaining() > 0) { int bytesToRead = Math.min(in.remaining(), BUF_SIZE); in.get(b, 0, bytesToRead); out.write(b, 0, bytesToRead); } } }
From source file:ml.dmlc.xgboost4j.java.NativeLibLoader.java
/** * Create a temp file that copies the resource from current JAR archive * <p/>//www . ja va 2 s.c o m * The file from JAR is copied into system temp file. * The temporary file is deleted after exiting. * Method uses String as filename because the pathname is "abstract", not system-dependent. * <p/> * The restrictions of {@link File#createTempFile(java.lang.String, java.lang.String)} apply to * {@code path}. * @param path Path to the resources in the jar * @return The created temp file. * @throws IOException * @throws IllegalArgumentException */ static String createTempFileFromResource(String path) throws IOException, IllegalArgumentException { // Obtain filename from path if (!path.startsWith("/")) { throw new IllegalArgumentException("The path has to be absolute (start with '/')."); } String[] parts = path.split("/"); String filename = (parts.length > 1) ? parts[parts.length - 1] : null; // Split filename to prexif and suffix (extension) String prefix = ""; String suffix = null; if (filename != null) { parts = filename.split("\\.", 2); prefix = parts[0]; suffix = (parts.length > 1) ? "." + parts[parts.length - 1] : null; // Thanks, davs! :-) } // Check if the filename is okay if (filename == null || prefix.length() < 3) { throw new IllegalArgumentException("The filename has to be at least 3 characters long."); } // Prepare temporary file File temp = File.createTempFile(prefix, suffix); temp.deleteOnExit(); if (!temp.exists()) { throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist."); } // Prepare buffer for data copying byte[] buffer = new byte[1024]; int readBytes; // Open and check input stream InputStream is = NativeLibLoader.class.getResourceAsStream(path); if (is == null) { throw new FileNotFoundException("File " + path + " was not found inside JAR."); } // Open output stream and copy data between source file in JAR and the temporary file OutputStream os = new FileOutputStream(temp); try { while ((readBytes = is.read(buffer)) != -1) { os.write(buffer, 0, readBytes); } } finally { // If read/write fails, close streams safely before throwing an exception os.close(); is.close(); } return temp.getAbsolutePath(); }
From source file:com.example.facebookfriendviewer.utils.Utils.java
public static void copyStream(InputStream is, OutputStream os) { try {/* w w w.j a v a 2s .co m*/ byte[] bytes = new byte[BUFFER_SIZE]; for (;;) { int count = is.read(bytes, 0, BUFFER_SIZE); if (count == -1) break; os.write(bytes, 0, count); } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:br.org.ipti.guigoh.util.DownloadService.java
public static synchronized void downloadFile(File file, String mimeType) { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext context = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\""); response.setContentLength((int) file.length()); response.setContentType(mimeType);// w w w .j a v a 2 s . c om try { OutputStream out; try (FileInputStream in = new FileInputStream(file)) { out = response.getOutputStream(); byte[] buf = new byte[(int) file.length()]; int count; while ((count = in.read(buf)) >= 0) { out.write(buf, 0, count); } } out.flush(); out.close(); facesContext.responseComplete(); } catch (IOException ex) { System.out.println("Error in downloadFile: " + ex.getMessage()); } }
From source file:Main.java
public static void writeInputStreamToOutputStream(InputStream in, OutputStream out, long maxLength) throws IOException { byte[] buf = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { maxLength -= bytesRead;//from ww w . j ava2s . c om if (maxLength <= 0) { throw new IOException("Stream exceeded max size"); } out.write(buf, 0, bytesRead); } }