Java ByteArrayOutputStream Write loadIntoMemory(InputStream is)

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

Description

Writes all contents of the given InputStream to a byte array.

License

Open Source License

Declaration

public static byte[] loadIntoMemory(InputStream is) throws IOException 

Method Source Code

//package com.java2s;
/* //  ww w  .j  a  va 2s .c  om
 GeoGebra - Dynamic Mathematics for Everyone
 http://www.geogebra.org
    
 This file is part of GeoGebra.
    
 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.
     
 */

import java.io.ByteArrayOutputStream;

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

public class Main {
    /**
     *  Writes all contents of the given InputStream to a byte array.
     */
    public static byte[] loadIntoMemory(InputStream is) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        copyStream(is, bos);
        bos.close();
        return bos.toByteArray();
    }

    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > -1) {
            out.write(buf, 0, len);
        }
    }
}

Related

  1. loadAsciiTxt()
  2. loadAsText(Class cls, String name)
  3. loadBinFile(java.io.File f)
  4. loadFully(final InputStream aStream)
  5. loadIfileAsBytes(final IFile ifile)
  6. loadProbe(InputStream in, int buffSize)
  7. loadStream(InputStream in, String encoding)
  8. readAll(File file)
  9. readAll(InputStream aInputStream)