Path: blob/master/test/jdk/java/io/Reader/ReadCharBuffer.java
41149 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4926314 826601426* @summary Test for Reader#read(CharBuffer).27* @run testng ReadCharBuffer28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;323334import java.io.IOException;35import java.io.BufferedReader;36import java.io.CharArrayReader;37import java.io.Reader;38import java.io.UncheckedIOException;39import java.nio.ByteBuffer;40import java.nio.CharBuffer;41import java.util.Arrays;42import java.util.Objects;4344import static org.testng.Assert.assertEquals;4546@Test(groups = "unit")47public class ReadCharBuffer {4849private static final int BUFFER_SIZE = 8 + 8192 + 2;5051@DataProvider(name = "buffers")52public Object[][] createBuffers() {53// test both on-heap and off-heap buffers as they make use different code paths54return new Object[][]{55new Object[]{CharBuffer.allocate(BUFFER_SIZE)},56new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}57};58}5960@Test(dataProvider = "buffers")61public void read(CharBuffer buffer) throws IOException {62fillBuffer(buffer);6364StringBuilder input = new StringBuilder(BUFFER_SIZE - 2 + 1);65input.append("ABCDEF");66for (int i = 0; i < 8192; i++) {67input.append('y');68}69input.append("GH");7071try (Reader reader = new UnoptimizedStringReader(input.toString())) {72// put only between position and limit in the target buffer73int limit = 1 + 6;74buffer.limit(limit);75buffer.position(1);76assertEquals(reader.read(buffer), 6);77assertEquals(buffer.position(), limit);78assertEquals(buffer.limit(), limit);7980// read the full temporary buffer81// and then accurately reduce the next #read call82limit = 8 + 8192 + 1;83buffer.limit(8 + 8192 + 1);84buffer.position(8);85assertEquals(reader.read(buffer), 8192 + 1);86assertEquals(buffer.position(), limit);87assertEquals(buffer.limit(), limit);8889assertEquals(reader.read(), 'H');90assertEquals(reader.read(), -1);91}9293buffer.clear();94StringBuilder expected = new StringBuilder(BUFFER_SIZE);95expected.append("xABCDEFx");96for (int i = 0; i < 8192; i++) {97expected.append('y');98}99expected.append("Gx");100assertEquals(buffer.toString(), expected.toString());101}102103@Test104public void readZeroLength() {105char[] buf = new char[] {1, 2, 3};106BufferedReader r = new BufferedReader(new CharArrayReader(buf));107int n = -1;108try {109n = r.read(CharBuffer.allocate(0));110} catch (IOException e) {111throw new UncheckedIOException(e);112}113assertEquals(n, 0);114}115116private void fillBuffer(CharBuffer buffer) {117char[] filler = new char[buffer.remaining()];118Arrays.fill(filler, 'x');119buffer.put(filler);120buffer.clear();121}122123/**124* Unoptimized version of StringReader in case StringReader overrides125* #read(CharBuffer)126*/127static final class UnoptimizedStringReader extends Reader {128129private String str;130private int next = 0;131132UnoptimizedStringReader(String s) {133this.str = s;134}135136@Override137public int read() throws IOException {138synchronized (lock) {139if (next >= str.length())140return -1;141return str.charAt(next++);142}143}144145@Override146public int read(char cbuf[], int off, int len) throws IOException {147synchronized (lock) {148Objects.checkFromIndexSize(off, len, cbuf.length);149if (next >= str.length())150return -1;151int n = Math.min(str.length() - next, len);152str.getChars(next, next + n, cbuf, off);153next += n;154return n;155}156}157158@Override159public void close() throws IOException {160161}162}163164}165166167