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 unzip(InputStream is, OutputStream os) {
    GZIPInputStream gzip = null;//from  w w w  .  j a  v  a2 s . c  om
    try {
        gzip = new GZIPInputStream(is);
        byte[] buf = new byte[1024];
        int len;
        while ((len = gzip.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(gzip);
        closeIO(os);
    }
}

From source file:Main.java

/**
 * Copies all available data from in to out without closing any stream.
 * //from  w w  w  . jav a2  s  .  com
 * @return number of bytes copied
 */
public static int copyAllBytes(InputStream in, OutputStream out) throws IOException {
    int byteCount = 0;
    byte[] buffer = new byte[4096];
    while (true) {
        int read = in.read(buffer);
        if (read == -1) {
            break;
        }
        out.write(buffer, 0, read);
        byteCount += read;
    }
    return byteCount;
}

From source file:Main.java

/**
 * This method copies data from input stream to output stream.
 * // www  .  j a va 2  s .  co  m
 * @param is
 * @param os
 */
public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {
        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:Main.java

/** Copy binary file */
public static boolean copyFile(File srcFile, File dstFile) {
    try {//w w w  . ja  v a 2 s . co m
        InputStream in = new FileInputStream(srcFile);
        if (dstFile.exists()) {
            dstFile.delete();
        }

        OutputStream out = new FileOutputStream(dstFile);
        try {
            int cnt;
            byte[] buf = new byte[4096];
            while ((cnt = in.read(buf)) >= 0) {
                out.write(buf, 0, cnt);
            }
        } finally {
            out.close();
            in.close();
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

static boolean copyFiles(File sourceLocation, File targetLocation) throws IOException {
    if (sourceLocation.equals(targetLocation))
        return false;

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();/*from  ww w.j av a2  s .  co  m*/
        }
        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyFiles(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else if (sourceLocation.exists()) {
        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    return true;
}

From source file:Main.java

public static void copy(InputStream inputStream, File output) throws IOException {
    OutputStream outputStream = null;
    try {//from   w  w  w  . j a  va  2 s.com
        outputStream = new FileOutputStream(output);
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}

From source file:CompressTransfer.java

/**
 * ?//w ww .ja v a2  s .c om
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void bzdecompress(InputStream is, OutputStream os) throws Exception {

    BZip2CompressorInputStream gis = new BZip2CompressorInputStream(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:Main.java

/**
 * Copies data from IS to OS in chunks of 1024 bytes.
 * /* w w w .j a v a2  s . c o  m*/
 * 
 * @param inputStream
 *            where data needs to be read from
 * @param outputStream
 *            where data needs to be written
 * @throws IOException
 *             in case of error.
 */
public static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
    final int BUFFER_SIZE = 1024;
    byte[] bytes = new byte[BUFFER_SIZE];
    while (true) {
        int count = inputStream.read(bytes, 0, BUFFER_SIZE);
        if (count == -1)
            break;
        outputStream.write(bytes, 0, count);
    }
}

From source file:edu.isi.webserver.FileUtil.java

public static void copyFiles(File destination, File source) throws FileNotFoundException, IOException {
    if (!destination.exists())
        destination.createNewFile();//from w  w w .  j a v  a2s. c  o  m
    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(destination);

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

    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    logger.debug("Done copying contents of " + source.getName() + " to " + destination.getName());
}

From source file:Main.java

public static File writeFromInput(String path, String fileName, InputStream is) {
    File file = createFile(path, fileName);

    OutputStream os = null;
    try {/* w ww .ja v a2  s .  c  o  m*/
        os = new FileOutputStream(file);
        byte[] buffer = new byte[4 * 1024];
        int read = 0;
        while ((read = is.read(buffer)) != -1) {
            os.write(buffer, 0, read);
        }
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return file;
}