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

/**
 * Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when
 * finished.//w  w w  .ja v a 2 s .  c  o  m
 * 
 * @param content the content to write to the stream; may be null
 * @param stream the stream to which the content is to be written
 * @throws IOException
 * @throws IllegalArgumentException if the stream is null
 */
public static void write(String content, OutputStream stream) throws IOException {
    boolean error = false;
    try {
        if (content != null) {
            byte[] bytes = content.getBytes();
            stream.write(bytes, 0, bytes.length);
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing stream
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing stream
        throw e;
    } finally {
        try {
            stream.flush();
        } catch (IOException e) {
            if (!error)
                throw e;
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                if (!error)
                    throw e;
            }
        }
    }
}

From source file:Main.java

private static File createFileFromInputStream(InputStream inputStream, String name) {

    try {/*from w w  w  . j av a  2 s .com*/
        File f = File.createTempFile("font", null);
        OutputStream outputStream = new FileOutputStream(f);
        byte buffer[] = new byte[1024];
        int length = 0;

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

        outputStream.close();
        inputStream.close();
        return f;
    } catch (Exception e) {
        // Logging exception
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

static public boolean copyFileTo(Context c, String orifile, String desfile) throws IOException {
    InputStream myInput;//from   w ww.jav  a  2  s .  com
    OutputStream myOutput = new FileOutputStream(desfile);
    myInput = c.getAssets().open(orifile);
    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();

    return true;
}

From source file:Main.java

public static void DownloadFromUrl(String thisUrl, String path, String filename) {
    Log.d("DownloadFromUrl", "url: " + thisUrl);
    Log.d("DownloadFromUrl", "path: " + path);
    Log.d("DownloadFromUrl", "filename: " + filename);
    try {/*from w  w w.  j  ava  2 s . c  o  m*/
        URL url = new URL(thisUrl);
        new File(path).mkdirs();
        File file = new File(path, filename);

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        Log.d("DownloadFromUrl", "about to write file");
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        try {
            OutputStream os = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[4096];
                for (int n; (n = is.read(buffer)) != -1;)
                    os.write(buffer, 0, n);
            } finally {
                os.close();
            }
        } finally {
            is.close();
        }
        Log.d("DownloadFromUrl", "finished");

    } catch (IOException e) {
        Log.d("DownloadFromUrl", "Error: " + e);
    }
}

From source file:GoogleImages.java

public static void saveImage(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;/*from   w ww.j  av a2s .c  o  m*/

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

From source file:Main.java

/**
 * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
 * <code>OutputStream</code>.
 * <p>/*from  w  w  w . jav  a2 s .co  m*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input
 *            the <code>InputStream</code> to read from
 * @param output
 *            the <code>OutputStream</code> to write to
 * @return the number of bytes copied
 * @throws NullPointerException
 *             if the input or output is null
 * @throws IOException
 *             if an I/O error occurs
 */
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

/**
 * Copy the contents of an InputStream into an OutputStream.
 * /*from www.  j a  v a 2 s .c  o m*/
 * @param in
 * @param out
 * @return
 * @throws IOException
 */
public static int copy(final InputStream in, final OutputStream out) throws IOException {
    final int BUFFER_LENGTH = 1024;
    final byte[] buffer = new byte[BUFFER_LENGTH];
    int totalRead = 0;
    int read = 0;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
        totalRead += read;
    }
    return totalRead;
}

From source file:colt.nicity.performance.latent.http.ApacheHttpClient.java

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

From source file:com.varaneckas.hawkscope.util.IOUtils.java

/**
 * Copies a file/*from  w  w  w  . j  a v  a  2 s. com*/
 * 
 * @param in source file
 * @param out target file
 * @return boolean on success
 */
public static synchronized boolean copyFile(final InputStream in, final OutputStream out) {
    try {
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        return true;
    } catch (final Exception e) {
        log.error("Failed copying file: " + in + " -> " + out, e);
        return false;
    }
}

From source file:io.cloudslang.content.vmware.utils.OvfUtils.java

public static long writeToStream(OutputStream outputStream, ProgressUpdater progressUpdater, long bytesCopied,
        byte[] buffer, int read) throws Exception {
    outputStream.write(buffer, 0, read);
    outputStream.flush();//from w ww. j a  va 2s .c  o m
    bytesCopied += read;
    progressUpdater.updateBytesSent(read);
    return bytesCopied;
}