Java InputStream Read Bytes readBytes(InputStream in, byte[] b)

Here you can find the source of readBytes(InputStream in, byte[] b)

Description

Fill array from stream

License

Apache License

Parameter

Parameter Description
in input stream
b bytes

Exception

Parameter Description

Return

count bytes reads from stream

Declaration

public static int readBytes(InputStream in, byte[] b) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w ww. ja  v a2s .  c o m
 * Created on 01.02.2005
 *
 * JDBReport Generator
 * 
 * Copyright (C) 2005-2014 Andrey Kholmanskih
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

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

public class Main {
    /**
     * Fill array from stream
     *
     * @param in input stream
     * @param b bytes
     * @return count bytes reads from stream
     * @throws java.io.IOException
     */
    public static int readBytes(InputStream in, byte[] b) throws IOException {
        int l = b.length;
        int pos = 0;
        int n;
        do {
            n = in.read(b, pos, l);
            if (n > 0) {
                pos += n;
                l -= n;
            }
        } while (n > 0 && l > 0);
        return pos;
    }
}

Related

  1. readBytes(InputStream in)
  2. readBytes(InputStream in, boolean forceClose)
  3. readBytes(InputStream in, byte[] buf, int length)
  4. readBytes(InputStream in, byte[] buffer)
  5. readBytes(InputStream in, byte[] buffer, int maxBytesAtOnce)
  6. readBytes(InputStream in, byte[] data, int offset, int length)