6218397: Printing to file does not throw a PrinterException if the file cannot be created
authorpsadhukhan
Fri, 01 Jul 2016 10:54:18 +0530
changeset 39544 263c59f6ca01
parent 39543 f382e19d6c63
child 39545 1b92907cf159
6218397: Printing to file does not throw a PrinterException if the file cannot be created Reviewed-by: prr, jdv
jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java
jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java
--- 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);
         }