aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/jarjar/jarjar-core/src/main/java/com/tonicsystems/jarjar/classpath/ClassPathArchive.java
blob: 9c3a4bf47089438b97acd31c555e2758e8a4a6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tonicsystems.jarjar.classpath;

import com.tonicsystems.jarjar.util.ClassNameUtils;
import com.tonicsystems.jarjar.util.RuntimeIOException;
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.annotation.Nonnull;

/**
 *
 * @author shevek
 */
public abstract class ClassPathArchive implements Iterable<ClassPathResource> {

    protected final File root;

    public ClassPathArchive(@Nonnull File root) {
        this.root = root;
    }

    @Nonnull
    public String getArchiveName() {
        return root.getPath();
    }

    public static class ZipArchive extends ClassPathArchive {

        public ZipArchive(@Nonnull File root) {
            super(root);
        }

        @Override
        public Iterator<ClassPathResource> iterator() {
            try {
                return new ZipIterator(root);
            } catch (IOException e) {
                throw new RuntimeIOException(e);
            }
        }

    }

    private static class ZipIterator implements Iterator<ClassPathResource>, Closeable {

        private final ZipFile zipFile;
        private Enumeration<? extends ZipEntry> zipEntries;

        ZipIterator(@Nonnull File file) throws IOException {
            this.zipFile = new ZipFile(file);
            this.zipEntries = zipFile.entries();
        }

        @Override
        public boolean hasNext() {
            if (!zipEntries.hasMoreElements()) {
                // close();
                return false;
            } else {
                return true;
            }
        }

        @Override
        public ClassPathResource next() {
            final ZipEntry entry = zipEntries.nextElement();
            if (entry == null)
                throw new NoSuchElementException();
            return new ClassPathResource() {
                @Override
                public String getArchiveName() {
                    return zipFile.getName();
                }

                @Override
                public String getName() {
                    return entry.getName();
                }

                @Override
                public long getLastModifiedTime() {
                    return entry.getTime();
                }

                @Override
                public InputStream openStream() throws IOException {
                    return zipFile.getInputStream(entry);
                }
            };
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void close() throws IOException {
            zipFile.close();
        }
    }

    public static class DirectoryArchive extends ClassPathArchive {

        public DirectoryArchive(@Nonnull File root) {
            super(root);
        }

        @Override
        public Iterator<ClassPathResource> iterator() {
            return new DirectoryIterator(root);
        }

    }

    private static class DirectoryIterator implements Iterator<ClassPathResource> {

        @Nonnull
        private static void findClassFiles(@Nonnull Collection<? super File> out, @Nonnull File dir) {
            for (File file : dir.listFiles()) {
                if (file.isDirectory()) {
                    findClassFiles(out, file);
                } else if (file.isFile()) {
                    if (ClassNameUtils.isClass(file.getName())) {
                        out.add(file);
                    }
                }
            }
        }

        private final File root;
        private final Iterator<File> entries;

        DirectoryIterator(@Nonnull File root) {
            this.root = root;
            List<File> files = new ArrayList<File>();
            findClassFiles(files, root);
            this.entries = files.iterator();
        }

        @Override
        public boolean hasNext() {
            return entries.hasNext();
        }

        @Override
        public ClassPathResource next() {
            final File file = entries.next();
            return new ClassPathResource() {
                @Override
                public String getArchiveName() {
                    return root.getPath();
                }

                @Override
                public String getName() {
                    return file.getName();
                }

                @Override
                public long getLastModifiedTime() {
                    return file.lastModified();
                }

                @Override
                public InputStream openStream() throws IOException {
                    return new BufferedInputStream(new FileInputStream(file));
                }
            };
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "(" + root + ")";
    }

}