Java File Read via ByteBuffer readUint32AsInt(DataInput di)

Here you can find the source of readUint32AsInt(DataInput di)

Description

Overflow checking since java can't handle unsigned numbers.

License

Open Source License

Declaration

public static int readUint32AsInt(DataInput di) throws IOException 

Method Source Code


//package com.java2s;
/*//from www.  j  a v  a 2 s.c o  m
 * Entagged Audio Tag library
 * Copyright (c) 2003-2005 Rapha?l Slinckx <raphael@slinckx.net>
 * 
 * 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 (at your option) 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.*;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Overflow checking since java can't handle unsigned numbers.
     */
    public static int readUint32AsInt(DataInput di) throws IOException {
        final long l = readUint32(di);
        if (l > Integer.MAX_VALUE) {
            throw new IOException("uint32 value read overflows int");
        }
        return (int) l;
    }

    public static long readUint32(DataInput di) throws IOException {
        final byte[] buf8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        di.readFully(buf8, 4, 4);
        final long l = ByteBuffer.wrap(buf8).getLong();
        return l;
    }
}

Related

  1. readToBuffer(final String filename)
  2. readToByteArray(String fname)
  3. readTwoByteInt(byte[] array, int start)
  4. readTxtFromFile(File file)
  5. readUint32(final DataInput di)
  6. readUintLE(byte[] bytes, int pointer, int size)
  7. readUnsignedInt(InputStream is)
  8. readUnsignedInt24(InputStream is)
  9. readUtf8(DataInput in)