Java File Read via ByteBuffer readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)

Here you can find the source of readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)

Description

Reads len bytes in a loop using the channel of the stream

License

Apache License

Parameter

Parameter Description
fileChannel a FileChannel to read len bytes into buf
buf The buffer to fill
off offset from the buffer
len the length of bytes to read

Exception

Parameter Description
IOException if it could not read requested number of bytes for any reason (including EOF)

Declaration

public static void readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w w  w  . ja v a 2  s  .  c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.IOException;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    /**
     * Reads len bytes in a loop using the channel of the stream
     * 
     * @param fileChannel
     *            a FileChannel to read len bytes into buf
     * @param buf
     *            The buffer to fill
     * @param off
     *            offset from the buffer
     * @param len
     *            the length of bytes to read
     * @throws IOException
     *             if it could not read requested number of bytes for any reason (including EOF)
     */
    public static void readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
            throws IOException {
        int toRead = len;
        ByteBuffer byteBuffer = ByteBuffer.wrap(buf, off, len);
        while (toRead > 0) {
            int ret = fileChannel.read(byteBuffer);
            if (ret < 0) {
                throw new IOException("Premeture EOF from inputStream");
            }
            toRead -= ret;
            off += ret;
        }
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFileAsByteArray(File file)
  4. readFileAsByteArray(String path)
  5. readFileAsStringArray(File file)
  6. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
  7. readFileDataIntoBufferBE(FileChannel fc, final int size)
  8. readFileFragment(FileChannel fc, long pos, int size)
  9. readFileHeader(FileInputStream fpi)