Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

In this page you can find the example usage for java.io OutputStream write.

Prototype

public void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this output stream.

Usage

From source file:Main.java

public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
    final byte[] buffer = new byte[bufferSize];
    int n;/*from  w w  w. j  a  v  a 2 s  . com*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
}

From source file:Main.java

public static void copy(InputStream src, File dst) throws IOException {
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;// w  ww.  jav a2s. c  om
    while ((len = src.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    src.close();
    out.close();
}

From source file:Main.java

/**
 * Copies inputStream to outputStream in a somewhat buffered way
 * @param in Input stream//from  w  ww  . ja  va 2s . c  o m
 * @param out Output stream
 * @throws IOException if the operation fails
 */
public static void copyStream(InputStream in, OutputStream out)
        throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

public static File saveTmpFile(InputStream is) throws Exception {
    File file = getTmpFile();/*from   ww  w .j a v  a2s.  c o  m*/
    OutputStream os = new FileOutputStream(file);

    byte[] buf = new byte[1024];
    int len = 0;

    while ((len = is.read(buf)) > 0) {
        os.write(buf, 0, len);
    }

    os.flush();
    os.close();

    return file;
}

From source file:Main.java

public static void copy(InputStream is, OutputStream os) throws IOException {
    byte[] buffer = new byte[1024];
    int len;/*from  ww  w .  ja va 2 s  .  com*/
    while ((len = is.read(buffer)) != -1) {
        os.write(buffer, 0, len);
    }
}

From source file:Main.java

public static void copyfile(File src, File dec) {
    try {/*from w  ww.  j  a v  a 2  s. c  o  m*/
        if (src == null || dec == null) {
            return;
        }

        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dec);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String extractFromAssets(Context ctx, String file, String destinationDirectory)
        throws IOException, FileNotFoundException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    AssetManager assetManager = ctx.getAssets();
    InputStream in = assetManager.open(file);
    String destinationFilename = destinationDirectory + File.separator + file;
    OutputStream out = new FileOutputStream(destinationFilename);
    byte[] buffer = new byte[1024];
    int read;//from w w w  . j ava2s  .c o  m
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();
    out.close();
    return destinationFilename;
}

From source file:Main.java

public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }/*from   w  w  w  .ja  v  a2  s  .  c o m*/
}

From source file:Main.java

public final static long copy(InputStream inp, OutputStream out) throws IOException {
    int nread;//  w  w  w.j av a 2s.  co  m
    byte[] buf = new byte[4096];
    long total = 0;
    while ((nread = inp.read(buf)) > 0) {
        total += nread;
        out.write(buf, 0, nread);
    }
    return total;
}

From source file:Main.java

/**
 * Copy the content of the input stream into the output stream, using a temporary byte array
 * buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
 *
 * @param in The input stream to copy from.
 * @param out The output stream to copy to.
 * @return the total length copied//from  w  ww.  j  a  v  a2  s.  c  o  m
 * @throws IOException If any error occurs during the copy.
 */
public static long copy(final InputStream in, final OutputStream out) throws IOException {
    long length = 0;
    final byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
        length += read;
    }
    return length;
}