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 writeAmrFileHeader(OutputStream out) throws IOException {
    byte[] header = new byte[6];
    header[0] = '#'; // AMR header
    header[1] = '!';
    header[2] = 'A';
    header[3] = 'M';
    header[4] = 'R';
    header[5] = '\n';
    out.write(header, 0, 6);
}

From source file:Main.java

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

    // Transfer bytes from in to out
    byte[] buf = buffers.get();
    int len;/*  ww  w.  java2 s  .  co  m*/
    while ((len = in.read(buf)) > 0) {
        Thread.yield();
        out.write(buf, 0, len);
    }
    out.close();
}

From source file:Main.java

public static void copyAssets(Context context, String dir, String fileName) {
    // String[] files;
    File mWorkingPath = new File(dir);
    if (!mWorkingPath.exists()) {
        if (!mWorkingPath.mkdirs()) {
        }//from   w ww .  j  av a  2  s.c o  m
    }
    try {
        InputStream in = context.getAssets().open(fileName);
        System.err.println("");
        File outFile = new File(mWorkingPath, fileName);
        OutputStream out = new FileOutputStream(outFile);
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

From source file:Main.java

public static boolean copyAsset(Context context, String fromFile, String toFile) throws IOException {

    InputStream in = context.getAssets().open(fromFile);
    OutputStream out = new FileOutputStream(toFile);
    try {/*  w  w  w .  jav  a  2  s  . c  om*/
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    } finally {
        try {
            if (in != null)
                in.close();
            if (out != null)
                out.close();

        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

    try {/*from  w  w w . java2s  .c  o m*/
        InputStream in = context.getAssets().open(filename);
        int max = in.available();
        if (pd != null) {
            pd.setMax(max);
        }

        File file = new File(destfilename);
        OutputStream out = new FileOutputStream(file);
        byte[] byt = new byte[1024];
        int len = 0;
        int total = 0;
        while ((len = in.read(byt)) != -1) {
            out.write(byt, 0, len);
            total += len;
            if (pd != null) {
                pd.setProgress(total);
            }
        }
        out.flush();
        out.close();
        in.close();

        return file;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

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

    // Transfer bytes from in to out
    byte[] buf = buffers.get();
    int len;/*from   www  .  ja  v  a 2 s  . c  o  m*/
    while ((len = in.read(buf)) > 0) {
        Thread.yield();
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:cn.vlabs.clb.api.io.FileUtil.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    try {//from w  w  w.  j  av a2 s.c o  m
        byte[] buff = new byte[4096];
        int count = -1;
        while ((count = in.read(buff)) != -1) {
            out.write(buff, 0, count);
        }
    } finally {
        in.close();
        out.close();
    }
}

From source file:de.nava.informa.utils.FileUtils.java

/**
 * Copies a file from <code>inFile</code> to <code>outFile</code>.
 *//*from   w  w  w .  j  a  v a  2  s  .c o  m*/
public static void copyFile(File inFile, File outFile) {
    try {
        logger.debug("Copying file " + inFile + " to " + outFile);
        InputStream in = new FileInputStream(inFile);
        OutputStream out = new FileOutputStream(outFile);
        byte[] buf = new byte[8 * 1024];
        int n;
        while ((n = in.read(buf)) >= 0) {
            out.write(buf, 0, n);
            out.flush();
        }
        in.close();
        out.close();
    } catch (Exception e) {
        logger.warn("Error occurred while copying file " + inFile + " to " + outFile);
        e.printStackTrace();
    }
}

From source file:Main.java

public static void toStream(final InputStream is, final OutputStream os) throws IOException {
    final int len = 4096;
    final byte[] buffer = new byte[len];
    int read;/*from w  w w  .jav  a  2s .c o m*/
    try {
        while ((read = is.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, read);
        }
    } finally {
        // close the inputstream (which is read to end) but not the output
        // stream, which can still be used
        is.close();
    }
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[BUFFER_SIZE];
    while (true) {
        int count = is.read(bytes, 0, BUFFER_SIZE);
        if (count == -1) {
            break;
        }//  ww w.j a  v  a2s  .c o  m
        os.write(bytes, 0, count);
    }
}