diff --git a/src/qz/printer/info/CachedPrintService.java b/src/qz/printer/info/CachedPrintService.java index f548e1e1b..611eed2e2 100644 --- a/src/qz/printer/info/CachedPrintService.java +++ b/src/qz/printer/info/CachedPrintService.java @@ -20,7 +20,7 @@ * See also JDK-7001133 */ public class CachedPrintService implements PrintService { - private PrintService printService; + private final PrintService printService; private final long lifespan; private final CachedObject cachedName; private final CachedObject cachedAttributeSet; @@ -62,6 +62,10 @@ public PrintServiceAttributeSet getAttributes() { return cachedAttributeSet.get(); } + public PrintService getJavaxPrintService() { + return printService; + } + @Override public T getAttribute(Class category) { if (!cachedAttributes.containsKey(category)) { diff --git a/src/qz/printer/info/CachedPrintServiceLookup.java b/src/qz/printer/info/CachedPrintServiceLookup.java index 28d428ffd..c8cff6aab 100644 --- a/src/qz/printer/info/CachedPrintServiceLookup.java +++ b/src/qz/printer/info/CachedPrintServiceLookup.java @@ -11,8 +11,9 @@ * See also CachedPrintService */ public class CachedPrintServiceLookup { - private static final CachedObject cachedDefault = new CachedObject<>(CachedPrintServiceLookup::innerLookupDefaultPrintService); - private static final CachedObject cachedPrintServices = new CachedObject<>(CachedPrintServiceLookup::innerLookupPrintServices); + private static final CachedObject cachedDefault = new CachedObject<>(CachedPrintServiceLookup::wrapDefaultPrintService); + private static final CachedObject cachedPrintServices = new CachedObject<>(CachedPrintServiceLookup::wrapPrintServices); + private static CachedPrintService[] oldPrintServices = {}; static { setLifespan(CachedObject.DEFAULT_LIFESPAN); @@ -31,15 +32,32 @@ public static PrintService[] lookupPrintServices() { return cachedPrintServices.get(); } - private static PrintService innerLookupDefaultPrintService() { + private static PrintService wrapDefaultPrintService() { + PrintService javaxPrintService = PrintServiceLookup.lookupDefaultPrintService(); + // If this CachedPrintService already exists, reuse it rather than wrapping a new one + CachedPrintService oldCachedPrintService = getMatch(oldPrintServices, javaxPrintService); + if (oldCachedPrintService != null) return oldCachedPrintService; return new CachedPrintService(PrintServiceLookup.lookupDefaultPrintService()); } - private static PrintService[] innerLookupPrintServices() { - PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); - for (int i = 0; i < printServices.length; i++) { - printServices[i] = new CachedPrintService(printServices[i]); + private static CachedPrintService[] wrapPrintServices() { + PrintService[] javaxPrintServices = PrintServiceLookup.lookupPrintServices(null, null); + CachedPrintService[] cachedPrintServices = new CachedPrintService[javaxPrintServices.length]; + for (int i = 0; i < javaxPrintServices.length; i++) { + // If this CachedPrintService already exists, reuse it rather than wrapping a new one + cachedPrintServices[i] = getMatch(oldPrintServices, javaxPrintServices[i]); + if (cachedPrintServices[i] == null) { + cachedPrintServices[i] = new CachedPrintService(javaxPrintServices[i]); + } } - return printServices; + oldPrintServices = cachedPrintServices; + return cachedPrintServices; + } + + private static CachedPrintService getMatch(CachedPrintService[] array, PrintService javaxPrintService) { + for (CachedPrintService cps : array) { + if (cps.getJavaxPrintService() == javaxPrintService) return cps; + } + return null; } }