Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Reader/ReadCharBuffer.java
41149 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 8266014
27
* @summary Test for Reader#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.IOException;
36
import java.io.BufferedReader;
37
import java.io.CharArrayReader;
38
import java.io.Reader;
39
import java.io.UncheckedIOException;
40
import java.nio.ByteBuffer;
41
import java.nio.CharBuffer;
42
import java.util.Arrays;
43
import java.util.Objects;
44
45
import static org.testng.Assert.assertEquals;
46
47
@Test(groups = "unit")
48
public class ReadCharBuffer {
49
50
private static final int BUFFER_SIZE = 8 + 8192 + 2;
51
52
@DataProvider(name = "buffers")
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")
62
public void read(CharBuffer buffer) throws IOException {
63
fillBuffer(buffer);
64
65
StringBuilder input = new StringBuilder(BUFFER_SIZE - 2 + 1);
66
input.append("ABCDEF");
67
for (int i = 0; i < 8192; i++) {
68
input.append('y');
69
}
70
input.append("GH");
71
72
try (Reader reader = new UnoptimizedStringReader(input.toString())) {
73
// put only between position and limit in the target buffer
74
int limit = 1 + 6;
75
buffer.limit(limit);
76
buffer.position(1);
77
assertEquals(reader.read(buffer), 6);
78
assertEquals(buffer.position(), limit);
79
assertEquals(buffer.limit(), limit);
80
81
// read the full temporary buffer
82
// and then accurately reduce the next #read call
83
limit = 8 + 8192 + 1;
84
buffer.limit(8 + 8192 + 1);
85
buffer.position(8);
86
assertEquals(reader.read(buffer), 8192 + 1);
87
assertEquals(buffer.position(), limit);
88
assertEquals(buffer.limit(), limit);
89
90
assertEquals(reader.read(), 'H');
91
assertEquals(reader.read(), -1);
92
}
93
94
buffer.clear();
95
StringBuilder expected = new StringBuilder(BUFFER_SIZE);
96
expected.append("xABCDEFx");
97
for (int i = 0; i < 8192; i++) {
98
expected.append('y');
99
}
100
expected.append("Gx");
101
assertEquals(buffer.toString(), expected.toString());
102
}
103
104
@Test
105
public void readZeroLength() {
106
char[] buf = new char[] {1, 2, 3};
107
BufferedReader r = new BufferedReader(new CharArrayReader(buf));
108
int n = -1;
109
try {
110
n = r.read(CharBuffer.allocate(0));
111
} catch (IOException e) {
112
throw new UncheckedIOException(e);
113
}
114
assertEquals(n, 0);
115
}
116
117
private void fillBuffer(CharBuffer buffer) {
118
char[] filler = new char[buffer.remaining()];
119
Arrays.fill(filler, 'x');
120
buffer.put(filler);
121
buffer.clear();
122
}
123
124
/**
125
* Unoptimized version of StringReader in case StringReader overrides
126
* #read(CharBuffer)
127
*/
128
static final class UnoptimizedStringReader extends Reader {
129
130
private String str;
131
private int next = 0;
132
133
UnoptimizedStringReader(String s) {
134
this.str = s;
135
}
136
137
@Override
138
public int read() throws IOException {
139
synchronized (lock) {
140
if (next >= str.length())
141
return -1;
142
return str.charAt(next++);
143
}
144
}
145
146
@Override
147
public int read(char cbuf[], int off, int len) throws IOException {
148
synchronized (lock) {
149
Objects.checkFromIndexSize(off, len, cbuf.length);
150
if (next >= str.length())
151
return -1;
152
int n = Math.min(str.length() - next, len);
153
str.getChars(next, next + n, cbuf, off);
154
next += n;
155
return n;
156
}
157
}
158
159
@Override
160
public void close() throws IOException {
161
162
}
163
}
164
165
}
166
167