1 /* 2 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4926314 27 * @summary Test for CharArrayReader#read(CharBuffer). 28 * @run testng ReadCharBuffer 29 */ 30 31 package test.java.io.CharArrayReader; 32 33 import org.testng.annotations.DataProvider; 34 import org.testng.annotations.Test; 35 36 37 import java.io.CharArrayReader; 38 import java.io.IOException; 39 import java.io.Reader; 40 import java.nio.ByteBuffer; 41 import java.nio.CharBuffer; 42 import java.util.Arrays; 43 44 import static org.testng.Assert.assertEquals; 45 46 public class ReadCharBuffer { 47 48 private static final int BUFFER_SIZE = 7; 49 50 @DataProvider(name = "buffers") createBuffers()51 public Object[][] createBuffers() { 52 // test both on-heap and off-heap buffers as they may use different code paths 53 return new Object[][]{ 54 new Object[]{CharBuffer.allocate(BUFFER_SIZE)}, 55 new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()} 56 }; 57 } 58 59 @Test(dataProvider = "buffers") read(CharBuffer buffer)60 public void read(CharBuffer buffer) throws IOException { 61 fillBuffer(buffer); 62 63 try (Reader reader = new CharArrayReader("ABCD".toCharArray())) { 64 buffer.limit(3); 65 buffer.position(1); 66 assertEquals(reader.read(buffer), 2); 67 assertEquals(buffer.position(), 3); 68 assertEquals(buffer.limit(), 3); 69 70 buffer.limit(7); 71 buffer.position(4); 72 assertEquals(reader.read(buffer), 2); 73 assertEquals(buffer.position(), 6); 74 assertEquals(buffer.limit(), 7); 75 76 assertEquals(reader.read(buffer), -1); 77 } 78 79 buffer.clear(); 80 assertEquals(buffer.toString(), "xABxCDx"); 81 } 82 fillBuffer(CharBuffer buffer)83 private void fillBuffer(CharBuffer buffer) { 84 char[] filler = new char[BUFFER_SIZE]; 85 Arrays.fill(filler, 'x'); 86 buffer.put(filler); 87 buffer.clear(); 88 } 89 90 } 91