读取Classpath下的资源为字符串

@param resource 资源路径，使用相对ClassPath的路径
@param charset 编码
@return 资源内容
@since 3.1.1
171150
public static String readStr(String resource, Charset charset) {
		return getResourceObj(resource).readStr(charset);
	}
935753
public SoapServerFaultResponseActionBuilder faultDetailResource(Resource resource, Charset charset) {
        try {
            getAction().getFaultDetails().add(FileUtils.readToString(resource, charset));
        } catch (IOException e) {
            throw new CitrusRuntimeException("Failed to read fault detail resource", e);
        }
        return this;
    }
286659
public Reader createScript(Charset charset) {

		String[] names = configParam.split("\\|");
		Properties props = new Properties();

		Locale currentLocale = getLocaleToApply();

		for (int x = 0; x < names.length; x++) {

			ResourceBundle bundle;

			try {
				bundle = ResourceBundle.getBundle(names[x], currentLocale, control);
			} catch (MissingResourceException ex) {
				// Fixes problems with some servers, e.g. WLS 10
				try {
					bundle = ResourceBundle.getBundle(names[x], currentLocale, getClass().getClassLoader(), control);
				} catch (Exception e) {
					bundle = ResourceBundle.getBundle(names[x], currentLocale,
							Thread.currentThread().getContextClassLoader(), control);
				}
			}

			updateProperties(bundle, props, charset);
		}
		return doCreateScript(props);
	}
462513
public static InputStream getResourceAsInputStream(String resource) {
        InputStream in = classLoaderWrapper.getResourceAsStream(resource);
        if (in == null) {
            return null;
        }
        return in;
    }
1309000
public char getChar( String key )
        throws MissingResourceException
    {
        ResourceBundle bundle = getBundle();
        String value = bundle.getString( key );

        if( 1 == value.length() )
        {
            return value.charAt( 0 );
        }
        else
        {
            throw new MissingResourceException( "Expecting a char value but got " + value,
                                                "java.lang.String",
                                                key
            );
        }
    }
1284909
public int submitFilePathOrClasspath(
            String filePathOrResourceClasspath) {
        return submitStream(JMOptional
                .getOptional(JMFiles.readLines(filePathOrResourceClasspath))
                .orElseGet(() -> JMResources
                        .readLines(filePathOrResourceClasspath)).stream());
    }
653360
public static ImmutableSet<String> wordSet(final Class<?> origin, final String resource) throws IOException {
        return ImmutableSet.copyOf(new HashSet<String>() {{
            readResource(origin, resource, new NullReturnLineProcessor() {
                @Override
                public boolean processLine(@Nonnull final String line) {
                    final String l = simplify(line);
                    // add to the containing HashSet we are currently in the init block of
                    if (!l.isEmpty() && !l.startsWith("#")) {
                        for (final String part : SPACES.split(l)) {
                            add(clean(part));
                        }
                    }
                    return true;
                }
            });
        }});
    }
862557
private static List<String> loadClasspathResource(String resource, ResourceLoader resourceLoader, String resourcePaths) throws IOException{
        List<String> result = new ArrayList<>();
        LOGGER.info("类路径资源："+resource);
        Enumeration<URL> ps = AutoDetector.class.getClassLoader().getResources(resource);
        while(ps.hasMoreElements()) {
            URL url=ps.nextElement();
            LOGGER.info("类路径资源URL："+url);
            if(url.getFile().contains(".jar!")){
                //加载jar资源
                result.addAll(load("classpath:"+resource));
                continue;
            }
            File file=new File(url.getFile());
            boolean dir = file.isDirectory();
            if(dir){
                //处理目录
                result.addAll(loadAndWatchDir(file.toPath(), resourceLoader, resourcePaths));
            }else{
                //处理文件
                result.addAll(load(file.getAbsolutePath()));
                //监控文件
                watchFile(file, resourceLoader, resourcePaths);
            }
        }
        return result;
    }
248565
public static <T> T withClasspathOrFilePath(
            String resourceClasspathOrFilePath,
            TypeReference<T> typeReference) {
        return withJsonString(JMResources.getStringWithClasspathOrFilePath(
                resourceClasspathOrFilePath), typeReference);
    }
303833
private static String readClasspathResource(String filename) {
        InputStream is = CreateEntities.class.getResourceAsStream(filename);
        if (is == null) {
            throw new IllegalArgumentException(
                    "Could not find the given file '" + filename + "' in the classpath.");
        }
        
        return readContents(is);
    }
1242285
public static Buffer readResourceToBuffer(@NotNull Class<?> clazz, @NotNull String resource) {
        try {
            Buffer buffer = Buffer.buffer(0);

            try (InputStream in = clazz.getResourceAsStream(resource)) {
                int read;
                byte[] data = new byte[4096];
                while ((read = in.read(data, 0, data.length)) != -1) {
                    if (read == data.length) {
                        buffer.appendBytes(data);
                    } else {
                        byte[] slice = new byte[read];
                        System.arraycopy(data, 0, slice, 0, slice.length);
                        buffer.appendBytes(slice);
                    }
                }
            }

            return buffer;
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
1119274
public static URL locateOnClasspath(String resourceName) {
        URL url = null;

        // attempt to load from the context classpath
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader != null) {
            url = loader.getResource(resourceName);

            if (url != null) {
                log.debug("Located '{}' in the context classpath", resourceName);
            }
        }

        // attempt to load from the system classpath
        if (url == null) {
            url = ClassLoader.getSystemResource(resourceName);

            if (url != null) {
                log.debug("Located '{}' in the system classpath", resourceName);
            }
        }

        return url;
    }
175723
public static Resource newSystemResource(String resource)
        throws IOException
    {
        URL url=null;
        // Try to format as a URL?
        ClassLoader
            loader=Thread.currentThread().getContextClassLoader();
        if (loader!=null)
        {
            url=loader.getResource(resource);
            if (url==null && resource.startsWith("/"))
                url=loader.getResource(resource.substring(1));
        }
        if (url==null)
        {
            loader=Resource.class.getClassLoader();
            if (loader!=null)
            {
                url=loader.getResource(resource);
                if (url==null && resource.startsWith("/"))
                    url=loader.getResource(resource.substring(1));
            }
        }
        
        if (url==null)
        {
            url=ClassLoader.getSystemResource(resource);
            if (url==null && resource.startsWith("/"))
                url=loader.getResource(resource.substring(1));
        }
        
        if (url==null)
            return null;
        
        return newResource(url);
    }
168121
protected static Charset getCharset(final Map<String, String> properties) {
		String charsetName = properties.get("charset");
		try {
			return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName);
		} catch (IllegalArgumentException ex) {
			InternalLogger.log(Level.ERROR, "Invalid charset: " + charsetName);
			return Charset.defaultCharset();
		}
	}
869000
public static String getClassName(Resource resource) {
        try {
            return getClassName(resource.getFile().getAbsolutePath());
        } catch (IOException e) {
            return null;
        }
    }
446235
public static Action resourceContent(String resourcePath, String charset) {
        URL resource = Action.class.getClassLoader().getResource(resourcePath);
        if (resource == null) {
            throw new IllegalArgumentException(String.format("Resource %s not found.", resourcePath));
        }
        Action charsetAction = charset == null ? noop() : charset(Charset.forName(charset));
        return composite(resourceContent(resource), charsetAction);
    }
1271957
public static Resource getGenericResource(String resource) {
        log.trace("retrieving resource -> " + resource);
        try {

            final File file = normFile(resource);
            if (file.exists()) {
                return new ResourceImpl(file.toURI().toURL());
            }

            if (resource.startsWith("/")) {
                throw new IllegalArgumentException("Classpath resource cannot start with '/': " + resource);
            }
            if (resource.indexOf('\\') != -1) {
                throw new IllegalArgumentException("Classpath resource cannot contain '\\': " + resource);
            }
            URL url = getLoader().getResource(resource);
            if (url == null) {
                return null;
            }
            return new ResourceImpl(url);
        } catch (Exception e) {
            log.error("Could not retrieve resource: " + e.getMessage(), e);
            return null;
        }
    }