6218397: Printing to file does not throw a PrinterException if the file cannot be created
Reviewed-by: prr, jdv
--- a/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java Thu Jun 30 23:26:42 2016 -0500
+++ b/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java Fri Jul 01 10:54:18 2016 +0530
@@ -501,7 +501,7 @@
// Note that we only open a file if it has been nominated by
// the end-user in a dialog that we ouselves put up.
- OutputStream output;
+ OutputStream output = null;
if (epsPrinter == null) {
if (getPrintService() instanceof PSStreamPrintService) {
@@ -526,6 +526,7 @@
spoolFile = new File(mDestination);
output = new FileOutputStream(spoolFile);
} catch (IOException ex) {
+ abortDoc();
throw new PrinterIOException(ex);
}
} else {
@@ -771,6 +772,10 @@
if (mPSStream != null) {
mPSStream.println(EOF_COMMENT);
mPSStream.flush();
+ if (mPSStream.checkError()) {
+ abortDoc();
+ throw new PrinterException("Error while writing to file");
+ }
if (mDestType != RasterPrinterJob.STREAM) {
mPSStream.close();
}
--- a/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java Thu Jun 30 23:26:42 2016 -0500
+++ b/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java Fri Jul 01 10:54:18 2016 +0530
@@ -1627,6 +1627,9 @@
(!f.isFile() || !f.canWrite())) ||
((pFile != null) &&
(!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
+ if (f.exists()) {
+ f.delete();
+ }
throw new PrinterException("Cannot write to file:"+
dest);
}