8212202: [Windows] Exception if no printers are installed.
authorsveerabhadra
Mon, 25 Feb 2019 11:02:53 +0530
changeset 53936 03163eb3b2d4
parent 53935 49b74aa7d2e3
child 53937 a986e16d8449
8212202: [Windows] Exception if no printers are installed. Reviewed-by: prr
src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java
src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp
test/jdk/java/awt/print/RemotePrinterStatusRefresh/RemotePrinterStatusRefresh.java
--- a/src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java	Thu Feb 21 12:27:39 2019 +0530
+++ b/src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java	Mon Feb 25 11:02:53 2019 +0530
@@ -403,18 +403,36 @@
        list.
     */
     class RemotePrinterChangeListener implements Runnable {
-        private String[] prevRemotePrinters;
+        private String[] prevRemotePrinters = null;
 
         RemotePrinterChangeListener() {
             prevRemotePrinters = getRemotePrintersNames();
         }
 
         boolean doCompare(String[] str1, String[] str2) {
+            if (str1 == null && str2 == null) {
+                return false;
+            } else if (str1 == null || str2 == null) {
+                return true;
+            }
+
             if (str1.length != str2.length) {
                 return true;
             } else {
                 for (int i = 0;i < str1.length;i++) {
                     for (int j = 0;j < str2.length;j++) {
+                        // skip if both are nulls
+                        if (str1[i] == null && str2[j] == null) {
+                            continue;
+                        }
+
+                        // return true if there is a 'difference' but
+                        // no need to access the individual string
+                        if (str1[i] == null || str2[j] == null) {
+                            return true;
+                        }
+
+                        // do comparison only if they are non-nulls
                         if (!str1[i].equals(str2[j])) {
                             return true;
                         }
@@ -428,15 +446,19 @@
         @Override
         public void run() {
             while (true) {
-                String[] currentRemotePrinters = getRemotePrintersNames();
-                if (doCompare(prevRemotePrinters, currentRemotePrinters)) {
+                if (prevRemotePrinters != null && prevRemotePrinters.length > 0) {
+                    String[] currentRemotePrinters = getRemotePrintersNames();
+                    if (doCompare(prevRemotePrinters, currentRemotePrinters)) {
 
-                    // updated the printers data
-                    // printers list now contains both local and network printer data
-                    refreshServices();
+                        // updated the printers data
+                        // printers list now contains both local and network printer data
+                        refreshServices();
 
-                    // store the current data for next comparison
-                    prevRemotePrinters = currentRemotePrinters;
+                        // store the current data for next comparison
+                        prevRemotePrinters = currentRemotePrinters;
+                    }
+                } else {
+                    prevRemotePrinters = getRemotePrintersNames();
                 }
 
                 try {
--- a/src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp	Thu Feb 21 12:27:39 2019 +0530
+++ b/src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp	Mon Feb 25 11:02:53 2019 +0530
@@ -249,7 +249,7 @@
     if (clazz == NULL) {
         return NULL;
     }
-    jobjectArray nameArray;
+    jobjectArray nameArray = NULL;
 
     try {
         ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
@@ -270,13 +270,14 @@
                 }
             }
 
-            // Allocate space only for the network type printers
-            nameArray = env->NewObjectArray(remotePrintersCount, clazz, NULL);
-            if (nameArray == NULL) {
-                throw std::bad_alloc();
+            // return remote printers only if the list contains it.
+            if (remotePrintersCount > 0) {
+                // Allocate space only for the network type printers
+                nameArray = env->NewObjectArray(remotePrintersCount, clazz, NULL);
+                if (nameArray == NULL) {
+                    throw std::bad_alloc();
+                }
             }
-        } else {
-            nameArray = NULL;
         }
 
         // Loop thro' network printers list only
@@ -298,7 +299,12 @@
 
     delete [] pPrinterEnum;
     delete [] pNetworkPrinterLoc;
-    return nameArray;
+
+    if (nameArray != NULL) {
+      return nameArray;
+    } else {
+      return env->NewObjectArray(0, clazz, NULL);
+    }
 
     CATCH_BAD_ALLOC_RET(NULL);
 }
--- a/test/jdk/java/awt/print/RemotePrinterStatusRefresh/RemotePrinterStatusRefresh.java	Thu Feb 21 12:27:39 2019 +0530
+++ b/test/jdk/java/awt/print/RemotePrinterStatusRefresh/RemotePrinterStatusRefresh.java	Mon Feb 25 11:02:53 2019 +0530
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8153732
+ * @bug 8153732 8212202
  * @requires (os.family == "Windows")
  * @summary Windows remote printer changes do not reflect in lookupPrintServices()
  * @ignore Requires a new network printer installation\removal