Java File Read via ByteBuffer readInt(InputStream in, ByteOrder order)

Here you can find the source of readInt(InputStream in, ByteOrder order)

Description

read Int

License

Open Source License

Declaration

public static int readInt(InputStream in, ByteOrder order)
            throws IOException 

Method Source Code

//package com.java2s;
/* JKTX//from w  w  w.  j a v a  2s. co  m
 * 
 * Copyright (c) 2011 Timon Bijlsma
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
    
 * This program 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 Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

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

public class Main {
    public static int readInt(InputStream in, ByteOrder order)
            throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.order(order);
        readFully(in, buf);
        return buf.getInt();
    }

    public static void readFully(InputStream in, ByteBuffer out)
            throws IOException {
        if (!out.hasRemaining()) {
            return;
        }

        int oldpos = out.position();
        try {
            if (out.hasArray()) {
                while (out.hasRemaining()) {
                    int r = in.read(out.array(),
                            out.arrayOffset() + out.position(),
                            out.remaining());
                    if (r < 0)
                        throw new EOFException();
                    out.position(out.position() + r);
                }
            } else {
                byte[] temp = new byte[Math.min(out.remaining(), 8192)];
                while (out.hasRemaining()) {
                    int r = in.read(temp, 0,
                            Math.min(temp.length, out.remaining()));
                    if (r < 0)
                        throw new EOFException();
                    out.put(temp, 0, r);
                }
            }
        } finally {
            out.position(oldpos);
        }
    }
}

Related

  1. readInputStreamToString(InputStream inputStream)
  2. readInt(byte[] bytes, int index)
  3. readInt(byte[] bytes, int offset, java.nio.ByteOrder byteorder)
  4. readInt(byte[] in)
  5. readInt(ByteArrayInputStream bin)
  6. readInt(InputStream input)
  7. readInt(InputStream inputStream)
  8. readInt(ReadableByteChannel channel)
  9. readInt(ReadableByteChannel channel)