Java InputStream Read Long readLong(InputStream inputStream)

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

Description

read Long

License

Apache License

Declaration

public static long readLong(InputStream inputStream) throws IOException 

Method Source Code

//package com.java2s;
/** Copyright 2013 BlackBerry, Inc.
 *
 *  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. /*  www  .j a va  2  s . com*/
 */

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

public class Main {
    public static long readLong(InputStream inputStream) throws IOException {
        long value = 0L;
        int i = 0;
        long b;
        while (((b = inputStream.read()) & 0x80L) != 0) {
            value |= (b & 0x7F) << i;
            i += 7;
            if (i >= 7 * 10) {
                throw new IOException("Didn't reach the end of the long varint after 10 bytes.");
            }
        }
        value |= b << i;

        // un-zig-zag it
        long temp = (((value << 63) >> 63) ^ value) >> 1;
        // since we lost that first bit during all that zigging and zagging,
        // make sure it's the right one now
        value = temp ^ (value & (1L << 63));

        return value;
    }
}

Related

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