summaryrefslogtreecommitdiff
path: root/sources/scalac/util/AbstractFile.java
blob: 7069df868be1a4d1e7ac723fe85bfc2a5f6a2c26 (plain) (blame)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
**                                                                      **
** $Id$
\*                                                                      */

package scalac.util;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.jar.*;

public abstract class AbstractFile {

    /** separator
     */
    protected char separator = File.separatorChar;

    /** table of all opened jar-files
     */
    protected static Hashtable  opened = new Hashtable();

    /** get name of the file
     */
    public abstract String getName();

    /** get path of the file
     */
    public abstract String getPath();

    /** does the file exist?
     */
    public abstract boolean exists();

    /** is the file a directory?
     */
    public abstract boolean isDirectory();

    /** date file was last modified
     */
    public abstract long lastModified();

    /** read content of the file into a byte[] buffer
     */
    public abstract byte[] read() throws IOException;

    /** list contents of a directory
     */
    public abstract String[] list() throws IOException;

    /** open a new file
     */
    public abstract AbstractFile open(String name);

    /** return an input stream for the file
     */
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(read());
    }

    /** open file 'name' in directory 'dirname'
     */
    public static AbstractFile open(String dirname, String name) {
        AbstractFile res;
        if (dirname == null)
            res = new PlainFile(new File(name));
        else if (dirname.endsWith(".zip")) {
            AbstractFile dir = (AbstractFile)opened.get(dirname);
            if (dir == null) {
                dir = new ZipDir(new File(dirname));
                if (dir.isDirectory())
                    opened.put(dirname, dir);
            }
            res = (name == null) ? dir : dir.open(name);
        } else if (dirname.endsWith(".jar")) {
            AbstractFile dir = (AbstractFile)opened.get(dirname);
            if (dir == null) {
                dir = new JarArchive(new File(dirname));
                if (dir.isDirectory())
                    opened.put(dirname, dir);
            }
            res = (name == null) ? dir : dir.open(name);
        } else if (name == null)
            res = new PlainFile(new File(dirname));
        else
            res = new PlainFile(new File(dirname, name));
        if (!res.exists())
            res = null;
        return res;
    }

    /** create file given by a fully qualified name from root directory `outdir';
     *  create intermediate directories if they do not exist already
     */
    public static File create(File outdir, String name,
                              String suffix) throws IOException {
        int     start = 0;
        int     end = name.indexOf('.');
        while (end >= start) {
            outdir = new File(outdir, name.substring(start, end));
            if (!outdir.exists())
                outdir.mkdir();
            start = end + 1;
            end = name.indexOf('.', start);
        }
        return new File(outdir, name.substring(start) + suffix);
    }
}

class PlainFile extends AbstractFile {
    File f;

    PlainFile(File f) {
        this.f = f;
    }

    public String getName() {
        return f.getName();
    }

    public String getPath() {
        return f.getPath();
    }

    public boolean exists() {
        return f.exists();
    }

    public boolean isDirectory() {
        return f.isDirectory();
    }

    public long lastModified() {
	return f.lastModified();
    }

    public byte[] read() throws IOException {
        FileInputStream in = new FileInputStream(f);
        int rest = (int)f.length();
        byte[] buf = new byte[rest];
        do {
            int res = in.read(buf, buf.length - rest, rest);
            if (res == -1)
                throw new IOException("read error");
            rest -= res;
        } while (rest > 0);
        in.close();
        return buf;
    }

    public String[] list() throws IOException {
        File[] fs = f.listFiles();
        if (fs == null)
            return new String[0];
        String[] res = new String[fs.length];
        for (int i = 0; i < fs.length; i++) {
            res[i] = fs[i].getName();
            if (fs[i].isDirectory() &&
                !res[i].endsWith("/"))
                res[i] = res[i] + "/";
        }
        return res;
    }

    public AbstractFile open(String name) {
        return new PlainFile(new File(f, name));
    }
}

class ZippedFile extends AbstractFile {
    ZipDir      dir;
    ZipEntry    zipEntry;

    {
        separator = '/';
    }

    ZippedFile(ZipDir dir, String name) {
        this.dir = dir;
        if (dir.zipFile != null) {
            name = name.replace(File.separatorChar, separator);
            zipEntry = this.dir.zipFile.getEntry(name);
            if (zipEntry == null)
                zipEntry = this.dir.zipFile.getEntry(name + separator);
        }
    }

    public String getName() {
        return zipEntry.getName();
    }

    public String getPath() {
        return dir.getPath() + "(" + zipEntry.getName() + ")";
    }

    public boolean exists() {
        return (zipEntry != null);
    }

    public boolean isDirectory() {
        return zipEntry.isDirectory();
    }

    public long lastModified() {
	return zipEntry.getTime();
    }

    public byte[] read() throws IOException {
        InputStream in = dir.zipFile.getInputStream(zipEntry);
        int rest = (int)zipEntry.getSize();
        byte[] buf = new byte[rest];
        do {
            int res = in.read(buf, buf.length - rest, rest);
            if (res == -1)
                throw new IOException("read error");
            rest -= res;
        } while (rest > 0);
        in.close();
        return buf;
    }

    public String[] list() throws IOException {
        if (!isDirectory())
            throw new IOException("not a directory");
        return dir.list(zipEntry.getName());
    }

    public AbstractFile open(String name) {
        String  pathname = zipEntry.getName();
        return new ZippedFile(dir, pathname + name);
    }
}

class ZipDir extends AbstractFile {
    File f;
    ZipFile zipFile;

    {
        separator = '/';
    }

    ZipDir(File f) {
        this.f = f;
        try {
            zipFile = new ZipFile(f);
        } catch (ZipException e) {
        } catch (IOException e) {}
    }

    public String getName() {
        return f.getName();
    }

    public String getPath() {
        return f.getPath();
    }

    public boolean exists() {
        return (zipFile != null);
    }

    public boolean isDirectory() {
        return (zipFile != null);
    }

    public long lastModified() {
	return -1;
    }

    public byte[] read() throws IOException {
        throw new IOException("cannot read directory");
    }

    public String[] list(String prefix) {
        int n = 0;
        for (Enumeration enum = zipFile.entries(); enum.hasMoreElements();) {
            ZipEntry    e = (ZipEntry)enum.nextElement();
            if (e.getName().startsWith(prefix)) {
                String  candidate = e.getName().substring(prefix.length());
                if (candidate.indexOf(separator) < 0)
                    n++;
            }
        }
        String[] filenames = new String[n];
        n = 0;
        for (Enumeration enum = zipFile.entries(); enum.hasMoreElements();) {
            ZipEntry    e = (ZipEntry)enum.nextElement();
            if (e.getName().startsWith(prefix)) {
                String  candidate = e.getName().substring(prefix.length());
                if (candidate.indexOf(separator) < 0)
                    filenames[n++] = candidate;
            }
        }
        return filenames;
    }

    public String[] list() throws IOException {
        return list("");
    }

    public AbstractFile open(String name) {
        return new ZippedFile(this, name);
    }
}

final class JarArchive extends AbstractFile {
    File f;
    JarFile jarFile;
    HashMap entries;

    public final static String[] EMPTY = new String[0];


    JarArchive(File f) {
        try {
            jarFile = new JarFile(this.f = f);
        }
        catch (ZipException e) {}
        catch (IOException e) {}
    }

    public String getName() {
        return f.getName();
    }

    public String getPath() {
        return f.getPath();
    }

    public boolean exists() {
        return jarFile != null;
    }

    public boolean isDirectory() {
        return jarFile != null;
    }

    public long lastModified() {
	return -1;
    }

    public byte[] read() throws IOException {
        throw new IOException("cannot read archive");
    }

    private void load() {
        entries = new HashMap();
        if (jarFile == null)
            return;
        Enumeration enum = jarFile.entries();
        while (enum.hasMoreElements()) {
            String candidate = ((JarEntry)enum.nextElement()).getName();
            int i = candidate.indexOf('/');
            int j = 0;
            HashMap files = entries;
            while (i >= 0) {
                String dirname = candidate.substring(j, j = (i + 1));
                JarDirEntry dir = (JarDirEntry)files.get(dirname);
                if (dir == null)
                    files.put(dirname, dir = new JarDirEntry(
                        candidate.substring(0, j)));
                files = dir.entries;
                i = candidate.indexOf('/', j);
            }
            if (j < (candidate.length() - 1)) {
                String filename = candidate.substring(j);
                JarFileEntry file = (JarFileEntry)files.get(filename);
                if (file == null)
                    files.put(filename, new JarFileEntry(candidate));
            }
        }
    }

    public String[] list(String prefix) {
        prefix = prefix.replace(File.separatorChar, '/');
        if (entries == null)
            load();
        int i = prefix.indexOf('/');
        int j = 0;
        HashMap files = entries;
        while (i >= 0) {
            String dirname = prefix.substring(j, j = (i + 1));
            JarDirEntry dir = (JarDirEntry)files.get(dirname);
            if (dir == null)
                return EMPTY;
            files = dir.entries;
            i = prefix.indexOf('/', j);
        }
        if (j < (prefix.length() - 1)) {
            String filename = prefix.substring(j);
            return (files.get(filename) != null) ? new String[]{prefix}
                                                 : EMPTY;
        } else
            return (String[])files.keySet().toArray(new String[files.size()]);
    }

    public String[] list() throws IOException {
        return list("");
    }

    public AbstractFile open(String name) {
        if (entries == null)
            load();
        name = name.replace(File.separatorChar, '/');
        int i = name.indexOf('/');
        int j = 0;
        int namelen = name.length();
        HashMap files = entries;
        while (i >= 0) {
            String dirname = name.substring(j, j = (i + 1));
            if (files != null) {
                JarDirEntry dir = (JarDirEntry)files.get(dirname);
                if (dir == null)
                    files = null;
                else if (j == namelen)
                    return dir;
                else
                    files = dir.entries;
            }
            i = name.indexOf('/', j);
        }
        if (j < (namelen - 1)) {
            String filename = name.substring(j);
            if (files == null)
                return new NoJarFileEntry(name);
            JarFileEntry file = (JarFileEntry)files.get(filename);
            if (file == null)
                return new NoJarFileEntry(name);
            else
                return file;
        } else
            return new NoJarDirEntry(name);
    }

    static class NoJarDirEntry extends AbstractFile {
        String name;

        NoJarDirEntry(String name) {
            this.name = name;
        }

        public String getName() {
            return name.substring(
                name.lastIndexOf('/', name.length() - 2) + 1);
        }

        public String getPath() {
            return name;
        }

        public String getFullName() {
            return name;
        }

        public boolean exists() {
            return false;
        }

        public boolean isDirectory() {
            return true;
        }

	public long lastModified() {
	    return -1;
	}

        public String[] list() throws IOException {
            throw new IOException("not a directory");
        }

        public byte[] read() throws IOException {
            throw new IOException("cannot read archive");
        }

        public AbstractFile open(String fname) {
            throw new Error("cannot open archive entry");
        }
    }

    final class JarDirEntry extends NoJarDirEntry {
        HashMap entries;

        JarDirEntry(String name) {
            super(name);
            this.entries = new HashMap();
        }

        public String getPath() {
            return JarArchive.this.getPath() + "(" + name + ")";
        }

        public boolean exists() {
            return true;
        }

        public String[] list() throws IOException {
            return JarArchive.this.list(name);
        }

        public AbstractFile open(String fname) {
            fname = fname.replace(File.separatorChar, '/');
            return JarArchive.this.open(name + fname);
        }
    }

    static class NoJarFileEntry extends AbstractFile {
        String name;

        NoJarFileEntry(String name) {
            this.name = name;
        }

        public String getName() {
            return name.substring(
                name.lastIndexOf('/', name.length() - 1) + 1);
        }

        public String getFullName() {
            return name;
        }

        public String getPath() {
            return name;
        }

        public boolean exists() {
            return false;
        }

        public boolean isDirectory() {
            return false;
        }

	public long lastModified() {
	    return -1;
	}

        public String[] list() throws IOException {
            throw new IOException("not a directory");
        }

        public byte[] read() throws IOException {
            throw new IOException("cannot read archive");
        }

        public AbstractFile open(String fname) {
            throw new Error("not a directory");
        }
    }

    final class JarFileEntry extends NoJarFileEntry {

        JarFileEntry(String name) {
            super(name);
        }

        public String getPath() {
            return JarArchive.this.getPath() + "(" + name + ")";
        }

        public boolean exists() {
            return true;
        }

	public long lastModified() {
	    return jarFile.getJarEntry(name).getTime();
	}

        public byte[] read() throws IOException {
            JarEntry jarEntry = jarFile.getJarEntry(name);
            if (jarEntry == null)
                throw new IOException("unable to read " + name);
            InputStream in = jarFile.getInputStream(jarEntry);
            int rest = (int)jarEntry.getSize();
            byte[] buf = new byte[rest];
            do {
                int res = in.read(buf, buf.length - rest, rest);
                if (res == -1)
                    throw new IOException("read error");
                rest -= res;
            } while (rest > 0);
            in.close();
            return buf;
        }
    }
}