Path: blob/master/test/jdk/java/io/InputStreamReader/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 492631426* @summary Test for InputStreamReader#read(CharBuffer).27* @run testng ReadCharBuffer28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;323334import java.io.ByteArrayInputStream;35import java.io.IOException;36import java.io.InputStreamReader;37import java.io.Reader;38import java.nio.ByteBuffer;39import java.nio.CharBuffer;40import java.util.Arrays;4142import static java.nio.charset.StandardCharsets.US_ASCII;43import static org.testng.Assert.assertEquals;4445public class ReadCharBuffer {4647private static final int BUFFER_SIZE = 24;4849@DataProvider(name = "buffers")50public Object[][] createBuffers() {51// test both on-heap and off-heap buffers as they make use different code paths52return new Object[][]{53new Object[]{CharBuffer.allocate(BUFFER_SIZE)},54new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}55};56}5758@Test(dataProvider = "buffers")59public void read(CharBuffer buffer) throws IOException {60fillBuffer(buffer);6162try (Reader reader = new InputStreamReader(new ByteArrayInputStream("ABCDEFGHIJKLMNOPQRTUVWXYZ".getBytes(US_ASCII)), US_ASCII)) {63buffer.limit(7);64buffer.position(1);65assertEquals(reader.read(buffer), 6);66assertEquals(buffer.position(), 7);67assertEquals(buffer.limit(), 7);6869buffer.limit(16);70buffer.position(8);71assertEquals(reader.read(buffer), 8);72assertEquals(buffer.position(), 16);73assertEquals(buffer.limit(), 16);74}7576buffer.clear();77assertEquals(buffer.toString(), "xABCDEFxGHIJKLMNxxxxxxxx");78}7980private void fillBuffer(CharBuffer buffer) {81char[] filler = new char[BUFFER_SIZE];82Arrays.fill(filler, 'x');83buffer.put(filler);84buffer.clear();85}8687}888990