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:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
62
test/jdk/java/io/PrintStream/CheckError.java
Normal file
62
test/jdk/java/io/PrintStream/CheckError.java
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 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 4213822
|
||||
* @summary Test that checkError() returns a correct value
|
||||
* when a PrintWriter is wrapped with another
|
||||
* PrintWriter.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class CheckError {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
File file = new File(System.getProperty("test.dir", "."),
|
||||
"junkie.out");
|
||||
file.deleteOnExit();
|
||||
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
PrintStream pps = new PrintStream(
|
||||
new PrintStream(fos));
|
||||
boolean passTest1 = false;
|
||||
|
||||
fos.close();
|
||||
pps.println("Hello World!");
|
||||
|
||||
if (pps.checkError()) {
|
||||
System.out.println("Correct: An error occured in the" +
|
||||
" underlying Stream");
|
||||
passTest1 = true;
|
||||
}
|
||||
pps.close();
|
||||
|
||||
if (!passTest1) {
|
||||
throw new Exception("CheckError() returned an incorrect value" +
|
||||
" when the error has occured in the underlying Stream");
|
||||
}
|
||||
}
|
||||
}
|
||||
67
test/jdk/java/io/PrintStream/ClearErrorStream.java
Normal file
67
test/jdk/java/io/PrintStream/ClearErrorStream.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 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 4491255
|
||||
* @summary Test for a new protected method PrintStream.clearError()
|
||||
* to reset the internal error state
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ClearErrorStream extends PrintStream {
|
||||
|
||||
public ClearErrorStream(OutputStream out, boolean autoFlush) {
|
||||
super(out, autoFlush);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
File f = new File(System.getProperty("test.dir", "."),
|
||||
"print-stream.out");
|
||||
f.deleteOnExit();
|
||||
|
||||
ClearErrorStream out = new ClearErrorStream(
|
||||
new BufferedOutputStream(
|
||||
new FileOutputStream(f)),
|
||||
true);
|
||||
out.println("Hello World!");
|
||||
out.close();
|
||||
out.println("Writing after close");
|
||||
|
||||
if (out.checkError()) {
|
||||
System.out.println("An error occured");
|
||||
out.clearError();
|
||||
|
||||
if (!out.checkError()) {
|
||||
System.out.println("Error status cleared");
|
||||
} else {
|
||||
throw new Exception("Error Status unchanged");
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println(" No error occured");
|
||||
}
|
||||
}
|
||||
}
|
||||
54
test/jdk/java/io/PrintStream/EncodingConstructor.java
Normal file
54
test/jdk/java/io/PrintStream/EncodingConstructor.java
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2011, 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 4378278
|
||||
* @summary java.io.PrintStream(..., String encoding) constructor
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
|
||||
public class EncodingConstructor {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
String s = "xyzzy";
|
||||
int n = s.length();
|
||||
try (PrintStream ps = new PrintStream(bo, false, "UTF-8")) {
|
||||
ps.print(s);
|
||||
}
|
||||
byte[] ba = bo.toByteArray();
|
||||
if (ba.length != n)
|
||||
throw new Exception("Length mismatch: " + n + " " + ba.length);
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ba[i] != (byte)s.charAt(i))
|
||||
throw new Exception("Content mismatch: "
|
||||
+ i + " "
|
||||
+ Integer.toString(ba[i]) + " "
|
||||
+ Integer.toString(s.charAt(i)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
137
test/jdk/java/io/PrintStream/EncodingTest.java
Normal file
137
test/jdk/java/io/PrintStream/EncodingTest.java
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
* Copyright (c) 2017, 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.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8183743
|
||||
* @summary Test to verify the new overload method with Charset functions the same
|
||||
* as the existing method that takes a charset name.
|
||||
* @run junit EncodingTest
|
||||
*/
|
||||
public class EncodingTest {
|
||||
static String USER_DIR = System.getProperty("user.dir", ".");
|
||||
static boolean AUTOFLUSH = true;
|
||||
public static enum ConstructorType {
|
||||
STRING,
|
||||
FILE,
|
||||
OUTPUTSTREAM
|
||||
}
|
||||
|
||||
/*
|
||||
* MethodSource fields:
|
||||
* Type of the constructor, a file to be written with a charset name,
|
||||
* a file to be written with a charset, charset name, charset.
|
||||
*/
|
||||
public static Stream<Arguments> parameters() throws IOException {
|
||||
String csn = StandardCharsets.UTF_8.name();
|
||||
Charset charset = StandardCharsets.UTF_8;
|
||||
File file1 = new File(USER_DIR, "PSCharsetTest1.txt");
|
||||
File file2 = new File(USER_DIR, "PSCharsetTest2.txt");
|
||||
|
||||
return Stream.of
|
||||
(Arguments.of(ConstructorType.STRING, file1, file2, csn, charset),
|
||||
Arguments.of(ConstructorType.FILE, file1, file2, csn, charset),
|
||||
Arguments.of(ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the overloading constructor behaves the same as the existing
|
||||
* one.
|
||||
* @param type the type of the constructor
|
||||
* @param file1 file1 written with the name of a charset
|
||||
* @param file2 file2 written with a charset
|
||||
* @param csn the charset name
|
||||
* @param charset the charset
|
||||
* @throws IOException
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("parameters")
|
||||
public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
|
||||
throws Exception {
|
||||
createFile(getPrintStream(type, file1.getPath(), csn, null));
|
||||
createFile(getPrintStream(type, file2.getPath(), null, charset));
|
||||
|
||||
assertEquals(Files.readAllLines(Paths.get(file2.getPath()), charset),
|
||||
Files.readAllLines(Paths.get(file1.getPath()), charset));
|
||||
}
|
||||
|
||||
public void createFile(PrintStream out) throws IOException {
|
||||
out.println("high surrogate");
|
||||
out.println(Character.MIN_HIGH_SURROGATE);
|
||||
out.println("low surrogate");
|
||||
out.println(Character.MIN_LOW_SURROGATE);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
PrintStream getPrintStream(ConstructorType type, String path, String csn, Charset charset)
|
||||
throws IOException {
|
||||
PrintStream out = null;
|
||||
if (csn != null) {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
out = new PrintStream(path, csn);
|
||||
break;
|
||||
case FILE:
|
||||
out = new PrintStream(new File(path), csn);
|
||||
break;
|
||||
case OUTPUTSTREAM:
|
||||
FileOutputStream fout = new FileOutputStream(path);
|
||||
out = new PrintStream(fout, AUTOFLUSH, csn);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (type) {
|
||||
case STRING:
|
||||
out = new PrintStream(path, charset);
|
||||
break;
|
||||
case FILE:
|
||||
out = new PrintStream(new File(path), charset);
|
||||
break;
|
||||
case OUTPUTSTREAM:
|
||||
FileOutputStream fout = new FileOutputStream(path);
|
||||
out = new PrintStream(fout, AUTOFLUSH, charset);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
134
test/jdk/java/io/PrintStream/FailingConstructors.java
Normal file
134
test/jdk/java/io/PrintStream/FailingConstructors.java
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2017, 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 7000511 8190577
|
||||
* @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
|
||||
* exception thrown
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class FailingConstructors {
|
||||
static final String fileName = "FailingConstructorsTest";
|
||||
static final String UNSUPPORTED_CHARSET = "unknownCharset";
|
||||
static final String FILE_CONTENTS = "This is a small file!";
|
||||
|
||||
private static void realMain(String[] args) throws Throwable {
|
||||
test(false, new File(fileName));
|
||||
|
||||
/* create the file and write its contents */
|
||||
Path path = Files.createTempFile(fileName, null);
|
||||
try {
|
||||
Files.write(path, FILE_CONTENTS.getBytes());
|
||||
test(true, path.toFile());
|
||||
} finally {
|
||||
Files.delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(boolean exists, File file) throws Throwable {
|
||||
/* PrintStream(File file, String csn) */
|
||||
try {
|
||||
new PrintStream(file, UNSUPPORTED_CHARSET);
|
||||
fail();
|
||||
} catch(FileNotFoundException|UnsupportedEncodingException e) {
|
||||
pass();
|
||||
}
|
||||
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new PrintStream(file, (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
}
|
||||
|
||||
check(exists, file);
|
||||
|
||||
/* PrintStream(String fileName, String csn) */
|
||||
try {
|
||||
new PrintStream(file.getName(), UNSUPPORTED_CHARSET);
|
||||
fail();
|
||||
} catch(FileNotFoundException|UnsupportedEncodingException e) {
|
||||
pass();
|
||||
}
|
||||
|
||||
check(exists, file);
|
||||
|
||||
try {
|
||||
new PrintStream(file.getName(), (String)null);
|
||||
fail();
|
||||
} catch(FileNotFoundException|NullPointerException e) {
|
||||
pass();
|
||||
}
|
||||
|
||||
check(exists, file);
|
||||
}
|
||||
|
||||
private static void check(boolean exists, File file) {
|
||||
if (exists) {
|
||||
/* the file should be unchanged */
|
||||
verifyContents(file);
|
||||
} else {
|
||||
/* the file should not have been created */
|
||||
if (file.exists()) { fail(file + " should not have been created"); }
|
||||
}
|
||||
}
|
||||
|
||||
private static void verifyContents(File file) {
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
byte[] contents = FILE_CONTENTS.getBytes();
|
||||
int read, count = 0;
|
||||
while ((read = fis.read()) != -1) {
|
||||
if (read != contents[count++]) {
|
||||
fail("file contents have been altered");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
unexpected(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String message) {System.out.println(message); fail(); }
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
59
test/jdk/java/io/PrintStream/FormatLocale.java
Normal file
59
test/jdk/java/io/PrintStream/FormatLocale.java
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 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 8146156
|
||||
* @summary test whether conversion follows Locale.Category.FORMAT locale.
|
||||
* @modules jdk.localedata
|
||||
* @run main/othervm FormatLocale
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Locale.Category;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class FormatLocale {
|
||||
|
||||
static final float src = 3.14f;
|
||||
static final List<Locale> formatLocale = List.of(Locale.US, Locale.FRANCE);
|
||||
static final List<String> expected = List.of("3.14", "3,14");
|
||||
|
||||
public static void main(String [] args) {
|
||||
IntStream.range(0, formatLocale.size()).forEach(i -> {
|
||||
Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i));
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
new PrintStream(baos).format("%.2f", src);
|
||||
if (!baos.toString().equals(expected.get(i))) {
|
||||
throw new RuntimeException(
|
||||
"Wrong conversion with PrintStream.format() in locale "
|
||||
+ formatLocale.get(i) +
|
||||
". Expected: " + expected.get(i) +
|
||||
" Returned: " + baos.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
109
test/jdk/java/io/PrintStream/InheritEncodingTest.java
Normal file
109
test/jdk/java/io/PrintStream/InheritEncodingTest.java
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 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.
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8276970
|
||||
* @summary Test to verify the charset in PrintStream is inherited
|
||||
* in the OutputStreamWriter/PrintWriter
|
||||
* @run junit InheritEncodingTest
|
||||
*/
|
||||
public class InheritEncodingTest {
|
||||
|
||||
private static final String testString = "\u00e9\u3042"; // "éあ"
|
||||
|
||||
public static Charset[] encodings() {
|
||||
return new Charset[]{
|
||||
StandardCharsets.ISO_8859_1,
|
||||
StandardCharsets.US_ASCII,
|
||||
StandardCharsets.UTF_8,
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16BE,
|
||||
StandardCharsets.UTF_16LE
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("encodings")
|
||||
public void testOutputStreamWriter(Charset stdCharset) throws IOException {
|
||||
var ba = new ByteArrayOutputStream();
|
||||
var ps = new PrintStream(ba, true, stdCharset);
|
||||
var expected = new String(testString.getBytes(stdCharset), stdCharset);
|
||||
|
||||
// tests OutputStreamWriter's encoding explicitly
|
||||
var osw = new OutputStreamWriter(ps);
|
||||
assertEquals(stdCharset, Charset.forName(osw.getEncoding()));
|
||||
|
||||
// tests roundtrip result
|
||||
osw.write(testString);
|
||||
osw.flush();
|
||||
var result = ba.toString(stdCharset);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("encodings")
|
||||
public void testPrintWriter(Charset stdCharset) throws IOException {
|
||||
var ba = new ByteArrayOutputStream();
|
||||
var ps = new PrintStream(ba, true, stdCharset);
|
||||
var expected = new String(testString.getBytes(stdCharset), stdCharset);
|
||||
|
||||
// tests roundtrip result
|
||||
var pw = new PrintWriter(ps);
|
||||
pw.write(testString);
|
||||
pw.flush();
|
||||
var result = ba.toString(stdCharset);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("encodings")
|
||||
public void testPrintStream(Charset stdCharset) throws IOException {
|
||||
var ba = new ByteArrayOutputStream();
|
||||
var ps = new PrintStream(ba, true, stdCharset);
|
||||
var expected = new String(testString.getBytes(stdCharset), stdCharset);
|
||||
|
||||
// tests PrintStream's charset explicitly
|
||||
var psWrapper = new PrintStream(ps);
|
||||
assertEquals(stdCharset, psWrapper.charset());
|
||||
|
||||
// tests roundtrip result
|
||||
psWrapper.print(testString);
|
||||
psWrapper.flush();
|
||||
var result = ba.toString(stdCharset);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
42
test/jdk/java/io/PrintStream/NullConstructor.java
Normal file
42
test/jdk/java/io/PrintStream/NullConstructor.java
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2003, 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 1265548
|
||||
@summary PrintStream should not accept a null output stream in its
|
||||
constructor
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class NullConstructor {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
try {
|
||||
PrintStream ps = new PrintStream((OutputStream) null);
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
throw new Exception("PrintStream does not catch null constructor");
|
||||
}
|
||||
}
|
||||
94
test/jdk/java/io/PrintStream/OversynchronizedTest.java
Normal file
94
test/jdk/java/io/PrintStream/OversynchronizedTest.java
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 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 4905777
|
||||
@summary PrintStream.println(Object) oversynchronized, can deadlock
|
||||
@key randomness
|
||||
*/
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class OversynchronizedTest extends Thread {
|
||||
private static TestObj testObj = new TestObj("This is a test.");
|
||||
private static int loopNum = 100;
|
||||
|
||||
public void run() {
|
||||
for(int i=0; i<loopNum; i++) {
|
||||
testObj.test();
|
||||
|
||||
//passing an object to System.out.println might cause deadlock
|
||||
//if the object has a synchronized toString() method.
|
||||
//using System.out.println(testObj.toString()) won't have a problem
|
||||
System.out.println(testObj);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
// should no NullPointerException
|
||||
System.out.println((Object)null);
|
||||
|
||||
// over synch test
|
||||
int num = 5;
|
||||
|
||||
OversynchronizedTest[] t = new OversynchronizedTest[num];
|
||||
for(int i=0; i<num; i++) {
|
||||
t[i] = new OversynchronizedTest();
|
||||
t[i].start();
|
||||
}
|
||||
|
||||
for(int i=0; i <num; i++) {
|
||||
t[i].join();
|
||||
}
|
||||
|
||||
System.out.println("Test completed.");
|
||||
}
|
||||
}
|
||||
|
||||
class TestObj {
|
||||
String mStr;
|
||||
|
||||
TestObj(String str) {
|
||||
mStr = str;
|
||||
}
|
||||
|
||||
synchronized void test() {
|
||||
try {
|
||||
long t = Math.round(Math.random()*10);
|
||||
Thread.currentThread().sleep(t);
|
||||
} catch (InterruptedException e) {
|
||||
// jtreg timeout?
|
||||
// Only jtreg will interrupt this thread so it knows what to do:
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//the following line might cause hang if there is System.out.println(testObj)
|
||||
//called by other threads.
|
||||
System.out.println("In test().");
|
||||
}
|
||||
|
||||
public synchronized String toString() {
|
||||
System.out.println("Calling toString\n");
|
||||
return mStr;
|
||||
}
|
||||
}
|
||||
76
test/jdk/java/io/PrintStream/WriteBytes.java
Normal file
76
test/jdk/java/io/PrintStream/WriteBytes.java
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 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 8187898
|
||||
* @summary Test of writeBytes(byte[])
|
||||
*/
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class WriteBytes {
|
||||
public static void main(String[] args) throws Exception {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
OutputStream out = new BufferedOutputStream(baos, 512);
|
||||
PrintStream ps = new PrintStream(out, false);
|
||||
|
||||
byte[] buf = new byte[128];
|
||||
for (int i = 0; i < buf.length; i++) {
|
||||
buf[i] = (byte)i;
|
||||
}
|
||||
|
||||
ps.writeBytes(buf);
|
||||
assertTrue(baos.size() == 0, "Buffer should not have been flushed");
|
||||
ps.close();
|
||||
assertTrue(baos.size() == buf.length, "Stream size " + baos.size() +
|
||||
" but expected " + buf.length);
|
||||
|
||||
ps = new PrintStream(out, true);
|
||||
ps.writeBytes(buf);
|
||||
assertTrue(baos.size() == 2*buf.length, "Stream size " + baos.size() +
|
||||
" but expected " + 2*buf.length);
|
||||
|
||||
byte[] arr = baos.toByteArray();
|
||||
assertTrue(arr.length == 2*buf.length, "Array length " + arr.length +
|
||||
" but expected " + 2*buf.length);
|
||||
assertTrue(Arrays.equals(buf, 0, buf.length, arr, 0, buf.length),
|
||||
"First write not equal");
|
||||
assertTrue(Arrays.equals(buf, 0, buf.length, arr, buf.length,
|
||||
2*buf.length), "Second write not equal");
|
||||
|
||||
ps.close();
|
||||
ps.writeBytes(buf);
|
||||
assertTrue(ps.checkError(), "Error condition should be true");
|
||||
}
|
||||
|
||||
private static void assertTrue(boolean condition, String msg) {
|
||||
if (!condition)
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue