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 copyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {/*from   w  w  w .j ava2 s.  c om*/
        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) {
    }
}

From source file:Main.java

public static void copyFile(InputStream in, File dest) throws IOException {
    if (!dest.exists()) {
        dest.createNewFile();/*  www.  j a  v a  2 s  . co  m*/
    }
    OutputStream out = null;
    try {
        out = new FileOutputStream(dest);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:eu.planets_project.pp.plato.util.FileUtils.java

public static void writeToFile(InputStream in, OutputStream out) throws IOException {
    try {/* w  ww . jav a  2 s.  c o  m*/
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } catch (FileNotFoundException ex) {
        log.debug("Error copying file " + ex.getMessage(), ex);
    } finally {
        try {
            in.close();
        } catch (Exception skip) {
        }
        try {
            out.close();
        } catch (Exception skip) {
        }

    }
}

From source file:Main.java

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {//from w  ww. j  a  va2  s .  com
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
        os.flush();
    } catch (Exception ex) {
    }
}

From source file:Main.java

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {//from   ww  w  .j ava  2  s.c om
        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 e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void upZipFile(File zipFile, String folderPath) {
    ZipFile zf;/*from w  w  w. j a v  a 2 s. c  om*/
    try {
        zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.isDirectory()) {
                String dirstr = entry.getName();
                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }

            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }

            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void unzip(InputStream is, OutputStream os) {
    GZIPInputStream gzip = null;//from w w  w .j  a v a 2s.c o  m
    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, os);
    }
}

From source file:ar.com.zauber.commons.message.message.templates.AbstractTemplate.java

/**
 * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
 * <code>OutputStream</code>.
 * <p>/*from   www . j a  v  a2  s.c  o m*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param input  the <code>InputStream</code> to read from
 * @param output  the <code>OutputStream</code> to write to
 * @return the number of bytes copied
 * @throws NullPointerException if the input or output is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.3
 */
public static long copyLarge(final InputStream input, final OutputStream output) throws IOException {
    byte[] buffer = new byte[1024 * 4];
    long count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static void unzip(String zipFilePath, String fileName) {

    try {//from w  ww  . j ava2 s  .  c  o m
        FileInputStream fin = new FileInputStream(zipFilePath);
        ZipInputStream zin = new ZipInputStream(fin);

        do {
            // no-op
        } while (!zin.getNextEntry().getName().equals(fileName));

        OutputStream os = new FileOutputStream(fileName);

        byte[] buffer = new byte[1024];
        int length;

        //read the entry from zip file and extract it to disk
        while ((length = zin.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.close();
        zin.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.predic8.membrane.examples.DistributionExtractingTestcase.java

public static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int len;/*from   w w  w  .  j a va2 s  .  c  o  m*/

    while ((len = in.read(buffer)) >= 0)
        out.write(buffer, 0, len);

    in.close();
    out.close();
}