undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@summary BufferedReader should throw an OutOfMemoryError when the
read-ahead limit is very large
@bug 6350733
@build BigMark
@run main/othervm BigMark
*/
import java.io.*;
public class BigMark {
public static void main(String[] args) throws IOException {
String line;
int i = 0;
String dir = System.getProperty("test.src", ".");
BufferedReader br
= new BufferedReader(new FileReader(new File(dir, "BigMark.java")), 100);
br.mark(200);
line = br.readLine();
System.err.println(i + ": " + line);
i++;
try {
// BR.fill() call to new char[Integer.MAX_VALUE] should succeed
br.mark(Integer.MAX_VALUE);
line = br.readLine();
} catch (OutOfMemoryError x) {
x.printStackTrace();
throw x;
}
System.out.println("OutOfMemoryError not thrown as expected");
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 4072575
@summary Test all the EOL delimiters accepted by BufferedReader
*/
import java.io.*;
public class EOL {
public static void main(String[] args) throws IOException {
Reader sr = new StringReader("one\rtwo\r\nthree\nfour\r");
BufferedReader br = new BufferedReader(sr);
for (int i = 0;; i++) {
String l = br.readLine();
if (l == null) {
if (i != 4)
throw new RuntimeException("Expected 4 lines, got " + i);
break;
}
System.err.println(i + ": " + l);
}
}
}

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 4090383
@summary Ensure that BufferedReader's read method will fill the target array
whenever possible
*/
import java.io.IOException;
import java.io.Reader;
import java.io.BufferedReader;
public class Fill {
/**
* A simple Reader that is always ready but may read fewer than the
* requested number of characters
*/
static class Source extends Reader {
int shortFall;
char next = 0;
Source(int shortFall) {
this.shortFall = shortFall;
}
public int read(char[] cbuf, int off, int len) throws IOException {
int n = len - shortFall;
for (int i = off; i < n; i++)
cbuf[i] = next++;
return n;
}
public boolean ready() {
return true;
}
public void close() throws IOException {
}
}
/**
* Test BufferedReader with an underlying source that always reads
* shortFall fewer characters than requested
*/
static void go(int shortFall) throws Exception {
Reader r = new BufferedReader(new Source(shortFall), 10);
char[] cbuf = new char[8];
int n1 = r.read(cbuf);
int n2 = r.read(cbuf);
System.err.println("Shortfall " + shortFall
+ ": Read " + n1 + ", then " + n2 + " chars");
if (n1 != cbuf.length)
throw new Exception("First read returned " + n1);
if (n2 != cbuf.length)
throw new Exception("Second read returned " + n2);
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 8; i++) go(i);
}
}

View file

@ -0,0 +1,308 @@
/*
* Copyright (c) 2012, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8003258 8029434
* @run junit Lines
*/
import java.io.BufferedReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.LineNumberReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Lines {
private static final Map<String, Integer> cases = new HashMap<>();
static {
cases.put("", 0);
cases.put("Line 1", 1);
cases.put("Line 1\n", 1);
cases.put("Line 1\n\n\n", 3);
cases.put("Line 1\nLine 2\nLine 3", 3);
cases.put("Line 1\nLine 2\nLine 3\n", 3);
cases.put("Line 1\n\nLine 3\n\nLine5", 5);
}
/**
* Helper Reader class which generate specified number of lines contents
* with each line will be "<code>Line &lt;line_number&gt;</code>".
*
* <p>This class also support to simulate {@link IOException} when read pass
* a specified line number.
*/
private static class MockLineReader extends Reader {
final int line_count;
boolean closed = false;
int line_no = 0;
String line = null;
int pos = 0;
int inject_ioe_after_line;
MockLineReader(int cnt) {
this(cnt, cnt);
}
MockLineReader(int cnt, int inject_ioe) {
line_count = cnt;
inject_ioe_after_line = inject_ioe;
}
public void reset() {
synchronized(lock) {
line = null;
line_no = 0;
pos = 0;
closed = false;
}
}
public void inject_ioe() {
inject_ioe_after_line = line_no;
}
public int getLineNumber() {
synchronized(lock) {
return line_no;
}
}
@Override
public void close() {
closed = true;
}
@Override
public int read(char[] buf, int off, int len) throws IOException {
synchronized(lock) {
if (closed) {
throw new IOException("Stream is closed.");
}
if (line == null) {
if (line_count > line_no) {
line_no += 1;
if (line_no > inject_ioe_after_line) {
throw new IOException("Failed to read line " + line_no);
}
line = "Line " + line_no + "\n";
pos = 0;
} else {
return -1; // EOS reached
}
}
int cnt = line.length() - pos;
assert(cnt != 0);
// try to fill with remaining
if (cnt >= len) {
line.getChars(pos, pos + len, buf, off);
pos += len;
if (cnt == len) {
assert(pos == line.length());
line = null;
}
return len;
} else {
line.getChars(pos, pos + cnt, buf, off);
off += cnt;
len -= cnt;
line = null;
/* hold for next read, so we won't IOE during fill buffer
int more = read(buf, off, len);
return (more == -1) ? cnt : cnt + more;
*/
return cnt;
}
}
}
}
private static void verify(Map.Entry<String, Integer> e) {
final String data = e.getKey();
final int total_lines = e.getValue();
assertDoesNotThrow
(() -> {
try (BufferedReader br =
new BufferedReader(new StringReader(data))) {
assertEquals(total_lines,
br.lines().mapToInt(l -> 1).reduce(0, (x, y) -> x + y),
data + " should produce " + total_lines + " lines.");
}
});
}
@Test
public void testLinesBasic() {
// Basic test cases
cases.entrySet().stream().forEach(Lines::verify);
// Similar test, also verify MockLineReader is correct
assertDoesNotThrow
(() -> {
for (int i = 0; i < 10; i++) {
try (BufferedReader br =
new BufferedReader(new MockLineReader(i))) {
assertEquals(i,
br.lines()
.peek(l -> assertTrue(l.matches("^Line \\d+$")))
.mapToInt(l -> 1).reduce(0, (x, y) -> x + y),
"MockLineReader(" + i + ") should produce " + i + " lines.");
}
}
});
}
@Test
public void testUncheckedIOException() throws IOException {
MockLineReader r = new MockLineReader(10, 3);
ArrayList<String> ar = new ArrayList<>();
assertDoesNotThrow
(() -> {
try (BufferedReader br = new BufferedReader(r)) {
br.lines().limit(3L).forEach(ar::add);
assertEquals(3, ar.size(), "Should be able to read 3 lines.");
}
});
r.reset();
assertThrows(UncheckedIOException.class,
() -> {
try (BufferedReader br = new BufferedReader(r)) {
br.lines().forEach(ar::add);
}
});
assertEquals(4, r.getLineNumber(), "should fail to read 4th line");
assertEquals(6, ar.size(), "3 + 3 lines read");
for (int i = 0; i < ar.size(); i++) {
assertEquals("Line " + (i % 3 + 1), ar.get(i));
}
}
@Test
public void testIterator() throws IOException {
MockLineReader r = new MockLineReader(6);
BufferedReader br = new BufferedReader(r);
String line = br.readLine();
assertEquals(1, r.getLineNumber(), "Read one line");
Stream<String> s = br.lines();
Iterator<String> it = s.iterator();
// Ensure iterate with only next works
for (int i = 0; i < 5; i++) {
String str = it.next();
assertEquals("Line " + (i + 2), str, "Addtional five lines");
}
// NoSuchElementException
assertThrows(NoSuchElementException.class, () -> it.next(),
"Should have run out of lines.");
}
@Test
public void testPartialReadAndLineNo() throws IOException {
MockLineReader r = new MockLineReader(5);
LineNumberReader lr = new LineNumberReader(r);
char[] buf = new char[5];
lr.read(buf, 0, 5);
assertEquals(0, lr.getLineNumber(), "LineNumberReader start with line 0");
assertEquals(1, r.getLineNumber(), "MockLineReader start with line 1");
assertEquals("Line ", new String(buf));
String l1 = lr.readLine();
assertEquals("1", l1, "Remaining of the first line");
assertEquals(1, lr.getLineNumber(), "Line 1 is read");
assertEquals(1, r.getLineNumber(), "MockLineReader not yet go next line");
lr.read(buf, 0, 4);
assertEquals(1, lr.getLineNumber(), "In the middle of line 2");
assertEquals("Line", new String(buf, 0, 4));
ArrayList<String> ar = lr.lines()
.peek(l -> assertEquals(lr.getLineNumber(), r.getLineNumber()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
assertEquals(" 2", ar.get(0), "Remaining in the second line");
for (int i = 1; i < ar.size(); i++) {
assertEquals("Line " + (i + 2), ar.get(i), "Rest are full lines");
}
}
@Test
public void testInterlacedRead() throws IOException {
MockLineReader r = new MockLineReader(10);
BufferedReader br = new BufferedReader(r);
char[] buf = new char[5];
Stream<String> s = br.lines();
Iterator<String> it = s.iterator();
br.read(buf);
assertEquals("Line ", new String(buf));
assertEquals("1", it.next());
assertThrows(IllegalStateException.class, () -> s.iterator().next(),
"Should fail on second call to Iterator next method");
br.read(buf, 0, 2);
assertEquals("Li", new String(buf, 0, 2));
// Get stream again should continue from where left
// Only read remaining of the line
br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
br.read(buf, 0, 2);
assertEquals("Li", new String(buf, 0, 2));
br.read(buf, 0, 2);
assertEquals("ne", new String(buf, 0, 2));
assertEquals(" 3", it.next());
// Line 4
br.readLine();
// interator pick
assertEquals("Line 5", it.next());
// Another stream instantiated by lines()
AtomicInteger line_no = new AtomicInteger(6);
br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
// Read after EOL
assertFalse(it.hasNext());
}
@Test
public void testCharacteristics() {
assertDoesNotThrow
(() -> {
try (BufferedReader br =
new BufferedReader(new StringReader(""))) {
Spliterator<String> instance = br.lines().spliterator();
assertTrue(instance.hasCharacteristics(Spliterator.NONNULL));
assertTrue(instance.hasCharacteristics(Spliterator.ORDERED));
}
});
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 4069687
@summary Test if fill() will behave correctly at EOF
when mark is set.
*/
import java.io.*;
public class MarkedFillAtEOF {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new StringReader("12"));
int count = 0;
r.read();
r.mark(2);
// trigger the call to fill()
while (r.read() != -1);
r.reset();
// now should only read 1 character
while (r.read() != -1) {
count++;
}
if (count != 1) {
throw new Exception("Expect 1 character, but got " + count);
}
}
}

View file

@ -0,0 +1,147 @@
/*
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4151072
* @summary Ensure that BufferedReader's methods handle the new line character
* following the carriage return correctly after a readLine
* operation that resulted in reading a line terminated by a
* carriage return (\r).
*/
import java.io.*;
public class ReadLine {
public static void main(String[] args) throws IOException {
// Make sure that the reader does not wait for additional characters to
// be read after reading a new line.
BufferedReader reader;
String[][] strings = {
{"CR/LF\r\n", "CR/LF"},
{"LF-Only\n", "LF-Only"},
{"CR-Only\r", "CR-Only"},
{"CR/LF line\r\nMore data", "More data"}
};
// test 0 "CR/LF\r\n"
// test 1 "LF-Only\n"
// test 2 "CR-Only\r"
for (int i = 0; i < 3; i++) {
reader = new BufferedReader(new
BoundedReader(strings[i][0]), strings[i][0].length());
if (!reader.readLine().equals(strings[i][1]))
throw new RuntimeException("Read incorrect text");
}
// Now test the mark and reset operations. Consider two cases.
// 1. For lines ending with CR only.
markResetTest("Lot of textual data\rMore textual data\n",
"More textual data");
// 2. Now for lines ending with CR/LF
markResetTest("Lot of textual data\r\nMore textual data\n",
"More textual data");
// 3. Now for lines ending with LF only
markResetTest("Lot of textual data\nMore textual data\n",
"More textual data");
// Need to ensure behavior of read() after a readLine() read of a CR/LF
// terminated line.
// 1. For lines ending with CR/LF only.
// uses "CR/LF line\r\nMore data"
reader = new BufferedReader(new
BoundedReader(strings[3][0]), strings[3][0].length());
reader.readLine();
if (reader.read() != 'M')
throw new RuntimeException("Read() failed");
// Need to ensure that a read(char[], int, int) following a readLine()
// read of a CR/LF terminated line behaves correctly.
// uses "CR/LF line\r\nMore data"
reader = new BufferedReader(new
BoundedReader(strings[3][0]), strings[3][0].length());
reader.readLine();
char[] buf = new char[9];
reader.read(buf, 0, 9);
String newStr = new String(buf);
if (!newStr.equals(strings[3][1]))
throw new RuntimeException("Read(char[],int,int) failed");
}
static void markResetTest(String inputStr, String resetStr)
throws IOException {
BufferedReader reader = new BufferedReader(new
BoundedReader(inputStr), inputStr.length());
System.out.println("> " + reader.readLine());
reader.mark(30);
System.out.println("......Marking stream .....");
String str = reader.readLine();
System.out.println("> " + str);
reader.reset();
String newStr = reader.readLine();
System.out.println("reset> " + newStr);
// Make sure that the reset point was set correctly.
if (!newStr.equals(resetStr))
throw new RuntimeException("Mark/Reset failed");
}
private static class BoundedReader extends Reader{
private char[] content;
private int limit;
private int pos = 0;
public BoundedReader(String content) {
this.limit = content.length();
this.content = new char[limit];
content.getChars(0, limit, this.content, 0);
}
public int read() throws IOException {
if (pos >= limit)
throw new RuntimeException("Read past limit");
return content[pos++];
}
public int read(char[] buf, int offset, int length)
throws IOException
{
int oldPos = pos;
for (int i = offset; i < length; i++) {
buf[i] = (char)read();
}
return (pos - oldPos);
}
public void close() {}
}
}

View file

@ -0,0 +1,134 @@
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 5073414
* @summary Ensure that there is no race condition in BufferedReader.readLine()
* when a line is terminated by '\r\n' is read by multiple threads.
*/
import java.io.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ReadLineSync {
public static int lineCount = 0;
public static void main( String[] args ) throws Exception {
String dir = System.getProperty(".", ".");
File f = new File(dir, "test.txt");
createFile(f);
f.deleteOnExit();
BufferedReader reader = new BufferedReader(
new FileReader(f));
try {
int threadCount = 2;
ExecutorService es = Executors.newFixedThreadPool(threadCount);
for (int i=0; i < threadCount; i++)
es.execute(new BufferedReaderConsumer(reader));
// Wait for the tasks to complete
es.shutdown();
while (!es.awaitTermination(60, TimeUnit.SECONDS));
} finally {
reader.close();
}
}
static class BufferedReaderConsumer extends Thread {
BufferedReader reader;
public BufferedReaderConsumer( BufferedReader reader ) {
this.reader = reader;
}
public void run() {
try {
String record = reader.readLine();
if ( record == null ) {
// if the first thread is too fast the second will hit
// this which is ok
System.out.println( "File already finished" );
return;
}
if ( record.length() == 0 ) {
// usually it comes out here indicating the first read
// done by the second thread to run failed
System.out.println("Empty string on first read." +
Thread.currentThread().getName() );
}
while ( record != null ) {
lineCount++;
// Verify the token count
if ( record.length() == 0 ) {
// very occasionally it will fall over here
throw new Exception( "Invalid tokens with string '" +
record + "' on line " + lineCount );
}
record = reader.readLine();
}
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}
// Create a relatively big file
private static void createFile(File f) throws IOException {
BufferedWriter w = new BufferedWriter(
new FileWriter(f));
int count = 10000;
while (count > 0) {
w.write("abcd \r\n");
w.write("efg \r\n");
w.write("hijk \r\n");
w.write("lmnop \r\n");
w.write("qrstuv \r\n");
w.write("wxy and z \r\n");
w.write("now you \r\n");
w.write("know your \r\n");
w.write("abc \r\n");
w.write("next time \r\n");
w.write("want you \r\n");
w.write("sing with me \r\n");
count--;
}
w.close();
}
}

View file

@ -0,0 +1,105 @@
/*
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4329985
* @summary Ensure that BufferedReader's ready() method handles the new line
* character following the carriage return correctly and returns the right
* value so that a read operation after a ready() does not block unnecessarily.
*/
import java.io.*;
public class Ready {
public static void main(String[] args) throws IOException {
BufferedReader reader;
String[] strings = {
"LF-Only\n",
"LF-Only\n",
"CR/LF\r\n",
"CR/LF\r\n",
"CR-Only\r",
"CR-Only\r",
"CR/LF line\r\nMore data.\r\n",
"CR/LF line\r\nMore data.\r\n"
};
// The buffer sizes are chosen such that the boundary conditions are
// tested.
int[] bufsizes = { 7, 8, 6, 5, 7, 8, 11, 10};
for (int i = 0; i < strings.length; i++) {
reader = new BufferedReader(new BoundedReader(strings[i]),
bufsizes[i]);
while (reader.ready()) {
String str = reader.readLine();
System.out.println("read>>" + str);
}
}
}
private static class BoundedReader extends Reader{
private char[] content;
private int limit;
private int pos = 0;
public BoundedReader(String content) {
this.limit = content.length();
this.content = new char[limit];
content.getChars(0, limit, this.content, 0);
}
public int read() throws IOException {
if (pos >= limit)
throw new RuntimeException("Hit infinite wait condition");
return content[pos++];
}
public int read(char[] buf, int offset, int length)
throws IOException
{
if (pos >= limit)
throw new RuntimeException("Hit infinite wait condition");
int oldPos = pos;
int readlen = (length > (limit - pos)) ? (limit - pos) : length;
for (int i = offset; i < readlen; i++) {
buf[i] = (char)read();
}
return (pos - oldPos);
}
public void close() {}
public boolean ready() {
if (pos < limit)
return true;
else
return false;
}
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 4152453
@summary Skip must throw an exception for negative args
*/
import java.io.*;
public class SkipNegative {
public static void main(String argv[]) throws Exception {
char[] cbuf = "testString".toCharArray();
CharArrayReader CAR = new CharArrayReader(cbuf);
BufferedReader BR = new BufferedReader(CAR);
long nchars = -1L;
try {
long actual = BR.skip(nchars);
} catch(IllegalArgumentException e){
// Negative argument caught
return;
}
throw new Exception("Skip should not accept negative values");
}
}