Java InputStream Read Long readLong(InputStream in)

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

Description

Reads a long from in.

License

Open Source License

Declaration

public static long readLong(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /** Reads a long from in.  Input: there must at least eight bytes
    in in, otherwise if there are no bytes, an IOException is thrown
    (to be used as an end of stream test), but if there are not
    enough bytes, a runtime error is thrown.  Output: The long
    corresponding to the next eight bytes.  
    @see byteArrayToLong *//*from w ww .j a  v  a  2s . c om*/
    public static long readLong(InputStream in) throws IOException {
        byte b[] = new byte[8];
        int cnt = read(in, b);

        if (cnt < 8) {
            if (cnt <= 0) {
                throw new IOException("readLong: the imput stream is empty.");
            } else {
                throw new IOException("readLong: not enought bytes.");
            }
        }
        return byteArrayToLong(b);
    }

    /** Reads len bytes from in and places in b starting at off and
    returns len.  Input: in should have at least len bytes; if not,
    then -1 is returned if no bytes are available (end of stream) or
    all of the remaining bytes are placed in b, starting at off and
    this number is returned.  Output: The number of bytes read is
    returned or -1 is the stream is empty.  This should be used
    instead of the read provided by any of the InputStreams; this is
    because for such streams (e.g. BufferedInputStream and
    GZIPOutputStream) if you use the given read routines to read in
    x bytes, but the buffer has some, but not enough bytes, then you
    don't get them all; you just get what the buffer has. This
    version fixes that problem. */
    public static int read(InputStream in, byte[] b, int off, int len) throws IOException {
        int cnt = 0;
        while (cnt < len) {
            int readOnce = in.read(b, off + cnt, len - cnt);
            if (readOnce <= 0)
                break;
            cnt += readOnce;
        }
        return cnt;
    }

    public static int read(InputStream in, byte[] b) throws IOException {
        return read(in, b, 0, b.length);
    }

    public static long byteArrayToLong(byte[] b) {
        long i0 = (b[0] & 0xFF) << 56;
        long i1 = (b[1] & 0xFF) << 48;
        long i2 = (b[2] & 0xFF) << 40;
        long i3 = (b[3] & 0xFF) << 32;
        long i4 = (b[4] & 0xFF) << 24;
        long i5 = (b[5] & 0xFF) << 16;
        long i6 = (b[6] & 0xFF) << 8;
        long i7 = (b[7] & 0xFF);
        return (i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7);
    }
}

Related

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