jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystem.java
changeset 10365 8a89ee820687
parent 9035 1255eb81cc2f
child 10373 d4c5e59b82f8
--- a/jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystem.java	Wed Aug 17 15:18:16 2011 -0700
+++ b/jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileSystem.java	Fri Aug 26 15:36:21 2011 -0700
@@ -31,6 +31,7 @@
 
 package com.sun.nio.zipfs;
 
+import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.EOFException;
@@ -1165,7 +1166,6 @@
     // sync the zip file system, if there is any udpate
     private void sync() throws IOException {
         //System.out.printf("->sync(%s) starting....!%n", toString());
-
         // check ex-closer
         if (!exChClosers.isEmpty()) {
             for (ExChannelCloser ecc : exChClosers) {
@@ -1179,84 +1179,84 @@
         if (!hasUpdate)
             return;
         Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
-        OutputStream os = Files.newOutputStream(tmpFile, WRITE);
-        ArrayList<Entry> elist = new ArrayList<>(inodes.size());
-        long written = 0;
-        byte[] buf = new byte[8192];
-        Entry e = null;
+        try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE)))
+        {
+            ArrayList<Entry> elist = new ArrayList<>(inodes.size());
+            long written = 0;
+            byte[] buf = new byte[8192];
+            Entry e = null;
 
-        // write loc
-        for (IndexNode inode : inodes.values()) {
-            if (inode instanceof Entry) {    // an updated inode
-                e = (Entry)inode;
-                try {
-                    if (e.type == Entry.COPY) {
-                        // entry copy: the only thing changed is the "name"
-                        // and "nlen" in LOC header, so we udpate/rewrite the
-                        // LOC in new file and simply copy the rest (data and
-                        // ext) without enflating/deflating from the old zip
-                        // file LOC entry.
-                        written += copyLOCEntry(e, true, os, written, buf);
-                    } else {                          // NEW, FILECH or CEN
-                        e.locoff = written;
-                        written += e.writeLOC(os);    // write loc header
-                        if (e.bytes != null) {        // in-memory, deflated
-                            os.write(e.bytes);        // already
-                            written += e.bytes.length;
-                        } else if (e.file != null) {  // tmp file
-                            try (InputStream is = Files.newInputStream(e.file)) {
-                                int n;
-                                if (e.type == Entry.NEW) {  // deflated already
-                                    while ((n = is.read(buf)) != -1) {
-                                        os.write(buf, 0, n);
-                                        written += n;
+            // write loc
+            for (IndexNode inode : inodes.values()) {
+                if (inode instanceof Entry) {    // an updated inode
+                    e = (Entry)inode;
+                    try {
+                        if (e.type == Entry.COPY) {
+                            // entry copy: the only thing changed is the "name"
+                            // and "nlen" in LOC header, so we udpate/rewrite the
+                            // LOC in new file and simply copy the rest (data and
+                            // ext) without enflating/deflating from the old zip
+                            // file LOC entry.
+                            written += copyLOCEntry(e, true, os, written, buf);
+                        } else {                          // NEW, FILECH or CEN
+                            e.locoff = written;
+                            written += e.writeLOC(os);    // write loc header
+                            if (e.bytes != null) {        // in-memory, deflated
+                                os.write(e.bytes);        // already
+                                written += e.bytes.length;
+                            } else if (e.file != null) {  // tmp file
+                                try (InputStream is = Files.newInputStream(e.file)) {
+                                    int n;
+                                    if (e.type == Entry.NEW) {  // deflated already
+                                        while ((n = is.read(buf)) != -1) {
+                                            os.write(buf, 0, n);
+                                            written += n;
+                                        }
+                                    } else if (e.type == Entry.FILECH) {
+                                        // the data are not deflated, use ZEOS
+                                        try (OutputStream os2 = new EntryOutputStream(e, os)) {
+                                            while ((n = is.read(buf)) != -1) {
+                                                os2.write(buf, 0, n);
+                                            }
+                                        }
+                                        written += e.csize;
+                                        if ((e.flag & FLAG_DATADESCR) != 0)
+                                            written += e.writeEXT(os);
                                     }
-                                } else if (e.type == Entry.FILECH) {
-                                    // the data are not deflated, use ZEOS
-                                    try (OutputStream os2 = new EntryOutputStream(e, os)) {
-                                        while ((n = is.read(buf)) != -1) {
-                                            os2.write(buf, 0, n);
-                                        }
-                                    }
-                                    written += e.csize;
-                                    if ((e.flag & FLAG_DATADESCR) != 0)
-                                        written += e.writeEXT(os);
                                 }
+                                Files.delete(e.file);
+                                tmppaths.remove(e.file);
+                            } else {
+                                // dir, 0-length data
                             }
-                            Files.delete(e.file);
-                            tmppaths.remove(e.file);
-                        } else {
-                            // dir, 0-length data
                         }
+                        elist.add(e);
+                    } catch (IOException x) {
+                        x.printStackTrace();    // skip any in-accurate entry
                     }
-                    elist.add(e);
-                } catch (IOException x) {
-                    x.printStackTrace();    // skip any in-accurate entry
-                }
-            } else {                        // unchanged inode
-                if (inode.pos == -1) {
-                    continue;               // pseudo directory node
-                }
-                e = Entry.readCEN(this, inode.pos);
-                try {
-                    written += copyLOCEntry(e, false, os, written, buf);
-                    elist.add(e);
-                } catch (IOException x) {
-                    x.printStackTrace();    // skip any wrong entry
+                } else {                        // unchanged inode
+                    if (inode.pos == -1) {
+                        continue;               // pseudo directory node
+                    }
+                    e = Entry.readCEN(this, inode.pos);
+                    try {
+                        written += copyLOCEntry(e, false, os, written, buf);
+                        elist.add(e);
+                    } catch (IOException x) {
+                        x.printStackTrace();    // skip any wrong entry
+                    }
                 }
             }
+
+            // now write back the cen and end table
+            end.cenoff = written;
+            for (Entry entry : elist) {
+                written += entry.writeCEN(os);
+            }
+            end.centot = elist.size();
+            end.cenlen = written - end.cenoff;
+            end.write(os, written);
         }
-
-        // now write back the cen and end table
-        end.cenoff = written;
-        for (Entry entry : elist) {
-            written += entry.writeCEN(os);
-        }
-        end.centot = elist.size();
-        end.cenlen = written - end.cenoff;
-        end.write(os, written);
-        os.close();
-
         if (!streams.isEmpty()) {
             //
             // TBD: ExChannelCloser should not be necessary if we only
@@ -1959,7 +1959,7 @@
             writeBytes(os, name);
             if (elen64 != 0) {
                 writeShort(os, EXTID_ZIP64);// Zip64 extra
-                writeShort(os, elen64);     // size of "this" extra block
+                writeShort(os, elen64 - 4); // size of "this" extra block
                 if (size0 == ZIP64_MINVAL)
                     writeLong(os, size);
                 if (csize0 == ZIP64_MINVAL)