MOD:更换为Vertx框架 提高效率
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package com.leosam.tvbox.mv.utils;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
@@ -14,19 +14,33 @@ import java.util.Properties;
|
||||
*/
|
||||
public class ClassPathReaderUtils {
|
||||
|
||||
public static int getSize(String path) {
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(path);
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
int available = inputStream.available();
|
||||
return available;
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static InputStreamReader getInputStreamReader(String path) {
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(path);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(),StandardCharsets.UTF_8);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
|
||||
return inputStreamReader;
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static BufferedReader getBufferedReader(String path) {
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(path);
|
||||
|
||||
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(),StandardCharsets.UTF_8);
|
||||
BufferedReader reader = new BufferedReader(inputStreamReader);
|
||||
return reader;
|
||||
@@ -35,17 +49,6 @@ public class ClassPathReaderUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Properties getProperties(String path) {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(path);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream());
|
||||
properties.load(new BufferedReader(inputStreamReader));
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
public static String getContent(String path) {
|
||||
StringBuilder content = new StringBuilder();
|
||||
@@ -70,5 +73,52 @@ public class ClassPathReaderUtils {
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
public static class ClassPathResource {
|
||||
private final String path;
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
public ClassPathResource(String path) {
|
||||
this.path = path;
|
||||
this.classLoader = getDefaultClassLoader();
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
InputStream is;
|
||||
if (this.classLoader != null) {
|
||||
is = this.classLoader.getResourceAsStream(this.path);
|
||||
} else {
|
||||
is = ClassLoader.getSystemResourceAsStream(this.path);
|
||||
}
|
||||
if (is == null) {
|
||||
throw new FileNotFoundException(path + " cannot be opened because it does not exist");
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
public static ClassLoader getDefaultClassLoader() {
|
||||
ClassLoader cl = null;
|
||||
try {
|
||||
cl = Thread.currentThread().getContextClassLoader();
|
||||
} catch (Throwable ex) {
|
||||
// Cannot access thread context ClassLoader - falling back...
|
||||
}
|
||||
if (cl == null) {
|
||||
// No thread context class loader -> use class loader of this class.
|
||||
cl = ClassPathResource.class.getClassLoader();
|
||||
if (cl == null) {
|
||||
// getClassLoader() returning null indicates the bootstrap ClassLoader
|
||||
try {
|
||||
cl = ClassLoader.getSystemClassLoader();
|
||||
} catch (Throwable ex) {
|
||||
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
|
||||
}
|
||||
}
|
||||
}
|
||||
return cl;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user