Fills the CharacterBuffer with characters read from the given reader Reader . - Java java.nio

Java examples for java.nio:CharBuffer

Description

Fills the CharacterBuffer with characters read from the given reader Reader .

Demo Code

/*/* ww  w  .  j  a v a2 s. com*/
 * 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.Reader;

public class Main{
    /**
     * Fills the {@link CharacterBuffer} with characters read from the given
     * reader {@link Reader}. This method tries to read <code>numChars</code>
     * characters into the {@link CharacterBuffer}, each call to fill will start
     * filling the buffer from offset <code>0</code> up to <code>numChars</code>.
     * In case code points can span across 2 java characters, this method may
     * only fill <code>numChars - 1</code> characters in order not to split in
     * the middle of a surrogate pair, even if there are remaining characters in
     * the {@link Reader}.
     * <p>
     * This method guarantees
     * that the given {@link CharacterBuffer} will never contain a high surrogate
     * character as the last element in the buffer unless it is the last available
     * character in the reader. In other words, high and low surrogate pairs will
     * always be preserved across buffer boarders.
     * </p>
     * <p>
     * A return value of <code>false</code> means that this method call exhausted
     * the reader, but there may be some bytes which have been read, which can be
     * verified by checking whether <code>buffer.getLength() &gt; 0</code>.
     * </p>
     * 
     * @param buffer
     *          the buffer to fill.
     * @param reader
     *          the reader to read characters from.
     * @param numChars
     *          the number of chars to read
     * @return <code>false</code> if and only if reader.read returned -1 while trying to fill the buffer
     * @throws IOException
     *           if the reader throws an {@link IOException}.
     */
    public static boolean fill(CharacterBuffer buffer, Reader reader,
            int numChars) throws IOException {
        assert buffer.buffer.length >= 2;
        if (numChars < 2 || numChars > buffer.buffer.length) {
            throw new IllegalArgumentException(
                    "numChars must be >= 2 and <= the buffer size");
        }
        final char[] charBuffer = buffer.buffer;
        buffer.offset = 0;
        final int offset;

        // Install the previously saved ending high surrogate:
        if (buffer.lastTrailingHighSurrogate != 0) {
            charBuffer[0] = buffer.lastTrailingHighSurrogate;
            buffer.lastTrailingHighSurrogate = 0;
            offset = 1;
        } else {
            offset = 0;
        }

        final int read = readFully(reader, charBuffer, offset, numChars
                - offset);

        buffer.length = offset + read;
        final boolean result = buffer.length == numChars;
        if (buffer.length < numChars) {
            // We failed to fill the buffer. Even if the last char is a high
            // surrogate, there is nothing we can do
            return result;
        }

        if (Character.isHighSurrogate(charBuffer[buffer.length - 1])) {
            buffer.lastTrailingHighSurrogate = charBuffer[--buffer.length];
        }
        return result;
    }
    /** Convenience method which calls <code>fill(buffer, reader, buffer.buffer.length)</code>. */
    public static boolean fill(CharacterBuffer buffer, Reader reader)
            throws IOException {
        return fill(buffer, reader, buffer.buffer.length);
    }
    static int readFully(Reader reader, char[] dest, int offset, int len)
            throws IOException {
        int read = 0;
        while (read < len) {
            final int r = reader.read(dest, offset + read, len - read);
            if (r == -1) {
                break;
            }
            read += r;
        }
        return read;
    }
}

Related Tutorials