add method

This commit is contained in:
Looly 2021-07-16 01:34:27 +08:00
parent f2e54a0f93
commit df2147cd7c
4 changed files with 78 additions and 31 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.7.5 (2021-07-14) # 5.7.5 (2021-07-16)
### 🐣新特性 ### 🐣新特性
* 【core 】 DateUtil增加ceiling重载可选是否归零毫秒 * 【core 】 DateUtil增加ceiling重载可选是否归零毫秒
@ -13,6 +13,7 @@
* 【core 】 StrUtil.insert支持负数index * 【core 】 StrUtil.insert支持负数index
* 【core 】 Calculator类支持取模运算issue#I40DUW@Gitee * 【core 】 Calculator类支持取模运算issue#I40DUW@Gitee
* 【core 】 增加Base64.isBase64方法issue#1710@Github * 【core 】 增加Base64.isBase64方法issue#1710@Github
* 【core 】 ManifestUtil新增方法getManifest(Class<?> cls)pr#370@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee * 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee

View File

@ -1,6 +1,6 @@
package cn.hutool.core.io; package cn.hutool.core.io;
import cn.hutool.core.util.ClassUtil; import cn.hutool.core.io.resource.ResourceUtil;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -22,44 +22,45 @@ public class ManifestUtil {
private static final String[] MANIFEST_NAMES = {"Manifest.mf", "manifest.mf", "MANIFEST.MF"}; private static final String[] MANIFEST_NAMES = {"Manifest.mf", "manifest.mf", "MANIFEST.MF"};
/** /**
* 根据 class 获取 jar 包文件的 Manifest * 根据 class 获取 所在 jar 包文件的 Manifest<br>
* 此方法主要利用class定位jar包如引入hutool-all则传入hutool中任意一个类即可获取这个jar的Manifest信息<br>
* 如果这个类不在jar包中返回{@code null}
* *
* @param cls * @param cls
* @return Manifest * @return Manifest
* @throws IORuntimeException IO异常
*/ */
public static Manifest getManifest(Class<?> cls) throws IOException { public static Manifest getManifest(Class<?> cls) throws IORuntimeException {
URL url = ClassUtil.getResourceUrl("", cls); URL url = ResourceUtil.getResource(null, cls);
JarFile jarFile = null; URLConnection connection;
try { try {
URLConnection connection = url.openConnection(); connection = url.openConnection();
if (connection instanceof JarURLConnection) { }catch (final IOException e) {
JarURLConnection urlConnection = (JarURLConnection) connection; throw new IORuntimeException(e);
jarFile = urlConnection.getJarFile(); }
return jarFile.getManifest();
} if (connection instanceof JarURLConnection) {
} finally { JarURLConnection conn = (JarURLConnection) connection;
IoUtil.close(jarFile); return getManifest(conn);
} }
return null; return null;
} }
/** /**
* 获取 jar 包文件 Manifest * 获取 jar 包文件或项目目录下 Manifest
* *
* @param classpathItem 文件路径 * @param classpathItem 文件路径
* @return Manifest * @return Manifest
* @throws IORuntimeException IO异常
*/ */
public static Manifest getManifest(File classpathItem) { public static Manifest getManifest(File classpathItem) throws IORuntimeException{
Manifest manifest = null; Manifest manifest = null;
if (classpathItem.isFile()) { if (classpathItem.isFile()) {
JarFile jar = null; try (JarFile jarFile = new JarFile(classpathItem)){
try { manifest = getManifest(jarFile);
jar = new JarFile(classpathItem); } catch (final IOException e) {
manifest = jar.getManifest(); throw new IORuntimeException(e);
} catch (final IOException ignore) {
} finally {
IoUtil.close(jar);
} }
} else { } else {
final File metaDir = new File(classpathItem, "META-INF"); final File metaDir = new File(classpathItem, "META-INF");
@ -73,18 +74,47 @@ public class ManifestUtil {
} }
} }
} }
if (manifestFile != null) { if (null != manifestFile) {
FileInputStream fis = null; try(FileInputStream fis = new FileInputStream(manifestFile)){
try {
fis = new FileInputStream(manifestFile);
manifest = new Manifest(fis); manifest = new Manifest(fis);
} catch (final IOException ignore) { } catch (final IOException e) {
} finally { throw new IORuntimeException(e);
IoUtil.close(fis);
} }
} }
} }
return manifest; return manifest;
} }
/**
* 根据 {@link JarURLConnection} 获取 jar 包文件的 Manifest
*
* @param connection {@link JarURLConnection}
* @return Manifest
* @throws IORuntimeException IO异常
*/
public static Manifest getManifest(JarURLConnection connection) throws IORuntimeException{
final JarFile jarFile;
try {
jarFile = connection.getJarFile();
} catch (IOException e) {
throw new IORuntimeException(e);
}
return getManifest(jarFile);
}
/**
* 根据 {@link JarURLConnection} 获取 jar 包文件的 Manifest
*
* @param jarFile {@link JarURLConnection}
* @return Manifest
* @throws IORuntimeException IO异常
*/
public static Manifest getManifest(JarFile jarFile) throws IORuntimeException {
try {
return jarFile.getManifest();
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
} }

View File

@ -174,11 +174,12 @@ public class ResourceUtil {
/** /**
* 获得资源相对路径对应的URL * 获得资源相对路径对应的URL
* *
* @param resource 资源相对路径 * @param resource 资源相对路径{@code null}""都表示classpath根路径
* @param baseClass 基准Class获得的相对路径相对于此Class所在路径如果为{@code null}则相对ClassPath * @param baseClass 基准Class获得的相对路径相对于此Class所在路径如果为{@code null}则相对ClassPath
* @return {@link URL} * @return {@link URL}
*/ */
public static URL getResource(String resource, Class<?> baseClass) { public static URL getResource(String resource, Class<?> baseClass) {
resource = StrUtil.nullToEmpty(resource);
return (null != baseClass) ? baseClass.getResource(resource) : ClassLoaderUtil.getClassLoader().getResource(resource); return (null != baseClass) ? baseClass.getResource(resource) : ClassLoaderUtil.getClassLoader().getResource(resource);
} }

View File

@ -0,0 +1,15 @@
package cn.hutool.core.io;
import org.junit.Assert;
import org.junit.Test;
import java.util.jar.Manifest;
public class ManifestUtilTest {
@Test
public void getManiFestTest(){
final Manifest manifest = ManifestUtil.getManifest(Test.class);
Assert.assertNotNull(manifest);
}
}