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  ww  w  . ja  v a2 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.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

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

public class Main {
    public static final int BUFFER_SIZE = 1024 * 8;

    /**
     * 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;
        }
    }

    /**
     * read string.
     * 
     * @param reader
     *            Reader instance.
     * @return String.
     * @throws IOException
     */
    public static String read(Reader reader) throws IOException {
        StringWriter writer = new StringWriter();
        try {
            write(reader, writer);
            return writer.getBuffer().toString();
        } finally {
            writer.close();
        }
    }

    /**
     * write.
     * 
     * @param is
     *            InputStream instance.
     * @param os
     *            OutputStream instance.
     * @return count.
     * @throws IOException.
     */
    public static long write(InputStream is, OutputStream os) throws IOException {
        return write(is, os, BUFFER_SIZE);
    }

    /**
     * write.
     * 
     * @param is
     *            InputStream instance.
     * @param os
     *            OutputStream instance.
     * @param bufferSize
     *            buffer size.
     * @return count.
     * @throws IOException.
     */
    public static long write(InputStream is, OutputStream os, int bufferSize) throws IOException {
        int read;
        long total = 0;
        byte[] buff = new byte[bufferSize];
        while (is.available() > 0) {
            read = is.read(buff, 0, buff.length);
            if (read > 0) {
                os.write(buff, 0, read);
                total += read;
            }
        }
        return total;
    }

    /**
     * write.
     * 
     * @param reader
     *            Reader.
     * @param writer
     *            Writer.
     * @return count.
     * @throws IOException
     */
    public static long write(Reader reader, Writer writer) throws IOException {
        return write(reader, writer, BUFFER_SIZE);
    }

    /**
     * write.
     * 
     * @param reader
     *            Reader.
     * @param writer
     *            Writer.
     * @param bufferSize
     *            buffer size.
     * @return count.
     * @throws IOException
     */
    public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
        int read;
        long total = 0;
        char[] buf = new char[BUFFER_SIZE];
        while ((read = reader.read(buf)) != -1) {
            writer.write(buf, 0, read);
            total += read;
        }
        return total;
    }

    /**
     * write string.
     * 
     * @param writer
     *            Writer instance.
     * @param string
     *            String.
     * @throws IOException
     */
    public static long write(Writer writer, String string) throws IOException {
        Reader reader = new StringReader(string);
        try {
            return write(reader, writer);
        } finally {
            reader.close();
        }
    }
}

Related

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