Java Dump Stream dumpStreamAndReOffer(InputStream is)

Here you can find the source of dumpStreamAndReOffer(InputStream is)

Description

dump Stream And Re Offer

License

Open Source License

Declaration

public static InputStream dumpStreamAndReOffer(InputStream is)
            throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Gijs de Vries aka Janoz.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from   ww w .ja v a  2s .  co m
 * Contributors:
 *     Gijs de Vries aka Janoz - initial API and implementation
 ******************************************************************************/

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    private static final int BUFF_SIZE = 1024;

    public static InputStream dumpStreamAndReOffer(InputStream is)
            throws IOException {
        FileOutputStream fos = null;
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            copyStream(is, os);
            byte[] data = os.toByteArray();
            fos = new FileOutputStream(File.createTempFile("dump", ".bin",
                    new File(".")));
            fos.write(data);
            return new ByteArrayInputStream(data);
        } finally {
            if (fos != null) {
                fos.close();
            }
        }

    }

    private static void copyStream(InputStream in, OutputStream out)
            throws IOException {
        byte[] buffer = new byte[BUFF_SIZE];
        int len;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }
}

Related

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