Java InputStream to Byte Array fread(short[] data, int length, InputStream fp)

Here you can find the source of fread(short[] data, int length, InputStream fp)

Description

Read data from fp.

License

LGPL

Parameter

Parameter Description
data a parameter
length a parameter
fp a parameter

Exception

Parameter Description

Return

length of resulting data array

Declaration

static int fread(short[] data, int length, InputStream fp) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w  w w .  j  a  v  a  2 s  .  c  o m*/
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.io.*;

public class Main {
    /**
     * Read <code>data</code> from <code>fp</code>.
     *
     * @param data
     * @param length
     * @param fp
     * @return length of resulting data array
     * @throws java.io.IOException
     */
    static int fread(short[] data, int length, InputStream fp) throws IOException {
        byte[] bytes = new byte[2];
        int readLength = 0;

        for (int i = 0; i < length; i++) {
            if (fp.read(bytes) != 2)
                break;
            data[i] = (short) ((bytes[1] << 8) | (bytes[0] & 0x00FF));
            readLength++;
        }
        return readLength;
    }
}

Related

  1. getBytes(InputStream attachment)
  2. getBytes(InputStream fis)
  3. getBytes(InputStream in)
  4. getBytes(InputStream in)