Java File Read via ByteBuffer readLong(InputStream is, ByteOrder bo)

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

Description

Read a single long from the InputStream using given ByteOrder pointer respectively.

License

Open Source License

Parameter

Parameter Description
is a parameter
bo a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static long readLong(InputStream is, ByteOrder bo) throws IOException 

Method Source Code

//package com.java2s;
/*//w w w . j a v a2  s.  c o  m
   Copyright (c) 2009-2011
  Speech Group at Informatik 5, Univ. Erlangen-Nuremberg, GERMANY
  Korbinian Riedhammer
  Tobias Bocklet
    
   This file is part of the Java Speech Toolkit (JSTK).
    
   The JSTK is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   The JSTK 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 General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with the JSTK. If not, see <http://www.gnu.org/licenses/>.
*/

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

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**
     * Read a single long from the InputStream using given ByteOrder
     * pointer respectively.
     * @param is
     * @param bo
     * @return
     * @throws IOException
     */
    public static long readLong(InputStream is, ByteOrder bo) throws IOException {
        byte[] bbuf = new byte[Long.SIZE / 8];
        int read = is.read(bbuf);

        if (read < bbuf.length)
            throw new IOException("could not read required bytes");

        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

        return bb.getLong();
    }

    /**
     * Read a long array from the InputStream using given ByteOrder
     * pointer respectively.
     * @param is
     * @param buf
     * @param bo
     * @return false if frame could not be filled
     * @throws IOException
     */
    public static boolean readLong(InputStream is, long[] buf, ByteOrder bo) throws IOException {
        byte[] bbuf = new byte[buf.length * Long.SIZE / 8];
        int read = is.read(bbuf);

        if (read < bbuf.length)
            return false;

        ByteBuffer bb = ByteBuffer.wrap(bbuf);
        bb.order(bo);

        for (int i = 0; i < buf.length; ++i)
            buf[i] = bb.getLong();

        return true;
    }
}

Related

  1. readJsonFromFile(String path)
  2. readLargeFile(File file)
  3. readLong(byte[] src, int pointer)
  4. readLong(InputStream input)
  5. readLong(InputStream is)
  6. readLong(RandomAccessFile raFile)
  7. readLTriad(byte[] triad)
  8. readNByte(ReadableByteChannel channel, int n)
  9. readNIOFile(String filePath)