Java InputStream Read Long readLong(InputStream stream)

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

Description

Reading long from stream

License

Open Source License

Parameter

Parameter Description
stream source stream

Exception

Parameter Description
IOException reading exception

Return

value

Declaration

public static long readLong(InputStream stream) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from  w  w w.ja v a  2s . c om
     * Reading long from stream
     *
     * @param stream source stream
     * @return value
     * @throws IOException reading exception
     */
    public static long readLong(InputStream stream) throws IOException {
        long a = readUInt(stream);
        long b = readUInt(stream);

        return a + (b << 32);
    }

    /**
     * Reading long value from bytes array
     *
     * @param src    source bytes
     * @param offset offset in array
     * @return long value
     */
    public static long readLong(byte[] src, int offset) {
        long a = readUInt(src, offset);
        long b = readUInt(src, offset + 4);

        return (a & 0xFFFFFFFF) + ((b & 0xFFFFFFFF) << 32);
    }

    /**
     * Reading uint from stream
     *
     * @param stream source stream
     * @return value
     * @throws IOException reading exception
     */
    public static long readUInt(InputStream stream) throws IOException {
        long a = stream.read();
        if (a < 0) {
            throw new IOException();
        }
        long b = stream.read();
        if (b < 0) {
            throw new IOException();
        }
        long c = stream.read();
        if (c < 0) {
            throw new IOException();
        }
        long d = stream.read();
        if (d < 0) {
            throw new IOException();
        }

        return a + (b << 8) + (c << 16) + (d << 24);
    }

    /**
     * Reading uint from bytes array
     *
     * @param src source bytes
     * @return uint value
     */
    public static long readUInt(byte[] src) {
        return readUInt(src, 0);
    }

    /**
     * Reading uint from bytes array
     *
     * @param src    source bytes
     * @param offset offset in array
     * @return uint value
     */
    public static long readUInt(byte[] src, int offset) {
        long a = src[offset + 0] & 0xFF;
        long b = src[offset + 1] & 0xFF;
        long c = src[offset + 2] & 0xFF;
        long d = src[offset + 3] & 0xFF;

        return a + (b << 8) + (c << 16) + (d << 24);
    }
}

Related

  1. readLong(InputStream is)
  2. readLong(InputStream is)
  3. readLong(InputStream is)
  4. readLong(InputStream stream)
  5. readLong(InputStream stream)
  6. readLong(InputStream stream)
  7. readLongFrom(InputStream is)
  8. readLongLE(InputStream in)
  9. readLongLE(InputStream in)