Java Dump Stream dumpStreamToFile(InputStream is, String filename)

Here you can find the source of dumpStreamToFile(InputStream is, String filename)

Description

Dump the input stream to the specified file.

License

Academic Free License

Parameter

Parameter Description
is a parameter
filename a parameter

Declaration

public static void dumpStreamToFile(InputStream is, String filename) 

Method Source Code

//package com.java2s;
//License from project: Academic Free License 

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class Main {
    /**/*from   w  w w.j a  va  2s  .co m*/
     * Dump the input stream to the specified file.
     * 
     * @param is
     * @param filename
     */
    public static void dumpStreamToFile(InputStream is, String filename) {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(filename);
            dumpStreamToStream(is, fos);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            close(fos);
        }
    }

    /**
     * Dump the input stream to the output stream.
     * 
     * @param is
     * @param os
     */
    public static void dumpStreamToStream(InputStream is, OutputStream os) {
        byte ba[] = new byte[2048];
        int numRead = 0;
        try {
            numRead = is.read(ba);
            while (numRead > 0) {
                os.write(ba, 0, numRead);
                numRead = is.read(ba);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (os != null)
                try {
                    os.flush();
                } catch (Exception ex) {
                }
            ;
        }
    }

    /**
     * Close the output stream and trap any exceptions.
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ex) {
            }
        }
    }

    public static void close(Writer fw) {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception ex) {
            }
            ;
        }
    }

    /**
     * Close the input stream and trap any exceptions.
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ex) {
            }
        }
    }

    /**
     * Close the reader.
     * 
     * @param r
     */
    public static void close(Reader r) {
        if (r != null) {
            try {
                r.close();
            } catch (Exception ex) {

            }
        }
    }
}

Related

  1. dumpInputStreamIntoString(InputStream f, String encoding)
  2. dumpMap(PrintStream out, Map Values)
  3. dumpStream(InputStream in, PrintStream dump, boolean closeIn)
  4. dumpStream(InputStream in, String file)
  5. dumpStreamAndReOffer(InputStream is)
  6. dumpStreamToStream(InputStream is, OutputStream os)
  7. dumpThreads(PrintStream ps)
  8. dumpToFile(Object obj, OutputStream os)