Java InputStream Read Bytes readBytes(InputStream ins, byte[] buf, int size)

Here you can find the source of readBytes(InputStream ins, byte[] buf, int size)

Description

Read size bytes from stream into buf.

License

Open Source License

Parameter

Parameter Description
ins stream to read from
buf buffer to read into
size number of bytes to read

Exception

Parameter Description
IOException an exception

Return

number of bytes read, which will be less than size iff EOF is reached

Declaration

public static int readBytes(InputStream ins, byte[] buf, int size) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   www. j  a v a2  s .c  om*/
    
Copyright (c) 2000-2003 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.
    
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
STANFORD UNIVERSITY 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.
    
Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.
    
*/

import java.io.*;

public class Main {
    /** Read size bytes from stream into buf.  Keeps trying to read until
     * enough bytes have been read or EOF or error.
     * @param ins stream to read from
     * @param buf buffer to read into
     * @param size number of bytes to read
     * @return number of bytes read, which will be less than size iff EOF is
     * reached
     * @throws IOException
     */
    public static int readBytes(InputStream ins, byte[] buf, int size) throws IOException {
        int off = 0;
        while (off < size) {
            int nread = ins.read(buf, off, size - off);
            if (nread == -1) {
                return off;
            }
            off += nread;
        }
        return off;
    }
}

Related

  1. readBytes(InputStream inputStream, byte[] buffer, int offset, int length)
  2. readBytes(InputStream inputStream, int bufSize)
  3. readBytes(InputStream inputStream, int length)
  4. readBytes(InputStream inputStream, int numberOfBytes)
  5. readBytes(InputStream inputStream, int numberOfBytes)
  6. readBytes(InputStream is)
  7. readBytes(InputStream is)
  8. readBytes(InputStream is)
  9. readBytes(InputStream is)