Java ByteArrayOutputStream Write load(InputStream is, boolean close)

Here you can find the source of load(InputStream is, boolean close)

Description

Copy the contents of is to the returned byte array.

License

Open Source License

Parameter

Parameter Description
is a parameter
close If true, is is closed after the copy.

Exception

Parameter Description
IOException an exception

Declaration

public static final byte[] load(InputStream is, boolean close) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  w  w.j  av  a2s  .co m
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.ByteArrayOutputStream;
import java.io.Closeable;

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

public class Main {
    /**
     * Copy the contents of is to the returned byte array.
     *
     * @param is
     * @param close If true, is is closed after the copy.
     * @throws IOException
     */
    public static final byte[] load(InputStream is, boolean close) throws IOException {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        copy(is, os, null, close);
        return os.toByteArray();
    }

    /**
     * Copy dest.length bytes from the inputstream into the dest bytearray.
     *
     * @param is
     * @param dest
     * @throws IOException
     */
    public static void copy(InputStream is, byte[] dest) throws IOException {
        int len = dest.length;
        int ofs = 0;
        while (len > 0) {
            int size = is.read(dest, ofs, len);
            ofs += size;
            len -= size;
        }
    }

    /**
     * Copy the contents of is to os.
     *
     * @param is
     * @param os
     * @param buf   Can be null
     * @param close If true, is is closed after the copy.
     * @throws IOException
     */
    public static final void copy(InputStream is, OutputStream os, byte[] buf, boolean close) throws IOException {
        try {
            int len;
            if (buf == null) {
                buf = new byte[4096];
            }
            while ((len = is.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
            os.flush();
        } finally {
            // in any case, we must close the stream if requested
            if (close) {
                is.close();
            }
        }
    }

    /**
     * close quietly a stream (no IOException thrown), which might be null
     *
     * @param closeable the stream to close, might be null
     * @return true if the stream was null or was closed properly
     */
    public static final boolean close(Closeable closeable) {
        boolean ok = false;

        try {
            if (closeable != null) {
                closeable.close();
            }

            ok = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ok;
    }
}

Related

  1. load(final File file, final String encoding)
  2. load(final String resource, final byte[] defaultResult)
  3. load(InputStream in, byte[] buffer, int offset, int initialBufferSize)
  4. load(InputStream inputStream, Class type)
  5. load(InputStream is)
  6. load(String fileName)
  7. loadAFileToString(File f)
  8. loadAsciiTxt()
  9. loadAsText(Class cls, String name)