Java Utililty Methods InputStream to OutputStream

List of utility methods to do InputStream to OutputStream

Description

The list of methods to do InputStream to OutputStream are organized into topic(s).

Method

voidcopyStreamSafely(InputStream in, ByteArrayOutputStream os)
Copies the content from in into os.
try {
    copyStreamUnsafelyUseWithCaution(in, os);
} finally {
    in.close();
FilecopyStreamToFile(final InputStream stream, final File output)
Helper function for writing a text stream to a file.
final BufferedReader r = new BufferedReader(new InputStreamReader(stream));
String l = null;
final PrintWriter writer = new PrintWriter(new FileWriter(output));
while ((l = r.readLine()) != null) {
    writer.println(l);
writer.close();
r.close();
...
FilecopyStreamToFile(InputStream input, String outputPath)
copy Stream To File
File file = new File(outputPath);
try {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    InputStream is = input;
    byte[] buf = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0) {
        out.write(buf, 0, len);
...
voidCopyStreamToFile(InputStream inputStream, File outputFile)
Copies a InputStream into a file.
InputStreamReader in = new InputStreamReader(inputStream);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1) {
    out.write(c);
in.close();
out.close();
...
FilecopyStreamToFile(InputStream inputStream, File outputFile)
copy Stream To File
OutputStream outputStream = new FileOutputStream(outputFile);
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = inputStream.read(buf)) > 0) {
    outputStream.write(buf, 0, len);
outputStream.close();
inputStream.close();
...
voidcopyStreamToFile(InputStream is, File outputFile)
Copy contents of a stream to a target file.
OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
copyStreamToStream(is, os);
os.close();
voidcopyStreamToFile(InputStream stream, String outputFilePath)
Uses the default charset
BufferedInputStream is = new BufferedInputStream(stream);
OutputStream os = null;
try {
    os = new FileOutputStream(new File(outputFilePath));
    byte[] b = new byte[1024];
    int read;
    while ((read = is.read(b)) != -1) {
        os.write(b, 0, read);
...
FilecopyStreamToFileBinary(final InputStream stream, final File output)
Helper function for writing a binary stream to a file.
final FileOutputStream out = new FileOutputStream(output);
final byte buf[] = new byte[1024];
int len = 0;
while ((len = stream.read(buf)) > 0)
    out.write(buf, 0, len);
out.close();
stream.close();
return output;
...
voidcopyStreamToStream(final InputStream input, final OutputStream output)
Copy any input stream to output stream.
InputStream is = null;
OutputStream os = null;
int ch;
try {
    if (input instanceof BufferedInputStream) {
        is = input;
    } else {
        is = new BufferedInputStream(input);
...
voidcopyStreamToStream(InputStream in, OutputStream out)
Copy the contents of an InputStream, up to EOF, to the given OutputStream.
byte[] buf = new byte[BUFFER_SIZE];
int len;
try {
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
} finally {
    in.close();
...