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 inputStreamToFile(InputStream is, File file) {
    OutputStream os = null;
    try {//from w w w .  ja  va  2  s. co m
        os = new FileOutputStream(file);

        int bytesRead = 0;
        byte[] buffer = new byte[3072];

        while ((bytesRead = is.read(buffer, 0, 3072)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        is.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyFile(File from, File toFile, PrintStream reportStream) {
    if (reportStream != null) {
        reportStream.println("Coping file " + from.getAbsolutePath() + " to " + toFile.getAbsolutePath());
    }/* w ww  .ja v  a  2s  .  c o m*/
    if (!from.exists()) {
        throw new IllegalArgumentException("File " + from.getPath() + " does not exist.");
    }
    if (from.isDirectory()) {
        throw new IllegalArgumentException(from.getPath() + " is a directory. Should be a file.");
    }
    try {
        final InputStream in = new FileInputStream(from);
        if (!toFile.getParentFile().exists()) {
            toFile.getParentFile().mkdirs();
        }
        if (!toFile.exists()) {
            toFile.createNewFile();
        }
        final OutputStream out = new FileOutputStream(toFile);

        final byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (final IOException e) {
        throw new RuntimeException(
                "IO exception occured while copying file " + from.getPath() + " to " + toFile.getPath(), e);
    }

}

From source file:Main.java

/*****************************************************
 *
 * Transfers data from an input stream to an output stream.
 *
 *****************************************************/
static public void transferBytes(InputStream inputStream, OutputStream outputStream) throws IOException {
    byte[] transferBuffer = new byte[TRANSFER_BUFFER_SIZE_IN_BYTES];

    int byteCount;

    while ((byteCount = inputStream.read(transferBuffer)) >= 0) {
        outputStream.write(transferBuffer, 0, byteCount);
    }/*from w  w  w  .ja  v a 2 s .  c  om*/
}

From source file:models.logic.CipherDecipher.java

public static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);
    }// w  w  w.  java 2 s  .c o  m
    os.flush();
    os.close();
    is.close();
}

From source file:Main.java

static long copy(InputStream from, OutputStream to, int bufferSize) throws IOException {
    byte[] buf = new byte[bufferSize];

    long total = 0;
    while (true) {
        int read = from.read(buf);
        if (read < 0)
            break;
        to.write(buf, 0, read);
        total += read;//from   www.  j a v a  2s. c  om
    }

    return total;
}

From source file:com.nextep.designer.beng.model.impl.FileUtils.java

public static void copyStreams(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[FILE_BUFFER];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) >= 0) {
        out.write(buffer, 0, bytesRead);
    }/* w ww.ja v a 2s  .  co  m*/
}

From source file:JarUtils.java

/**
 * Copies the input stream to the output stream
 *///from w ww. j a va2 s.co m
public static void copyStream(OutputStream outputStream, InputStream inputStream) throws IOException {
    byte[] bytes = new byte[4096];
    int read = inputStream.read(bytes, 0, 4096);
    while (read > 0) {
        outputStream.write(bytes, 0, read);
        read = inputStream.read(bytes, 0, 4096);
    }
}

From source file:com.questdb.test.tools.HttpTestUtils.java

public static void copy(InputStream is, OutputStream os) throws IOException {
    byte[] buf = new byte[4096];
    int l;/* w ww .  j a v  a 2  s .c  om*/
    while ((l = is.read(buf)) > 0) {
        os.write(buf, 0, l);
    }
}

From source file:CompressTransfer.java

/**
 * ?/*  w w w. ja v a 2s .  c  o  m*/
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void decompress(InputStream is, OutputStream os) throws Exception {

    GZIPInputStream gis = new GZIPInputStream(is);
    int count;
    byte data[] = new byte[BUFFER];
    while ((count = gis.read(data, 0, BUFFER)) != -1) {
        os.write(data, 0, count);
    }

    gis.close();
}

From source file:com.dragome.callbackevictor.serverside.utils.RewritingUtils.java

/**
 * Copies the entire {@link InputStream} to the given {@link OutputStream}.
 *
 * @return// ww  w  .j a  v a 2s .  c om
 *      the number of bytes copied.
 */
public static int copy(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[8192];
    int n;
    int total = 0;
    while ((n = in.read(buf)) >= 0) {
        out.write(buf, 0, n);
        total += n;
    }
    return total;
}