• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 InputStreamReader#read(CharBuffer).
28  * @run testng ReadCharBuffer
29  */
30 
31 package test.java.io.InputStreamReader;
32 
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35 
36 
37 import java.io.ByteArrayInputStream;
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 import java.io.Reader;
41 import java.nio.ByteBuffer;
42 import java.nio.CharBuffer;
43 import java.util.Arrays;
44 
45 import static java.nio.charset.StandardCharsets.US_ASCII;
46 import static org.testng.Assert.assertEquals;
47 
48 public class ReadCharBuffer {
49 
50     private static final int BUFFER_SIZE = 24;
51 
52     @DataProvider(name = "buffers")
createBuffers()53     public Object[][] createBuffers() {
54         // test both on-heap and off-heap buffers as they make use different code paths
55         return new Object[][]{
56                 new Object[]{CharBuffer.allocate(BUFFER_SIZE)},
57                 new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}
58         };
59     }
60 
61     @Test(dataProvider = "buffers")
read(CharBuffer buffer)62     public void read(CharBuffer buffer) throws IOException {
63         fillBuffer(buffer);
64 
65         try (Reader reader = new InputStreamReader(new ByteArrayInputStream("ABCDEFGHIJKLMNOPQRTUVWXYZ".getBytes(US_ASCII)), US_ASCII)) {
66             buffer.limit(7);
67             buffer.position(1);
68             assertEquals(reader.read(buffer), 6);
69             assertEquals(buffer.position(), 7);
70             assertEquals(buffer.limit(), 7);
71 
72             buffer.limit(16);
73             buffer.position(8);
74             assertEquals(reader.read(buffer), 8);
75             assertEquals(buffer.position(), 16);
76             assertEquals(buffer.limit(), 16);
77         }
78 
79         buffer.clear();
80         assertEquals(buffer.toString(), "xABCDEFxGHIJKLMNxxxxxxxx");
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