Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/CharArrayReader/ReadCharBuffer.java
41152 views
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
import org.testng.annotations.DataProvider;
32
import org.testng.annotations.Test;
33
34
35
import java.io.CharArrayReader;
36
import java.io.IOException;
37
import java.io.Reader;
38
import java.nio.ByteBuffer;
39
import java.nio.CharBuffer;
40
import java.util.Arrays;
41
42
import static org.testng.Assert.assertEquals;
43
44
public class ReadCharBuffer {
45
46
private static final int BUFFER_SIZE = 7;
47
48
@DataProvider(name = "buffers")
49
public Object[][] createBuffers() {
50
// test both on-heap and off-heap buffers as they may use different code paths
51
return new Object[][]{
52
new Object[]{CharBuffer.allocate(BUFFER_SIZE)},
53
new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}
54
};
55
}
56
57
@Test(dataProvider = "buffers")
58
public void read(CharBuffer buffer) throws IOException {
59
fillBuffer(buffer);
60
61
try (Reader reader = new CharArrayReader("ABCD".toCharArray())) {
62
buffer.limit(3);
63
buffer.position(1);
64
assertEquals(reader.read(buffer), 2);
65
assertEquals(buffer.position(), 3);
66
assertEquals(buffer.limit(), 3);
67
68
buffer.limit(7);
69
buffer.position(4);
70
assertEquals(reader.read(buffer), 2);
71
assertEquals(buffer.position(), 6);
72
assertEquals(buffer.limit(), 7);
73
74
assertEquals(reader.read(buffer), -1);
75
}
76
77
buffer.clear();
78
assertEquals(buffer.toString(), "xABxCDx");
79
}
80
81
private void fillBuffer(CharBuffer buffer) {
82
char[] filler = new char[BUFFER_SIZE];
83
Arrays.fill(filler, 'x');
84
buffer.put(filler);
85
buffer.clear();
86
}
87
88
}
89
90