Java Byte Array from getBytesClassic(final int count, final InputStream is)

Here you can find the source of getBytesClassic(final int count, final InputStream is)

Description

Uses "old" IO's InputStream to read up exactly count bytes from provided input stream.

License

Open Source License

Parameter

Parameter Description
count the count of bytes to read from stream.
is the stream to read from.

Exception

Parameter Description
IOException in case of IO problem.

Return

array of bytes having exactly count length or null if not able to read up count bytes.

Declaration

public static byte[] getBytesClassic(final int count, final InputStream is) throws IOException 

Method Source Code

//package com.java2s;
/*//w w  w . j  a  va2s.co m
 * Sonatype Nexus (TM) Open Source Version
 * Copyright (c) 2007-2012 Sonatype, Inc.
 * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
 * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
 * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
 * Eclipse Foundation. All other trademarks are the property of their respective owners.
 */

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

public class Main {
    /**
     * Uses "old" IO's InputStream to read up exactly {@code count} bytes from provided input stream. If no IOException
     * occurred during read, but {@code count} bytes could not be read up, returns null. Does not closes the passed in
     * stream, just consumes provided count of bytes.
     * 
     * @param count the count of bytes to read from stream.
     * @param is the stream to read from.
     * @return array of bytes having exactly {@code count} length or null if not able to read up {@code count} bytes.
     * @throws IOException in case of IO problem.
     */
    public static byte[] getBytesClassic(final int count, final InputStream is) throws IOException {
        // Create the byte array to hold the data
        byte[] bytes = new byte[count];

        // Read in all the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        if (offset < bytes.length) {
            return null;
        }

        return bytes;
    }
}

Related

  1. getBytes2(InputStream aStream)
  2. getBytesAndClose(InputStream in)
  3. getBytesArrayFromFile(String fileName)
  4. getBytesASCII(String s)
  5. getBytesAsTempFile(byte[] bytes)
  6. getBytesFromArrayList(final ArrayList fileList)
  7. getBytesFromHexaText(String text)
  8. getBytesFromImage(File file)
  9. getBytesFromInt(int value)