Java InputStream Read Long readLong(InputStream is)

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

Description

read Long

License

Apache License

Declaration

public static long readLong(InputStream is) throws IOException 

Method Source Code


//package com.java2s;
/*// w  w w . j ava 2s  .c o m
 * Copyright 2016 Dmitriy Malakhov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    public static long readLong(InputStream is) throws IOException {
        byte[] b = new byte[8];
        read(is, b);
        return toLong(b);
    }

    public static void read(InputStream is, byte[] bytes) throws IOException {
        int start = 0;
        do {
            int count = is.read(bytes, start, bytes.length - start);
            start += count;
        } while (start < bytes.length);
    }

    public static long toLong(byte[] b) {
        long MASK = 0xFF;
        long result = 0;
        result = b[7] & MASK;
        result = result + ((b[6] & MASK) << 8);
        result = result + ((b[5] & MASK) << 16);
        result = result + ((b[4] & MASK) << 24);
        result = result + ((b[3] & MASK) << 32);
        result = result + ((b[2] & MASK) << 40);
        result = result + ((b[1] & MASK) << 48);
        result = result + ((b[0] & MASK) << 56);
        return result;
    }
}

Related

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