Class Resources

java.lang.Object
io.github.cjstehno.testthings.Resources

public final class Resources extends Object
Some useful utilities for working with classpath resources.
See Also:
  • Constructor Details

    • Resources

      public Resources()
  • Method Details

    • resourceToString

      public static String resourceToString(String path, Charset charset)
      Loads the specified resource path as a String with the specified charset.
      Parameters:
      path - the classpath resource path
      charset - the charset to be used
      Returns:
      the string version of the resource
    • resourceToString

      public static String resourceToString(String path)
      Loads the specified resource path as a UTF-8 string.
      Parameters:
      path - the classpath resource path
      Returns:
      the string version of the resource
    • resourceToBytes

      public static byte[] resourceToBytes(String path)
      Loads the specified classpath resource as a byte array.
      Parameters:
      path - the resource path
      Returns:
      a byte array containing the resource data
    • resourceStream

      public static InputStream resourceStream(String path)
      Loads the specified classpath resource as an InputStream.
      Parameters:
      path - the resource path
      Returns:
      an InputStream containing the resource data
    • resourceReader

      public static Reader resourceReader(String path)
      Loads the specified classpath resource as a Reader, specifically a BufferedReader.
      Parameters:
      path - the resource path
      Returns:
      a Reader containing the resource data
    • resourceUrl

      public static URL resourceUrl(String path)
      Retrieves the URL for the specified resource.
      Parameters:
      path - the resource path
      Returns:
      the resource URL
    • resourcePath

      public static Path resourcePath(String path)
      Retrieves the Path reference to the classpath resource.
      Parameters:
      path - the resource path
      Returns:
      the Path reference to the classpath resource
    • resourceFile

      public static File resourceFile(String path)
      Retrieves the File reference to the classpath resource.
      Parameters:
      path - the resource path
      Returns:
      the File reference to the classpath resource
    • resourceDeserialized

      public static <T> T resourceDeserialized(SerdesProvider provider, String path, Class<? extends T> type)
      Loads the resource at the specified path as bytes and then deserializes it to the specified object type.
      Type Parameters:
      T - the type of the deserialized object
      Parameters:
      provider - the serdes provider
      path - the resource path
      type - the object type
      Returns:
      the deserialized object loaded from the resource path
    • resourceDeserialized

      public static <T> T resourceDeserialized(String path, Class<? extends T> type)
      Loads the resource at the specified path as bytes and then deserializes as JSON it to the specified object type.
      Type Parameters:
      T - the type of the deserialized object
      Parameters:
      path - the resource path
      type - the object type
      Returns:
      the deserialized object from the resource path content
    • resourceUri

      public static URI resourceUri(String path)
      Retrieves the URI for the specified resource.
      Parameters:
      path - the resource path
      Returns:
      the resource URI
    • copyResourceTo

      public static void copyResourceTo(String resourcePath, Path path)
      Copies the classpath resource at the specified path to the provided destination path.
      Parameters:
      resourcePath - the classpath resource path
      path - the destination path
    • copyResourceTo

      public static void copyResourceTo(String resourcePath, File file)
      Copies the classpath resource at the specified path to the provided destination file.
      Parameters:
      resourcePath - the classpath resource path
      file - the destination file
    • template

      public static String template(String path, Map<String,Object> replacements)
      Loads the content at the specified classpath location as a string template. The replacement values are surrounded by curly braces, as {tag}. The provided map defines the replacement values.
      Parameters:
      path - the resource path
      replacements - the replacement values
      Returns:
      the template with all defined values replaced