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

/**
 * Copy configuration file from assets to data folder.
 *
 * @param file File to copy/*from   w  ww  .j  a  va  2 s  . co m*/
 */
private static void copyAssetToData(File file) {
    try {
        InputStream myInput = mContext.getAssets().open(file.getName());
        String outFileName = file.getPath();
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:SDRecord.java

private static long recordToFile(DatagramPacket packet, OutputStream writer) {
    try {// w  w  w .  jav  a2 s .  c om
        writer.write(packet.getData(), 0, packet.getLength());
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(5);
    }
    return packet.getLength();
}

From source file:net.sf.keystore_explorer.utilities.io.CopyUtil.java

/**
 * Copy data from one stream to another and do not close I/O.
 *
 * @param in//from  w  w  w .j  av a2  s .  c om
 *            Input stream
 * @param out
 *            Output stream
 * @throws IOException
 *             If an I/O problem occurred
 */
public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[2048];
    int i;
    while ((i = in.read(buffer)) > 0) {
        out.write(buffer, 0, i);
    }
}

From source file:Main.java

private static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;/*from   w w  w. j  a va2  s  .  c  o m*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:it.sephiroth.android.library.exif2.io.IOUtils.java

public static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
        throws IOException {
    long count = 0;
    int n = 0;/*from w w w . j  av  a  2s  .c o  m*/
    while (EOF != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static void readFullyWriteToOutputStream(@NonNull InputStream in, @NonNull OutputStream out)
        throws IOException {
    try {//  w ww  .  j a  va  2s  . co  m
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        out.close();
    }
}

From source file:com.cnten.platform.util.FileUtil.java

public static void writeStream(InputStream is, OutputStream os) throws Exception {
    byte[] buff = new byte[1024];
    int readCount = 0;
    readCount = is.read(buff);//  w w w.  ja  v  a 2  s . co  m
    while (readCount != -1) {
        os.write(buff, 0, readCount);
        readCount = is.read(buff);
    }
}

From source file:Main.java

public static boolean copyBackup(String fromFile, String toFile) throws IOException {

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

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

From source file:com.jivesoftware.os.jive.utils.shell.utils.Unzip.java

public static File unGzip(boolean verbose, File outputDir, String outName, File inputFile,
        boolean deleteOriginal) throws FileNotFoundException, IOException {
    String inFilePath = inputFile.getAbsolutePath();
    if (verbose) {
        System.out.println("unzipping " + inFilePath);
    }//  w w w. j av a 2  s . c  o  m
    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath));

    File outFile = new File(outputDir, outName);
    outFile.getParentFile().mkdirs();
    String outFilePath = outFile.getAbsolutePath();
    OutputStream out = new FileOutputStream(outFilePath);

    byte[] buf = new byte[1024];
    int len;
    while ((len = gzipInputStream.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    gzipInputStream.close();
    out.close();

    if (deleteOriginal) {
        FileUtils.forceDelete(inputFile);
        if (verbose) {
            System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath()));
        }
    }
    if (verbose) {
        System.out.println("unzipped " + inFilePath);
    }
    return new File(outFilePath);
}

From source file:Main.java

public static void streamCopy(InputStream is, OutputStream os) throws IOException {
    if (is == null || os == null) {
        return;/* w w  w  .j av  a  2  s .co m*/
    }

    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) > 0) {
        os.write(buffer, 0, len);
    }
}