From fa55ad2d91b379bc695257645accac69e9065f28 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Fri, 1 Jul 2016 10:54:18 +0530 Subject: [PATCH] 6218397: Printing to file does not throw a PrinterException if the file cannot be created Reviewed-by: prr, jdv --- .../java.desktop/share/classes/sun/print/PSPrinterJob.java | 7 ++++++- .../share/classes/sun/print/RasterPrinterJob.java | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java b/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java index deada0605ce..511fd3a6195 100644 --- a/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java +++ b/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java @@ -501,7 +501,7 @@ public class PSPrinterJob extends RasterPrinterJob { // 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 @@ public class PSPrinterJob extends RasterPrinterJob { spoolFile = new File(mDestination); output = new FileOutputStream(spoolFile); } catch (IOException ex) { + abortDoc(); throw new PrinterIOException(ex); } } else { @@ -771,6 +772,10 @@ public class PSPrinterJob extends RasterPrinterJob { 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(); } diff --git a/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java b/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java index c34561c5f65..cd6793a8786 100644 --- a/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java +++ b/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java @@ -1627,6 +1627,9 @@ public abstract class RasterPrinterJob extends PrinterJob { (!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); }