Java Dump Stream dumpInputStream(InputStream instream)

Here you can find the source of dumpInputStream(InputStream instream)

Description

dump an InputStream, returning a newly created byte[] array

License

Open Source License

Declaration

public static byte[] dumpInputStream(InputStream instream) throws IOException 

Method Source Code

//package com.java2s;
/*************************************************************************
*                                                                        *
*  This file is part of the 20n/act project.                             *
*  20n/act enables DNA prediction for synthetic biology/bioengineering.  *
*  Copyright (C) 2017 20n Labs, Inc.                                     *
*                                                                        *
*  Please direct all queries to act@20n.com.                             *
*                                                                        *
*  This program is free software: you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation, either version 3 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
*  This program is distributed in the hope that it will be useful,       *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*  GNU General Public License for more details.                          *
*                                                                        *
*  You should have received a copy of the GNU General Public License     *
*  along with this program.  If not, see <http://www.gnu.org/licenses/>. *
*                                                                        *
*************************************************************************/

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*from  www . j  a v a 2s.  c  om*/
     * dump an InputStream, returning a newly created byte[] array
     */
    public static byte[] dumpInputStream(InputStream instream) throws IOException {
        int lastindex = 0;
        int increment;
        byte[] A = new byte[2048];
        byte[] B;
        do {
            B = new byte[2 * A.length];
            System.arraycopy(A, 0, B, 0, A.length);
            A = B;
            try {
                increment = instream.read(A, lastindex, A.length - lastindex);
            } catch (IOException e) {
                throw e;
            }
            if (increment == -1)
                /* EOF, no characters read */
                break;
            lastindex += increment;
        } while (lastindex >= A.length);

        /* copy A into B of precisely correct length */
        B = new byte[lastindex];
        System.arraycopy(A, 0, B, 0, B.length);
        return B;
    }
}

Related

  1. dumpEvent(String event, OutputStream stream)
  2. dumpFile(InputStream file, String header, String testName)
  3. dumpFile(JarInputStream jin, File targetFile)
  4. dumpFile(String r_file, OutputStream outputstream)
  5. dumpInputOutputStream(InputStream is, OutputStream os)
  6. dumpInputStream(InputStream is, PrintStream p)
  7. dumpInputStream(InputStream stream)
  8. dumpInputStreamAsString(InputStream is)
  9. dumpInputStreamIntoString(InputStream f, String encoding)