Warnings Cleanup in java.util.<various> (more from hack day)

Michael Barker mikeb01 at gmail.com
Fri Dec 2 11:24:29 PST 2011


Hi,

We had some late submissions from our hack day participants.  2 more
patches.  One (j.u.z.ZipEntry) is possibly a little more involved for
a warnings fix.  It removes a usage of deprecated Date APIs in favour
of Calendar.  However, the fix results in code that is very similar to
Apache Harmony implementation.  The other is a couple of fixes for
generics (j.u.j.Manifest).

Thanks to:
- Prasannaa

Regards,
Michael Barker
mikeb2701
-------------- next part --------------
diff -r 43a630f11af6 src/share/classes/java/util/jar/Manifest.java
--- a/src/share/classes/java/util/jar/Manifest.java	Wed Nov 30 13:11:16 2011 -0800
+++ b/src/share/classes/java/util/jar/Manifest.java	Fri Dec 02 19:15:58 2011 +0000
@@ -30,6 +30,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -51,7 +52,7 @@
     private Attributes attr = new Attributes();
 
     // manifest entries
-    private Map entries = new HashMap();
+    private Map<String, Attributes> entries = new HashMap<>();
 
     /**
      * Constructs a new, empty Manifest.
@@ -148,20 +149,20 @@
         // Write out the main attributes for the manifest
         attr.writeMain(dos);
         // Now write out the pre-entry attributes
-        Iterator it = entries.entrySet().iterator();
+        Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator();
         while (it.hasNext()) {
-            Map.Entry e = (Map.Entry)it.next();
+            Map.Entry<String, Attributes> e = it.next();
             StringBuffer buffer = new StringBuffer("Name: ");
-            String value = (String)e.getKey();
+            String value = e.getKey();
             if (value != null) {
-                byte[] vb = value.getBytes("UTF8");
-                value = new String(vb, 0, 0, vb.length);
+                byte[] vb = value.getBytes(StandardCharsets.UTF_8);
+                value = new String(vb, 0, 0, StandardCharsets.UTF_8);
             }
             buffer.append(value);
             buffer.append("\r\n");
             make72Safe(buffer);
             dos.writeBytes(buffer.toString());
-            ((Attributes)e.getValue()).write(dos);
+            e.getValue().write(dos);
         }
         dos.flush();
     }
-------------- next part --------------
diff -r 43a630f11af6 src/share/classes/java/util/zip/ZipEntry.java
--- a/src/share/classes/java/util/zip/ZipEntry.java	Wed Nov 30 13:11:16 2011 -0800
+++ b/src/share/classes/java/util/zip/ZipEntry.java	Fri Dec 02 19:12:54 2011 +0000
@@ -24,7 +24,7 @@
  */
 
 package java.util.zip;
-
+import java.util.Calendar;
 import java.util.Date;
 
 /**
@@ -281,27 +281,33 @@
      * Converts DOS time to Java time (number of milliseconds since epoch).
      */
     private static long dosToJavaTime(long dtime) {
-        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
-                          (int)(((dtime >> 21) & 0x0f) - 1),
-                          (int)((dtime >> 16) & 0x1f),
-                          (int)((dtime >> 11) & 0x1f),
-                          (int)((dtime >> 5) & 0x3f),
-                          (int)((dtime << 1) & 0x3e));
-        return d.getTime();
+	    Calendar cal = Calendar.getInstance();
+	    cal.set(Calendar.YEAR, (int) ((dtime >> 25) & 0x7f) + 1980);
+	    cal.set(Calendar.MONTH, (int) ((dtime >> 21) & 0x0f) - 1);
+	    cal.set(Calendar.DATE, (int) (dtime >> 16) & 0x1f);
+	    cal.set(Calendar.HOUR_OF_DAY, (int) (dtime >> 11) & 0x1f);
+	    cal.set(Calendar.MINUTE, (int) (dtime >> 5) & 0x3f);
+	    cal.set(Calendar.SECOND, (int) (dtime << 1) & 0x3e);
+	    return cal.getTimeInMillis();
     }
 
     /*
      * Converts Java time to DOS time.
      */
     private static long javaToDosTime(long time) {
-        Date d = new Date(time);
-        int year = d.getYear() + 1900;
+        Calendar cal = Calendar.getInstance();
+        cal.setTimeInMillis(time);
+        int year = cal.get(Calendar.YEAR);
         if (year < 1980) {
             return (1 << 21) | (1 << 16);
         }
-        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
-               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
-               d.getSeconds() >> 1;
+        int month = cal.get(Calendar.MONTH) + 1;
+        return (year - 1980) << 25
+                | month << 21
+                | cal.get(Calendar.DAY_OF_MONTH) << 16
+                | cal.get(Calendar.HOUR_OF_DAY) << 11
+                | cal.get(Calendar.MINUTE) << 5
+                | cal.get(Calendar.SECOND) >> 1;
     }
 
     /**


More information about the jdk8-dev mailing list