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:Streams.java

public static void drain(InputStream r, OutputStream w) throws IOException {
    byte[] bytes = new byte[BLOCK_SIZE];
    try {/*w w w .  j  av a  2 s.c  om*/
        int length = r.read(bytes);
        while (length != -1) {
            if (length != 0) {
                w.write(bytes, 0, length);
            }
            length = r.read(bytes);
        }
    } finally {
        bytes = null;
    }

}

From source file:Main.java

/**
 * /*from  ww w .j  av  a2s. c om*/
 * @param context
 * @param backupFolder
 * @param db_name
 * @return
 */
public static boolean backupDB(Context context, String backupFolder, String db_name) {
    boolean result = false;

    try {
        String current_date = DateToString(GetToday(), "dd-MM-yyyy");

        File data = Environment.getDataDirectory();
        File sdcard = new File(Environment.getExternalStorageDirectory(), backupFolder + "/");
        sdcard.mkdirs();

        if (sdcard.canWrite()) {
            String currentDBPath = "//data//" + context.getPackageName() + "//databases//" + db_name + "";
            String backupDBPath = "backup_" + db_name + "_" + current_date + ".db";

            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sdcard, backupDBPath);

            if (currentDB.exists()) {
                InputStream input = new FileInputStream(currentDB);
                OutputStream output = new FileOutputStream(backupDB);

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

                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }

                output.flush();
                output.close();
                input.close();

                result = true;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }

    return result;
}

From source file:Main.java

public static boolean writeFile(String filePath, InputStream inputStream, boolean append) {
    OutputStream o = null;
    boolean result = false;
    try {//from  w w w . ja v a2  s.c om
        o = new FileOutputStream(filePath);
        byte data[] = new byte[1024];
        int length = -1;
        while ((length = inputStream.read(data)) != -1) {
            o.write(data, 0, length);
        }
        o.flush();
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (o != null) {
            try {
                o.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
    return result;
}

From source file:davmail.util.IOUtil.java

/**
 * Write all inputstream content to outputstream.
 *
 * @param inputStream  input stream//from w  w  w.j  a  va  2  s .  c o  m
 * @param outputStream output stream
 * @throws IOException on error
 */
public static void write(InputStream inputStream, OutputStream outputStream) throws IOException {
    byte[] bytes = new byte[8192];
    int length;
    while ((length = inputStream.read(bytes)) > 0) {
        outputStream.write(bytes, 0, length);
    }
}

From source file:Main.java

/**
 * Reads data from the input and writes it to the output, until the end of the
 * input stream./*  w  w w. j  a  v a 2s.c  o  m*/
 * 
 * @param in
 * @param out
 * @param bufSizeHint
 * @throws IOException
 */
public static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) throws IOException {
    int read = -1;
    byte[] buf = new byte[bufSizeHint];
    while ((read = in.read(buf, 0, bufSizeHint)) >= 0) {
        out.write(buf, 0, read);
    }
    out.flush();
}

From source file:org.apache.lucene.replicator.http.ReplicationService.java

private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[16384];
    int numRead;/*  w w w  .  ja  v a 2  s  .c  o  m*/
    while ((numRead = in.read(buf)) != -1) {
        out.write(buf, 0, numRead);
    }
}

From source file:com.clustercontrol.infra.composite.InfraFileUploadReceiver.java

private static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    boolean finished = false;
    while (!finished) {
        int bytesRead = inputStream.read(buffer);
        if (bytesRead != -1) {
            outputStream.write(buffer, 0, bytesRead);
        } else {/*from   ww  w  .  j  a  va 2  s . c om*/
            finished = true;
        }
    }
}

From source file:de.ingrid.iplug.csw.dsc.TestUtil.java

public static void copy(InputStream inputStream, OutputStream outputStream, boolean close) throws IOException {
    byte[] buf = new byte[1024];
    int len;//from   www  . ja va 2 s .  c  o  m
    while ((len = inputStream.read(buf)) > 0) {
        outputStream.write(buf, 0, len);
    }
    if (close) {
        inputStream.close();
        outputStream.close();
    }
}

From source file:com.sharneng.io.IOUtils.java

/**
 * Read everything from <code>in</code> and write to <code>out</code> until reachs the end of the input stream. If
 * <code>closeOnFinish</code> is <code>true</code>, it quitely closes both <code>in</code> and <code>out</code> (see
 * {@link #close(InputStream)}) before retun. Otherwise, it leaves both of them open.
 * /*ww  w  .  java2  s . co  m*/
 * @param in
 *            data copy from
 * @param out
 *            data copy to
 * @param closeOnFinish
 *            passing <code>true</code> to close both stream before method return
 * @throws IOException
 *             IO error occured when coping.
 * @see #copyStream(InputStream, OutputStream)
 */
public static void copyStream(InputStream in, OutputStream out, boolean closeOnFinish) throws IOException {
    byte[] buf = new byte[BUF_SIZE];
    int count;
    try {
        while ((count = in.read(buf)) != -1) {
            out.write(buf, 0, count);
        }
    } finally {
        if (closeOnFinish)
            close(in);
        if (closeOnFinish)
            close(out);
    }
}

From source file:com.wso2telco.services.bw.FileUtil.java

public static void copy(String src, String dst) throws IOException {

    String fileName = src.substring(src.lastIndexOf("/") + 1);

    File fsrc = new File(src);
    File fdst = new File(dst + "/" + fileName);

    InputStream in = new FileInputStream(fsrc);
    OutputStream out = new FileOutputStream(fdst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;/*  w w  w  . j ava2  s . com*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}