Java File Read via ByteBuffer readInt(ReadableByteChannel channel)

Here you can find the source of readInt(ReadableByteChannel channel)

Description

Read an integer value from a channel.

License

Apache License

Parameter

Parameter Description
channel the channel to read from.

Exception

Parameter Description
IOException if an error occurs while reading the data.

Return

the value read from the channel.

Declaration

public static int readInt(ReadableByteChannel channel)
        throws IOException 

Method Source Code

//package com.java2s;
/*//from w  w w.  j  a  va 2 s  .  c  o  m
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * 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.
 */

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

public class Main {
    /**
     * Read an integer value from a channel.
     * @param channel the channel to read from.
     * @return the value read from the channel.
     * @throws IOException if an error occurs while reading the data.
     */
    public static int readInt(ReadableByteChannel channel)
            throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(4);
        int count = 0;
        while (count < 4) {
            int n = 0;
            while (n == 0)
                n = channel.read(buf);
            if (n < 0)
                throw new ClosedChannelException();
            count += n;
            /*
            count += channel.read(buf);
            if (count < 0) throw new ClosedChannelException();
             */
        }
        buf.flip();
        return buf.getInt();
    }

    /**
     * Deserialize an int value from an array of bytes.
     * @param data the array of bytes into which to serialize the value.
     * @param offset the position in the array of byte at which the serializatrion should start.
     * @return the int value read from the array of bytes
     */
    public static int readInt(byte[] data, int offset) {
        int pos = offset;
        int result = convertByte(data[pos++]) << 24;
        result += convertByte(data[pos++]) << 16;
        result += convertByte(data[pos++]) << 8;
        result += convertByte(data[pos++]) << 0;
        /*
        int result = data[pos++] << 24;
        result    += data[pos++] << 16;
        result    += data[pos++] <<  8;
        result    += data[pos++] <<  0;
         */
        return result;
    }

    /**
     * Deserialize an int value from a stream.
     * @param is the stream to read from.
     * @return the int value read from the stream.
     * @throws IOException if an error occurs while reading the data.
     */
    public static int readInt(InputStream is) throws IOException {
        int result = convertByte(is.read()) << 24;
        result += convertByte(is.read()) << 16;
        result += convertByte(is.read()) << 8;
        result += convertByte(is.read()) << 0;
        /*
        int result = is.read() << 24;
        result    += is.read() << 16;
        result    += is.read() <<  8;
        result    += is.read() <<  0;
         */
        return result;
    }

    /**
     * Convert a byte value into an unsigned int value.
     * @param b the value to convert.
     * @return the unsigned int result.
     */
    private static int convertByte(int b) {
        return b < 0 ? b + 256 : b;
    }
}

Related

  1. readInt(byte[] in)
  2. readInt(ByteArrayInputStream bin)
  3. readInt(InputStream in, ByteOrder order)
  4. readInt(InputStream input)
  5. readInt(InputStream inputStream)
  6. readInt(ReadableByteChannel channel)
  7. readInt16(DataInput di)
  8. readIntArray(DataInput input)
  9. readIntLittleEndian(InputStream in)