langtools/src/share/classes/com/sun/tools/javac/zip/ZipFileIndexEntry.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 10 06bc494ca11e
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     1
/*
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     4
 *
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    10
 *
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    15
 * accompanied this code).
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    16
 *
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    20
 *
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    23
 * have any questions.
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    24
 */
1ebc7ce89018 6705945: com.sun.tools.javac.zip files do not have valid copyright
jjg
parents: 10
diff changeset
    25
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.zip;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
public final class ZipFileIndexEntry implements Comparable<ZipFileIndexEntry> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
    public static final ZipFileIndexEntry[] EMPTY_ARRAY = {};
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
    // Directory related
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
    String dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
    boolean isDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
    // File related
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
    String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    int offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    int size;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    int compressedSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    long javatime;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private int nativetime;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    public ZipFileIndexEntry(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        int separator = path.lastIndexOf(File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        if (separator == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
            dir = "".intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
            name = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            dir = path.substring(0, separator).intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
            name = path.substring(separator + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public ZipFileIndexEntry(String directory, String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        this.dir = directory.intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    public String getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        if (dir == null || dir.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        StringBuilder sb = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        sb.append(dir);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        sb.append(File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        sb.append(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public String getFileName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public long getLastModified() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        if (javatime == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                javatime = dosToJavaTime(nativetime);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        return javatime;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    // From java.util.zip
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    private static long dosToJavaTime(int nativetime) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        // Bootstrap build problems prevent me from using the code directly
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        // Convert the raw/native time to a long for now
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        return (long)nativetime;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    void setNativeTime(int natTime) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        nativetime = natTime;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public boolean isDirectory() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        return isDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public int compareTo(ZipFileIndexEntry other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        String otherD = other.dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        if (dir != otherD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            int c = dir.compareTo(otherD);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            if (c != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                return c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        return name.compareTo(other.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        return isDir ? ("Dir:" + dir + " : " + name) :
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            (dir + ":" + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
}