Java DataInput Read Long readLongSequence(DataInput in)

Here you can find the source of readLongSequence(DataInput in)

Description

read Long Sequence

License

LGPL

Declaration

public static long[] readLongSequence(DataInput in) throws Exception 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.*;

public class Main {
    public static long[] readLongSequence(DataInput in) throws Exception {
        byte len = in.readByte();
        if (len == 0)
            return new long[] { 0, 0 };

        byte[] lengths = decodeLength(len);
        long[] seqnos = new long[2];
        byte[] buf = new byte[lengths[0] + lengths[1]];
        in.readFully(buf, 0, buf.length);
        seqnos[0] = makeLong(buf, 0, lengths[0]);
        seqnos[1] = makeLong(buf, lengths[0], lengths[1]) + seqnos[0];
        return seqnos;
    }/*from   ww w  .j  a  va2s . com*/

    public static byte[] decodeLength(byte len) {
        byte[] retval = { (byte) 0, (byte) 0 };
        retval[0] = (byte) ((len & 0xff) >> 4);
        retval[1] = (byte) (len & ~0xf0); // 0xff is the first nibble set (11110000)
        // retval[1]=(byte)(len << 4);
        // retval[1]=(byte)((retval[1] & 0xff) >> 4);
        return retval;
    }

    static long makeLong(byte[] buf, int offset, int len) {
        long retval = 0;
        for (int i = 0; i < len; i++) {
            byte b = buf[offset + i];
            retval |= ((long) b & 0xff) << (i * 8);
        }
        return retval;
    }
}

Related

  1. readLong(DataInput dataInput)
  2. readLong(DataInput in)
  3. readLong(DataInput in)
  4. readLongUTF8(DataInput in)