Skip to content

Commit 7ffa148

Browse files
author
Brian Burkhalter
committed
8247918: Clarify Reader.skip behavior for end of stream
Reviewed-by: rriggs, naoto
1 parent 8a1c712 commit 7ffa148

File tree

8 files changed

+108
-69
lines changed

8 files changed

+108
-69
lines changed

src/java.base/share/classes/java/io/BufferedReader.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -397,14 +397,7 @@ public String readLine() throws IOException {
397397
}
398398

399399
/**
400-
* Skips characters.
401-
*
402-
* @param n The number of characters to skip
403-
*
404-
* @return The number of characters actually skipped
405-
*
406-
* @throws IllegalArgumentException If {@code n} is negative.
407-
* @throws IOException If an I/O error occurs
400+
* {@inheritDoc}
408401
*/
409402
public long skip(long n) throws IOException {
410403
if (n < 0L) {

src/java.base/share/classes/java/io/CharArrayReader.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -146,16 +146,19 @@ public int read(char b[], int off, int len) throws IOException {
146146
}
147147

148148
/**
149-
* Skips characters. Returns the number of characters that were skipped.
149+
* Skips characters. If the stream is already at its end before this method
150+
* is invoked, then no characters are skipped and zero is returned.
150151
*
151152
* <p>The {@code n} parameter may be negative, even though the
152153
* {@code skip} method of the {@link Reader} superclass throws
153154
* an exception in this case. If {@code n} is negative, then
154155
* this method does nothing and returns {@code 0}.
155156
*
156-
* @param n The number of characters to skip
157-
* @return The number of characters actually skipped
158-
* @throws IOException If the stream is closed, or an I/O error occurs
157+
* @param n {@inheritDoc}
158+
*
159+
* @return {@inheritDoc}
160+
*
161+
* @throws IOException {@inheritDoc}
159162
*/
160163
public long skip(long n) throws IOException {
161164
synchronized (lock) {

src/java.base/share/classes/java/io/FilterReader.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -76,9 +76,11 @@ public int read(char cbuf[], int off, int len) throws IOException {
7676
}
7777

7878
/**
79-
* Skips characters.
79+
* {@inheritDoc}
8080
*
81-
* @throws IOException If an I/O error occurs
81+
* @throws IllegalArgumentException If {@code n} is negative and the
82+
* contained {@code Reader}'s {@code skip} method throws an
83+
* IllegalArgumentException for a negative parameter
8284
*/
8385
public long skip(long n) throws IOException {
8486
return in.skip(n);

src/java.base/share/classes/java/io/LineNumberReader.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -267,18 +267,7 @@ public String readLine() throws IOException {
267267
private char skipBuffer[] = null;
268268

269269
/**
270-
* Skip characters.
271-
*
272-
* @param n
273-
* The number of characters to skip
274-
*
275-
* @return The number of characters actually skipped
276-
*
277-
* @throws IOException
278-
* If an I/O error occurs
279-
*
280-
* @throws IllegalArgumentException
281-
* If {@code n} is negative
270+
* {@inheritDoc}
282271
*/
283272
public long skip(long n) throws IOException {
284273
if (n < 0)

src/java.base/share/classes/java/io/PushbackReader.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -254,15 +254,7 @@ public void close() throws IOException {
254254
}
255255

256256
/**
257-
* Skips characters. This method will block until some characters are
258-
* available, an I/O error occurs, or the end of the stream is reached.
259-
*
260-
* @param n The number of characters to skip
261-
*
262-
* @return The number of characters actually skipped
263-
*
264-
* @throws IllegalArgumentException If {@code n} is negative.
265-
* @throws IOException If an I/O error occurs
257+
* {@inheritDoc}
266258
*/
267259
public long skip(long n) throws IOException {
268260
if (n < 0L)

src/java.base/share/classes/java/io/Reader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -269,6 +269,8 @@ public int read(char cbuf[]) throws IOException {
269269
/**
270270
* Skips characters. This method will block until some characters are
271271
* available, an I/O error occurs, or the end of the stream is reached.
272+
* If the stream is already at its end before this method is invoked,
273+
* then no characters are skipped and zero is returned.
272274
*
273275
* @param n The number of characters to skip
274276
*

src/java.base/share/classes/java/io/StringReader.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -105,31 +105,35 @@ public int read(char cbuf[], int off, int len) throws IOException {
105105
}
106106

107107
/**
108-
* Skips the specified number of characters in the stream. Returns
109-
* the number of characters that were skipped.
108+
* Skips characters. If the stream is already at its end before this method
109+
* is invoked, then no characters are skipped and zero is returned.
110110
*
111-
* <p>The {@code ns} parameter may be negative, even though the
111+
* <p>The {@code n} parameter may be negative, even though the
112112
* {@code skip} method of the {@link Reader} superclass throws
113-
* an exception in this case. Negative values of {@code ns} cause the
113+
* an exception in this case. Negative values of {@code n} cause the
114114
* stream to skip backwards. Negative return values indicate a skip
115115
* backwards. It is not possible to skip backwards past the beginning of
116116
* the string.
117117
*
118118
* <p>If the entire string has been read or skipped, then this method has
119-
* no effect and always returns 0.
119+
* no effect and always returns {@code 0}.
120120
*
121-
* @throws IOException If an I/O error occurs
121+
* @param n {@inheritDoc}
122+
*
123+
* @return {@inheritDoc}
124+
*
125+
* @throws IOException {@inheritDoc}
122126
*/
123-
public long skip(long ns) throws IOException {
127+
public long skip(long n) throws IOException {
124128
synchronized (lock) {
125129
ensureOpen();
126130
if (next >= length)
127131
return 0;
128132
// Bound skip by beginning and end of the source
129-
long n = Math.min(length - next, ns);
130-
n = Math.max(-next, n);
131-
next += n;
132-
return n;
133+
long r = Math.min(length - next, n);
134+
r = Math.max(-next, r);
135+
next += r;
136+
return r;
133137
}
134138
}
135139

test/jdk/java/io/Reader/Skip.java

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,29 +22,83 @@
2222
*/
2323

2424
/* @test
25-
@bug 4134311
26-
@summary Test if skip works correctly
25+
* @bug 4134311 8247918
26+
* @summary Test if skip works correctly
27+
* @run testng Skip
2728
*/
2829

30+
import java.io.CharArrayReader;
31+
import java.io.File;
32+
import java.io.FileReader;
33+
import java.io.IOException;
34+
import java.io.LineNumberReader;
35+
import java.io.PushbackReader;
36+
import java.io.RandomAccessFile;
37+
import java.io.Reader;
38+
import java.io.StringReader;
2939

30-
31-
import java.io.*;
40+
import org.testng.Assert;
41+
import org.testng.annotations.DataProvider;
42+
import org.testng.annotations.Test;
3243

3344
public class Skip {
34-
public static void main(String argv[]) throws Exception {
35-
File f = new File(System.getProperty("test.src", "."),
36-
"SkipInput.txt");
37-
FileReader fr = new FileReader(f);
38-
try {
45+
private static String FILENAME =
46+
System.getProperty("test.src", ".") + File.separator + "SkipInput.txt";
47+
private static File file = new File(FILENAME);
48+
49+
@Test
50+
public void skip() throws IOException {
51+
try (FileReader fr = new FileReader(file)) {
3952
long nchars = 8200;
4053
long actual = fr.skip(nchars);
4154

42-
if (actual > nchars) {
43-
throw new Exception
44-
("Should skip " + nchars + ", but skipped " +actual+" chars");
45-
}
46-
} finally {
47-
fr.close();
55+
Assert.assertFalse(actual > nchars,
56+
"Should skip " + nchars + ", but skipped " +actual+" chars");
4857
}
4958
}
59+
60+
@DataProvider(name = "readers")
61+
public Object[][] getReaders() throws IOException {
62+
return new Object[][] {
63+
{new LineNumberReader(new FileReader(file))},
64+
{new CharArrayReader(new char[] {27})},
65+
{new PushbackReader(new FileReader(file))},
66+
{new FileReader(file)},
67+
{new StringReader(new String(new byte[] {(byte)42}))}
68+
};
69+
}
70+
71+
@Test(dataProvider = "readers")
72+
public void eof(Reader r) throws IOException {
73+
r.skip(Long.MAX_VALUE);
74+
Assert.assertEquals(r.skip(1), 0);
75+
Assert.assertEquals(r.read(), -1);
76+
}
77+
78+
@DataProvider(name = "skipIAE")
79+
public Object[][] getSkipIAEs() throws IOException {
80+
return new Object[][] {
81+
{new LineNumberReader(new FileReader(file))},
82+
{new PushbackReader(new FileReader(file))},
83+
{new FileReader(file)}
84+
};
85+
}
86+
87+
@Test(dataProvider = "skipIAE", expectedExceptions = IllegalArgumentException.class)
88+
public void testThrowsIAE(Reader r) throws IOException {
89+
r.skip(-1);
90+
}
91+
92+
@DataProvider(name = "skipNoIAE")
93+
public Object[][] getSkipNoIAEs() throws IOException {
94+
return new Object[][] {
95+
{new CharArrayReader(new char[] {27})},
96+
{new StringReader(new String(new byte[] {(byte)42}))}
97+
};
98+
}
99+
100+
@Test(dataProvider = "skipNoIAE")
101+
public void testNoIAE(Reader r) throws IOException {
102+
r.skip(-1);
103+
}
50104
}

0 commit comments

Comments
 (0)