Java File Read via ByteBuffer read(File file)

Here you can find the source of read(File file)

Description

read

License

Open Source License

Declaration

public static String read(File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   ww  w  .ja  v  a 2s .c  o m*/
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * 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.BufferedReader;
import java.io.File;

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

import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;

import java.nio.file.Path;

import java.nio.file.StandardOpenOption;

import java.util.EnumSet;

public class Main {
    private static final Charset _UTF_8 = Charset.forName("UTF-8");
    private static final EnumSet<StandardOpenOption> _readOptions = EnumSet.of(StandardOpenOption.READ);
    private static final EnumSet<StandardOpenOption> _writeOptions = EnumSet.of(StandardOpenOption.WRITE,
            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

    public static String read(File file) throws IOException {
        return _collect(file.toPath(), _UTF_8);
    }

    private static String _collect(Path path, Charset encoding) throws IOException {
        return _collect(_reader(path, encoding));
    }

    private static String _collect(Reader reader) throws IOException {
        StringWriter stringWriter = new StringWriter();

        _copy(reader, stringWriter);

        return stringWriter.toString();
    }

    private static BufferedReader _reader(Path path, Charset encoding) throws IOException {
        return _reader(_readChannel(path), encoding);
    }

    private static BufferedReader _reader(ReadableByteChannel in, Charset encoding) throws IOException {
        return new BufferedReader(Channels.newReader(in, encoding.newDecoder(), -1));
    }

    private static Path _copy(InputStream in, Path path) throws IOException {
        try (FileChannel out = _writeChannel(path)) {
            _copy(in, out);
        }

        return path;
    }

    private static WritableByteChannel _copy(InputStream inputStream, WritableByteChannel writableByteChannel)
            throws IOException {

        try {
            ByteBuffer byteBuffer = ByteBuffer.allocate(4096 * 16);

            byte[] buffer = byteBuffer.array();

            for (int size; (size = inputStream.read(buffer, byteBuffer.position(), byteBuffer.remaining())) > 0;) {
                byteBuffer.position(byteBuffer.position() + size);
                byteBuffer.flip();

                writableByteChannel.write(byteBuffer);

                byteBuffer.compact();
            }

            for (byteBuffer.flip(); byteBuffer.hasRemaining();) {
                writableByteChannel.write(byteBuffer);
            }

            return writableByteChannel;
        } finally {
            inputStream.close();
        }
    }

    private static Writer _copy(Reader reader, Writer writer) throws IOException {
        try {
            char[] buffer = new char[4096 * 16];

            for (int size; (size = reader.read(buffer, 0, buffer.length)) > 0;) {
                writer.write(buffer, 0, size);
            }

            return writer;
        } finally {
            reader.close();
        }
    }

    private static FileChannel _readChannel(Path path) throws IOException {
        return FileChannel.open(path, _readOptions);
    }

    private static FileChannel _writeChannel(Path path) throws IOException {
        return FileChannel.open(path, _writeOptions);
    }
}

Related

  1. largeFileReader(String filename)
  2. mapReadWrite(File file)
  3. nioCopy(ReadableByteChannel input, WritableByteChannel output)
  4. openReadOnly(Path path, int offset, int length)
  5. read(DataInput in, int length)
  6. read(File file)
  7. read(File file, long offset, int length)
  8. read(File from)
  9. read(final File source)