Java InputStream Read Long readLongFrom(InputStream is)

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

Description

This method allows to restore a long from the input stream.

License

Open Source License

Parameter

Parameter Description
is the stream to read data from in order to restore the object

Return

the long

Declaration

public static long readLongFrom(InputStream is) throws IOException 

Method Source Code


//package com.java2s;
/*//from w ww  . j av a  2 s .  co  m
 * JORAM: Java(TM) Open Reliable Asynchronous Messaging
 * Copyright (C) 2006 - 2010 ScalAgent Distributed Technologies
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA.
 *
 * Initial developer(s): ScalAgent Distributed Technologies
 * Contributor(s): 
 */

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

public class Main {
    private static ThreadLocal perThreadBuffer = new ThreadLocal() {
        protected synchronized Object initialValue() {
            return new byte[8];
        }
    };

    /**
     * This method allows to restore a long from the input stream.
     *
     * @param is   the stream to read data from in order to restore the object
     * @return    the long 
     */
    public static long readLongFrom(InputStream is) throws IOException {
        byte[] buf = readFully(8, is);
        return ((buf[0] & 0xFFL) << 56) | ((buf[1] & 0xFFL) << 48) | ((buf[2] & 0xFFL) << 40)
                | ((buf[3] & 0xFFL) << 32) | ((buf[4] & 0xFFL) << 24) | ((buf[5] & 0xFFL) << 16)
                | ((buf[6] & 0xFFL) << 8) | (buf[7] & 0xFFL);
    }

    private static byte[] readFully(int length, InputStream is) throws IOException {
        int count = 0;
        byte[] buf = (byte[]) (perThreadBuffer.get());
        if (length > buf.length)
            buf = new byte[length];

        int nb = -1;
        do {
            nb = is.read(buf, count, length - count);
            if (nb < 0)
                throw new EOFException();
            count += nb;
        } while (count != length);
        return buf;
    }

    private static void readFully(byte[] buf, InputStream is) throws IOException {
        int count = 0;

        int nb = -1;
        do {
            nb = is.read(buf, count, buf.length - count);
            if (nb < 0)
                throw new EOFException();
            count += nb;
        } while (count != buf.length);
    }
}

Related

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