Java InputStream Read Long readLong(InputStream inputStream)

Here you can find the source of readLong(InputStream inputStream)

Description

Read in a long from an InputStream

License

Open Source License

Parameter

Parameter Description
inputStream The InputStream used to read the long

Exception

Parameter Description
IOException Thrown if there is a problem reading from the InputStream

Return

A long, which is the next 8 bytes converted from the InputStream

Declaration

public static long readLong(InputStream inputStream) throws IOException 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//  ww  w  .j av a2  s.  c o  m
     * Read in a long from an InputStream
     * 
     * @param inputStream
     *            The InputStream used to read the long
     * @return A long, which is the next 8 bytes converted from the InputStream
     * @throws IOException
     *             Thrown if there is a problem reading from the InputStream
     */
    public static long readLong(InputStream inputStream) throws IOException {
        byte[] byteArray = new byte[8];

        // Read in the next 8 bytes
        inputStream.read(byteArray);

        long number = convertLongFromBytes(byteArray);

        return number;
    }

    public static long convertLongFromBytes(byte[] bytes) {
        return convertLongFromBytes(bytes, 0);
    }

    public static long convertLongFromBytes(byte[] bytes, int offset) {
        // Convert it to an long
        return ((((long) bytes[offset + 7]) & 0xFF)
                + ((((long) bytes[offset + 6]) & 0xFF) << 8)
                + ((((long) bytes[offset + 5]) & 0xFF) << 16)
                + ((((long) bytes[offset + 4]) & 0xFF) << 24)
                + ((((long) bytes[offset + 3]) & 0xFF) << 32)
                + ((((long) bytes[offset + 2]) & 0xFF) << 40)
                + ((((long) bytes[offset + 1]) & 0xFF) << 48) + ((((long) bytes[offset + 0]) & 0xFF) << 56));
    }
}

Related

  1. readLong(InputStream in)
  2. readLong(InputStream in)
  3. readLong(InputStream in)
  4. readLong(InputStream input)
  5. readLong(InputStream inputStream)
  6. readLong(InputStream is)
  7. readLong(InputStream is)
  8. readLong(InputStream is)
  9. readLong(InputStream is)