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 assetsDataToSD(String fileOutPutName, String fileInPutName, Context context)
        throws IOException {
    InputStream myInput;/*ww w.  j ava2s .  c  o  m*/
    File file = new File(fileOutPutName);
    /*if (!file.exists()) {
    file.createNewFile();
       }else {
       return;
    }*/
    OutputStream myOutput = new FileOutputStream(fileOutPutName);
    myInput = context.getAssets().open(fileInPutName);
    byte[] buffer = new byte[1024];
    int length = myInput.read(buffer);
    while (length > 0) {
        myOutput.write(buffer, 0, length);
        length = myInput.read(buffer);
    }

    myOutput.flush();
    myInput.close();
    myOutput.close();
}

From source file:javarestart.Utils.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    int nRead;//ww w.j a  va2  s.  co  m
    byte[] data = new byte[16384];

    while ((nRead = in.read(data, 0, data.length)) != -1) {
        out.write(data, 0, nRead);
    }
}

From source file:Main.java

public static File getFile(Context context, Uri uri, boolean forceCreation) {

    if (!forceCreation && "file".equalsIgnoreCase(uri.getScheme())) {
        return new File(uri.getPath());
    }/*from   ww w  . j  av  a 2 s . c  o  m*/

    File file = null;

    try {
        File root = context.getFilesDir();
        if (root == null) {
            throw new Exception("data dir not found");
        }
        file = new File(root, getFilename(context, uri));
        file.delete();
        InputStream is = context.getContentResolver().openInputStream(uri);
        OutputStream os = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int cnt = is.read(buf);
        while (cnt > 0) {
            os.write(buf, 0, cnt);
            cnt = is.read(buf);
        }
        os.close();
        is.close();
        file.deleteOnExit();
    } catch (Exception e) {
        Log.e("OpenFile", e.getMessage(), e);
    }
    return file;
}

From source file:Main.java

/**
 * Copy data from a source stream to destFile. Return true if succeed,
 * return false if failed.//from w w  w.  j a  v a 2  s.  co  m
 */

private static boolean copyToFile(InputStream inputStream, File destFile) {
    try {
        OutputStream out = new FileOutputStream(destFile);
        try {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesRead);
            }
        } finally {
            out.close();
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:com.gistlabs.mechanize.util.Util.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    try {//from   w  w w  . j  av  a 2 s. co  m
        byte[] buffer = new byte[1024 * 16];
        int len = in.read(buffer);
        while (len != -1) {
            out.write(buffer, 0, len);
            len = in.read(buffer);
        }
    } finally {
        try {
            in.close();
        } catch (Throwable t) {
        }
        try {
            out.close();
        } catch (Throwable t) {
        }
    }
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);
    }/* ww w  . j  av a  2 s  . c o  m*/
    os.flush();
    os.close();
    is.close();
}

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

/**
 * Copies bytes from a large (over 2GB) <code>InputStream</code> to an
 * <code>OutputStream</code> via a <code>byte</code> buffer. This
 * method buffers input internally, so the input stream should not
 * be a <code>BufferedInputStream</code>.
 *
 * @param in the source/*ww w.j  a v  a2s  .  co  m*/
 * @param out the destination
 * @param buffer the buffer
 * @return the number of bytes copied
 * @throws IOException if one occurs while reading or writing
 */
public static long copyLarge(InputStream in, OutputStream out, byte[] buffer) throws IOException {
    long count = 0;
    int n = 0;
    while ((n = in.read(buffer)) != -1) {
        out.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

/**
 * Copies an input stream to an output stream. When the reading is done, the
 * input stream is closed.//from  w  ww.  j a  v a  2s  .  com
 * 
 * @param inputStream
 *            The input stream.
 * @param outputStream
 *            The output stream.
 * @throws IOException
 */
public static void copy(InputStream inputStream, java.io.OutputStream outputStream) throws IOException {
    int bytesRead;
    byte[] buffer = new byte[2048];

    while ((bytesRead = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, bytesRead);
    }

    outputStream.flush();
    inputStream.close();
}

From source file:com.opengamma.web.analytics.json.Compressor.java

static void decompressStream(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    @SuppressWarnings("resource")
    InputStream iStream = new GZIPInputStream(
            new Base64InputStream(new BufferedInputStream(inputStream), false, -1, null));
    OutputStream oStream = new BufferedOutputStream(outputStream);
    byte[] buffer = new byte[2048];
    int bytesRead;
    while ((bytesRead = iStream.read(buffer)) != -1) {
        oStream.write(buffer, 0, bytesRead);
    }//from   w  w  w . j av a 2  s . c o m
    oStream.flush();
}

From source file:Main.java

public static long copy(InputStream from, OutputStream to) throws IOException {
    byte[] buf = new byte[4096];
    long total = 0L;

    while (true) {
        int r = from.read(buf);
        if (r == -1) {
            return total;
        }//from   w  ww  .  ja v  a  2 s  .  co m

        to.write(buf, 0, r);
        total += (long) r;
    }
}