< prev index next >
src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
Print this page
rev 53038 : 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -27,49 +27,59 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
-import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
-import java.nio.channels.*;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.channels.WritableByteChannel;
import java.nio.file.*;
-import java.nio.file.attribute.*;
-import java.nio.file.spi.*;
+import java.nio.file.attribute.FileAttribute;
+import java.nio.file.attribute.FileTime;
+import java.nio.file.attribute.UserPrincipalLookupService;
+import java.nio.file.spi.FileSystemProvider;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;
import java.util.zip.CRC32;
-import java.util.zip.Inflater;
import java.util.zip.Deflater;
-import java.util.zip.InflaterInputStream;
import java.util.zip.DeflaterOutputStream;
+import java.util.zip.Inflater;
+import java.util.zip.InflaterInputStream;
import java.util.zip.ZipException;
-import static java.lang.Boolean.*;
+
+import static java.lang.Boolean.TRUE;
+import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+import static java.nio.file.StandardOpenOption.APPEND;
+import static java.nio.file.StandardOpenOption.CREATE;
+import static java.nio.file.StandardOpenOption.CREATE_NEW;
+import static java.nio.file.StandardOpenOption.READ;
+import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
+import static java.nio.file.StandardOpenOption.WRITE;
import static jdk.nio.zipfs.ZipConstants.*;
import static jdk.nio.zipfs.ZipUtils.*;
-import static java.nio.file.StandardOpenOption.*;
-import static java.nio.file.StandardCopyOption.*;
/**
* A FileSystem built on a zip file
*
* @author Xueming Shen
*/
-
class ZipFileSystem extends FileSystem {
-
private final ZipFileSystemProvider provider;
private final Path zfpath;
final ZipCoder zc;
private final ZipPath rootdir;
private boolean readOnly = false; // readonly file system
@@ -77,19 +87,19 @@
// configurable by env map
private final boolean noExtt; // see readExtra()
private final boolean useTempFile; // use a temp file for newOS, default
// is to use BAOS for better performance
private static final boolean isWindows = AccessController.doPrivileged(
- (PrivilegedAction<Boolean>) () -> System.getProperty("os.name")
- .startsWith("Windows"));
+ (PrivilegedAction<Boolean>)() -> System.getProperty("os.name")
+ .startsWith("Windows"));
private final boolean forceEnd64;
private final int defaultMethod; // METHOD_STORED if "noCompression=true"
// METHOD_DEFLATED otherwise
ZipFileSystem(ZipFileSystemProvider provider,
Path zfpath,
- Map<String, ?> env) throws IOException
+ Map<String, ?> env) throws IOException
{
// default encoding for name/comment
String nameEncoding = env.containsKey("encoding") ?
(String)env.get("encoding") : "UTF-8";
this.noExtt = "false".equals(env.get("zipinfo-time"));
@@ -267,16 +277,16 @@
} finally {
endWrite();
}
if (!streams.isEmpty()) { // unlock and close all remaining streams
Set<InputStream> copy = new HashSet<>(streams);
- for (InputStream is: copy)
+ for (InputStream is : copy)
is.close();
}
beginWrite(); // lock and sync
try {
- AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
+ AccessController.doPrivileged((PrivilegedExceptionAction<Void>)() -> {
sync(); return null;
});
ch.close(); // close the ch just in case no update
// and sync didn't close the ch
} catch (PrivilegedActionException e) {
@@ -294,11 +304,11 @@
def.end();
}
IOException ioe = null;
synchronized (tmppaths) {
- for (Path p: tmppaths) {
+ for (Path p : tmppaths) {
try {
AccessController.doPrivileged(
(PrivilegedExceptionAction<Boolean>)() -> Files.deleteIfExists(p));
} catch (PrivilegedActionException e) {
IOException x = (IOException)e.getException();
@@ -519,11 +529,11 @@
checkWritable();
boolean hasCreateNew = false;
boolean hasCreate = false;
boolean hasAppend = false;
boolean hasTruncate = false;
- for (OpenOption opt: options) {
+ for (OpenOption opt : options) {
if (opt == READ)
throw new IllegalArgumentException("READ not allowed");
if (opt == CREATE_NEW)
hasCreateNew = true;
if (opt == CREATE)
@@ -1453,10 +1463,11 @@
isClosed = true;
finish();
e.size = def.getBytesRead();
e.csize = def.getBytesWritten();
e.crc = crc.getValue();
+ releaseDeflater(def);
}
}
private InputStream getInputStream(Entry e)
throws IOException
@@ -1475,11 +1486,11 @@
// FILECH result is un-compressed.
eis = Files.newInputStream(e.file);
// TBD: wrap to hook close()
// streams.add(eis);
return eis;
- } else { // untouced CEN or COPY
+ } else { // untouched CEN or COPY
eis = new EntryInputStream(e, ch);
}
if (e.method == METHOD_DEFLATED) {
// MORE: Compute good size for inflater stream:
long bufSize = e.size + 2; // Inflater likes a bit of slack
@@ -1537,18 +1548,16 @@
private class EntryInputStream extends InputStream {
private final SeekableByteChannel zfch; // local ref to zipfs's "ch". zipfs.ch might
// point to a new channel after sync()
private long pos; // current position within entry data
protected long rem; // number of remaining bytes within entry
- protected final long size; // uncompressed size of this entry
EntryInputStream(Entry e, SeekableByteChannel zfch)
throws IOException
{
this.zfch = zfch;
rem = e.csize;
- size = e.size;
pos = e.locoff;
if (pos == -1) {
Entry e2 = getEntry(e.name);
if (e2 == null) {
throw new ZipException("invalid loc for entry <" + e.name + ">");
@@ -1611,14 +1620,10 @@
public int available() {
return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
}
- public long size() {
- return size;
- }
-
public void close() {
rem = 0;
streams.remove(this);
}
@@ -1670,11 +1675,11 @@
}
// List of available Deflater objects for compression
private final List<Deflater> deflaters = new ArrayList<>();
- // Gets an deflater from the list of available deflaters or allocates
+ // Gets a deflater from the list of available deflaters or allocates
// a new one.
private Deflater getDeflater() {
synchronized (deflaters) {
int size = deflaters.size();
if (size > 0) {
@@ -1983,13 +1988,11 @@
comment = Arrays.copyOfRange(cen, pos, pos + clen);
}
return this;
}
- int writeCEN(OutputStream os) throws IOException
- {
- int written = CENHDR;
+ int writeCEN(OutputStream os) throws IOException {
int version0 = version();
long csize0 = csize;
long size0 = size;
long locoff0 = locoff;
int elen64 = 0; // extra for ZIP64
@@ -2099,23 +2102,22 @@
}
///////////////////// LOC //////////////////////
int writeLOC(OutputStream os) throws IOException {
- writeInt(os, LOCSIG); // LOC header signature
- int version = version();
-
+ int version0 = version();
byte[] zname = isdir ? toDirectoryPath(name) : name;
int nlen = (zname != null) ? zname.length - 1 : 0; // [0] is slash
int elen = (extra != null) ? extra.length : 0;
boolean foundExtraTime = false; // if extra timestamp present
int eoff = 0;
int elen64 = 0;
int elenEXTT = 0;
int elenNTFS = 0;
+ writeInt(os, LOCSIG); // LOC header signature
if ((flag & FLAG_DATADESCR) != 0) {
- writeShort(os, version()); // version needed to extract
+ writeShort(os, version0); // version needed to extract
writeShort(os, flag); // general purpose bit flag
writeShort(os, method); // compression method
// last modification time
writeInt(os, (int)javaToDosTime(mtime));
// store size, uncompressed size, and crc-32 in data descriptor
@@ -2126,11 +2128,11 @@
} else {
if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
elen64 = 20; //headid(2) + size(2) + size(8) + csize(8)
writeShort(os, 45); // ver 4.5 for zip64
} else {
- writeShort(os, version()); // version needed to extract
+ writeShort(os, version0); // version needed to extract
}
writeShort(os, flag); // general purpose bit flag
writeShort(os, method); // compression method
// last modification time
writeInt(os, (int)javaToDosTime(mtime));
@@ -2428,11 +2430,10 @@
// directory entry
// (2) all entries are not stored/organized in a "tree"
// structure.
// A possible solution is to build the node tree ourself as
// implemented below.
- private IndexNode root;
// default time stamp for pseudo entries
private long zfsDefaultTimeStamp = System.currentTimeMillis();
private void removeFromTree(IndexNode inode) {
< prev index next >