Java InputStream Read All readAll(InputStream in, byte[] buffer, int off, int len)

Here you can find the source of readAll(InputStream in, byte[] buffer, int off, int len)

Description

Continue reading from a stream until EOF is reached, or the requested number of bytes is read.

License

Open Source License

Parameter

Parameter Description
in the stream to read from
buffer the buffer to fill
off offset within the buffer to begin reading into
len number of bytes to read

Exception

Parameter Description
IOException if an IOException occurred while reading from the stream

Return

number of bytes actually read (will only be less than the requested number at EOF)

Declaration

public static int readAll(InputStream in, byte[] buffer, int off, int len) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  w  w  .j  av a2  s.c om
 * Copyright (C) 2005-2007 Robey Pointer <robey@lag.net>
 *
 * This file is part of jaramiko.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

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

public class Main {
    /**
     * Continue reading from a stream until EOF is reached, or the requested
     * number of bytes is read.
     * 
     * @param in
     *            the stream to read from
     * @param buffer
     *            the buffer to fill
     * @param off
     *            offset within the buffer to begin reading into
     * @param len
     *            number of bytes to read
     * @return number of bytes actually read (will only be less than the
     *         requested number at EOF)
     * @throws IOException
     *             if an IOException occurred while reading from the stream
     */
    public static int readAll(InputStream in, byte[] buffer, int off, int len) throws IOException {
        int soFar = 0;
        while (soFar < len) {
            int n = in.read(buffer, off + soFar, len - soFar);
            if (n < 0) {
                return soFar;
            }
            soFar += n;
        }
        return len;
    }

    public static int readAll(InputStream in, byte[] buffer) throws IOException {
        return readAll(in, buffer, 0, buffer.length);
    }
}

Related

  1. readall(InputStream in)
  2. readAll(InputStream in)
  3. readAll(InputStream in)
  4. readAll(InputStream in)
  5. readAll(InputStream in)
  6. readall(InputStream in, byte[] buffer, int offset, int len)
  7. readAll(InputStream inputStream)
  8. readAll(InputStream is)
  9. readAll(InputStream is, byte[] buffer, int offset, int length)