src/share/classes/sun/print/PSPrinterJob.java

Print this page
rev 4570 : [NEW BUG]: Printer spoolers ignore result from spool process
Summary: Add checking, exception for spool process failure
Contributed-by: ngmr

*** 66,83 **** --- 66,87 ---- import javax.print.attribute.standard.JobName; import javax.print.attribute.standard.Sides; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; + import java.io.BufferedReader; import java.io.CharConversionException; import java.io.File; import java.io.InputStream; + import java.io.InputStreamReader; import java.io.IOException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.PrintStream; + import java.io.PrintWriter; + import java.io.StringWriter; import java.util.ArrayList; import java.util.Enumeration; import java.util.Locale; import java.util.Properties;
*** 671,702 **** // Inner class to run "privileged" to invoke the system print command private class PrinterSpooler implements java.security.PrivilegedAction { PrinterException pex; public Object run() { - try { - /** - * Spool to the printer. - */ if (spoolFile == null || !spoolFile.exists()) { pex = new PrinterException("No spool file"); return null; } String fileName = spoolFile.getAbsolutePath(); String execCmd[] = printExecCmd(mDestination, mOptions, mNoJobSheet, getJobNameInt(), 1, fileName); Process process = Runtime.getRuntime().exec(execCmd); process.waitFor(); ! spoolFile.delete(); ! } catch (IOException ex) { pex = new PrinterIOException(ex); } catch (InterruptedException ie) { pex = new PrinterException(ie.toString()); } return null; } } --- 675,733 ---- // Inner class to run "privileged" to invoke the system print command private class PrinterSpooler implements java.security.PrivilegedAction { PrinterException pex; + private void handleProcessFailure(final Process failedProcess, + final String[] execCmd, final int result) throws IOException { + try (StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw)) { + pw.append("error=").append(Integer.toString(result)); + pw.append(" running:"); + for (String arg: execCmd) { + pw.append(" '").append(arg).append("'"); + } + try (InputStream is = failedProcess.getErrorStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr)) { + while (br.ready()) { + pw.println(); + pw.append("\t\t").append(br.readLine()); + } + } finally { + pw.flush(); + throw new IOException(sw.toString()); + } + } + } + public Object run() { if (spoolFile == null || !spoolFile.exists()) { pex = new PrinterException("No spool file"); return null; } + try { + /** + * Spool to the printer. + */ String fileName = spoolFile.getAbsolutePath(); String execCmd[] = printExecCmd(mDestination, mOptions, mNoJobSheet, getJobNameInt(), 1, fileName); Process process = Runtime.getRuntime().exec(execCmd); process.waitFor(); ! final int result = process.exitValue(); ! if (0 != result) { ! handleProcessFailure(process, execCmd, result); ! } } catch (IOException ex) { pex = new PrinterIOException(ex); } catch (InterruptedException ie) { pex = new PrinterException(ie.toString()); + } finally { + spoolFile.delete(); } return null; } }