Java InputStream Read All readAll(InputStream i, byte b[])

Here you can find the source of readAll(InputStream i, byte b[])

Description

Reads until the array is full or until the stream ends

License

Open Source License

Parameter

Parameter Description
b the buffer into which the data is read.

Return

the amount read

Declaration

public static final int readAll(InputStream i, byte b[]) throws IOException 

Method Source Code

//package com.java2s;
/*//  w w  w  .j a  v a2  s  . co  m
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

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

public class Main {
    /**
     * Reads until the array is full or until the stream ends
     *
     * @param      b     the buffer into which the data is read.
     * @return     the amount read
     * @exception  IOException   the stream has been closed and the contained
     *          input stream does not support reading after close, or
     *          another I/O error occurs.
     */
    public static final int readAll(InputStream i, byte b[]) throws IOException {
        int len = b.length;
        int n = 0;
        while (n < len) {
            int count = i.read(b, n, len - n);
            if (count < 0) {
                return n;
            }
            n += count;
        }
        return n;
    }
}

Related

  1. readAll(final InputStream is)
  2. readAll(InputStream in)
  3. readall(InputStream in)
  4. readAll(InputStream in)
  5. readAll(InputStream in)