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
|
public final class FileToolBox {
private static final ClassLoader CL = FileToolBox.class.getClassLoader();
/**
* Renvoie un flux sur un fichier désigné par son nom de ressource.
*
* @param res Nom de ressource
* @return le flux sur le fichier designé
*/
public static InputStream ressourceToInputStream(String res) {
String fileName = ressourceToFileName(res);
// cas où res n'est pas une ressource : il s'agit peut-être d'un emplacement physique
if (fileName == null) {
return getInputStream(res);
}
return getInputStream(fileName);
}
/**
* Cette méthode convertit une ressource en nom de fichier.
*
* @param res la ressource à convertir
* @return la ressource sous forme de nom de fichier
*/
public static String ressourceToFileName(final String res) {
URL url = getResourceURL(res);
if (url != null) {
if (url.toExternalForm().startsWith("jar:")) {
return url.toExternalForm();
} else {
String temp =urlToFile(url).getAbsolutePath();
return temp==null?null:replace( temp, "%20", " ");
}
} else {
return null;
}
}
/**
* Construit une URL en s'aidant du classLoader. Les ressources URL sont définit dans la racine du "package"
*
* @param paramRes le nom de la ressource
* @return l'URL associée
*/
public static URL getResourceURL(final String paramRes) {
String res;
URL url = null;
// Les ressources ne peuvent pas commencer par "/"
if (paramRes.startsWith("/")) {
res = paramRes.substring(1, paramRes.length());
} else {
res = paramRes;
}
try {
url = CL.getResource(res);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Impossible de trouver la ressource: " + res);
}
}
return url;
}
/**
* Convertit une URL en un objet File.
*
* @param url l'URL à convertir
* @return un descripteur de fichier, ou NULL
*/
private static File urlToFile(final URL url) {
final String externalForm = url.toExternalForm();
if (externalForm.startsWith("file:")) {
final String path = externalForm.substring(5, externalForm.length());
final File f = new File(path);
if (f == null) {
System.err.println("URL NULL !");
}
return f;
}
return null;
}
/**
* Charge un fichier dans un flux.
*
* @param entree Nom du fichier
* @return Le flux correspondant à ce fichier
*/
public static InputStream getInputStream(String entree) {
if (entree.startsWith("jar:")) {
URL url;
try {
url = new URL(entree);
return url.openStream();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
return new FileInputStream(entree);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
/**
* Remplace toutes les occurences d'une chaîne dans une autre chaîne
* @param string La chaîne à modifier
* @param pattern La chaîne à remplacer
* @param newPattern La chaîne remplacée
* @return La chaîne modifiée
*/
public static String replace(final String string, final String pattern, final String newPattern) {
int index;
if (string == null) {
return null;
}
final StringBuffer buffer = new StringBuffer(string);
final StringBuffer buffer1 = new StringBuffer(string);
while ((index = buffer1.toString().indexOf(pattern)) > 0) {
buffer.replace(index, index + pattern.length(), newPattern);
buffer1.replace(index, index + pattern.length(), " ");
}
return buffer.toString();
}
} |
Partager