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:com.shadwelldacunha.byteswipe.core.Utilities.java

public static void copyResource(String resource, String destination) throws IOException {
    ClassLoader classLoader = Utilities.class.getClassLoader();
    InputStream resStreamIn = classLoader.getResourceAsStream(resource);
    File resDestFile = new File(destination);
    OutputStream resStreamOut = new FileOutputStream(resDestFile);
    int readBytes;
    byte[] buffer = new byte[1024];
    while ((readBytes = resStreamIn.read(buffer)) > 0) {
        resStreamOut.write(buffer, 0, readBytes);
    }/*from w  w  w .j a  v a 2 s . com*/
    resStreamIn.close();
    resStreamOut.close();
}

From source file:Main.java

public static void copy(InputStream stream, String path) throws IOException {

    final File file = new File(path);
    if (file.exists()) {
        file.delete();//from   ww w .j a v a2  s  . c  om
    }
    final File parentFile = file.getParentFile();

    if (!parentFile.exists()) {
        //noinspection ResultOfMethodCallIgnored
        parentFile.mkdir();
    }

    if (file.exists()) {
        return;
    }
    OutputStream myOutput = new FileOutputStream(path, true);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = stream.read(buffer)) >= 0) {
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    stream.close();

}

From source file:Main.java

public static void copy(File src, File dest) throws IOException {

    if (src.isDirectory()) {

        //if directory not exists, create it
        if (!dest.exists()) {
            dest.mkdir();/* w w  w.  j  a v  a2s .c  o  m*/
            System.out.println("Directory copied from " + src + "  to " + dest);
        }

        //list all the directory contents
        String files[] = src.list();

        for (int c = 0; c < files.length; c++) {
            //construct the src and dest file structure
            File srcFile = new File(src, files[c]);
            File destFile = new File(dest, files[c]);
            //recursive copy
            copy(srcFile, destFile);
        }

    } else {
        //if file, then copy it
        //Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes 
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
        System.out.println("File copied from " + src + " to " + dest);
    }
}

From source file:cn.im47.cloud.storage.utilities.RangingResourceHttpRequestHandler.java

public static void copyStream(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[1000];
    int l = 1;/*from  ww w  .j a  va2s.c  o m*/
    while (l > 0) {
        l = in.read(buf);
        if (l > 0) {
            out.write(buf, 0, l);
        }
    }
}

From source file:com.inmobi.conduit.distcp.tools.util.TestThrottledInputStream.java

private static void copyBytes(InputStream in, OutputStream out, int buffSize) throws IOException {

    byte buf[] = new byte[buffSize];
    int bytesRead = in.read(buf);
    while (bytesRead >= 0) {
        out.write(buf, 0, bytesRead);
        bytesRead = in.read(buf);//  ww  w. j a  v a 2  s  .c  om
    }
}

From source file:edu.isi.karma.util.FileUtil.java

public static void copyFiles(File destination, File source) throws FileNotFoundException, IOException {
    if (!destination.exists()) {
        destination.createNewFile();/*from   w w  w .jav  a  2 s  .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:gridool.db.partitioning.phihash.csv.distmm.InMemoryIndexHelper.java

/**
 * Synchronization is required./*from w w  w .  j a v  a2  s  .c om*/
 */
public static void writeToFile(final InputStream in) throws IOException {
    final byte[] recordBuf = new byte[2048]; // big buffer enough for a record
    final Map<String, OutputStream> outputMap = new HashMap<String, OutputStream>(12);
    while (in.available() > 0) {
        String fkIdxName = IOUtils.readString(in);
        int bucket = IOUtils.readInt(in);
        int recordlen = IOUtils.readInt(in);
        IOUtils.readFully(in, recordBuf, 0, recordlen);

        OutputStream out = prepareFkOutputStream(fkIdxName, bucket, outputMap);
        out.write(recordBuf, 0, recordlen);
    }
    for (OutputStream out : outputMap.values()) {
        out.flush();
        out.close();
    }
}

From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java

/**
 * Save compressed stream.//  ww  w. ja  v  a  2  s  . com
 *
 * @param buffer
 *            the buffer
 * @param out
 *            the out
 * @param len
 *            the len
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void saveCompressedStream(final byte[] buffer, final OutputStream out, final int len)
        throws IOException {
    try {
        out.write(buffer, 0, len);

    } catch (Exception e) {
        out.flush();
        out.close();

        IOException ioe = new IOException("Not valid archive file type.");
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:Main.java

/**
 * Compresses a GZIP file./*from w  ww  . jav a  2 s. co m*/
 * 
 * @param bytes
 *            The uncompressed bytes.
 * @return The compressed bytes.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static byte[] gzip(byte[] bytes) throws IOException {
    /* create the streams */
    InputStream is = new ByteArrayInputStream(bytes);
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        OutputStream os = new GZIPOutputStream(bout);
        try {
            /* copy data between the streams */
            byte[] buf = new byte[4096];
            int len = 0;
            while ((len = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, len);
            }
        } finally {
            os.close();
        }

        /* return the compressed bytes */
        return bout.toByteArray();
    } finally {
        is.close();
    }
}

From source file:de.dakror.scpuller.SCPuller.java

public static void copyInputStream(InputStream is, OutputStream out) throws Exception {
    byte[] buffer = new byte[2048];
    int len = is.read(buffer);
    while (len != -1) {
        out.write(buffer, 0, len);
        len = is.read(buffer);//from   w  w  w.j a va  2s. c o m
    }
    is.close();
    out.close();
}