summaryrefslogtreecommitdiffstats
path: root/src/com/google/doclava/JarUtils.java
blob: a5925ec94b744e7c3d8943cedea5f390b426d151 (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
/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.doclava;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarUtils {
  /**
   * Returns the jar file used to load class clazz, or defaultJar if clazz was not loaded from a
   * jar.
   */
  public static JarFile jarForClass(Class<?> clazz, JarFile defaultJar) {
    String path = "/" + clazz.getName().replace('.', '/') + ".class";
    URL jarUrl = clazz.getResource(path);
    if (jarUrl == null) {
      return defaultJar;
    }

    String url = jarUrl.toString();
    int bang = url.indexOf("!");
    String JAR_URI_PREFIX = "jar:file:";
    if (url.startsWith(JAR_URI_PREFIX) && bang != -1) {
      try {
        return new JarFile(url.substring(JAR_URI_PREFIX.length(), bang));
      } catch (IOException e) {
        throw new IllegalStateException("Error loading jar file.", e);
      }
    } else {
      return defaultJar;
    }
  }

  /**
   * Copies a directory from a jar file to an external directory.
   */
  public static void copyResourcesToDirectory(JarFile fromJar, String jarDir, String destDir)
      throws IOException {
    for (Enumeration<JarEntry> entries = fromJar.entries(); entries.hasMoreElements();) {
      JarEntry entry = entries.nextElement();
      if (entry.getName().startsWith(jarDir + "/") && !entry.isDirectory()) {
        File dest = new File(destDir + "/" + entry.getName().substring(jarDir.length() + 1));
        File parent = dest.getParentFile();
        if (parent != null) {
          parent.mkdirs();
        }

        FileOutputStream out = new FileOutputStream(dest);
        InputStream in = fromJar.getInputStream(entry);

        try {
          byte[] buffer = new byte[8 * 1024];

          int s = 0;
          while ((s = in.read(buffer)) > 0) {
            out.write(buffer, 0, s);
          }
        } catch (IOException e) {
          throw new IOException("Could not copy asset from jar file", e);
        } finally {
          try {
            in.close();
          } catch (IOException ignored) {}
          try {
            out.close();
          } catch (IOException ignored) {}
        }
      }
    }

  }

  private JarUtils() {} // non-instantiable
}