Package com.gengoai.io.resource
Class ClasspathResource
- java.lang.Object
-
- com.gengoai.io.resource.BaseResource
-
- com.gengoai.io.resource.ClasspathResource
-
- All Implemented Interfaces:
Resource
,Serializable
public class ClasspathResource extends BaseResource
A
Resource
implementation for resources that exist on the classpath. Resources are loaded either using the supplied class loader or the system class loader if none is supplied.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.gengoai.io.resource.Resource
Resource.Deserializer
-
-
Field Summary
-
Fields inherited from interface com.gengoai.io.resource.Resource
ALL_FILE_PATTERN
-
-
Constructor Summary
Constructors Constructor Description ClasspathResource(String resource)
Constructs a ClasspathResource resource with a given charset, compression and encoding settingClasspathResource(String resource, @NonNull ClassLoader classLoader)
Creates a classpath resource that uses the given class loader to load the resource.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Resource
append(byte[] byteArray)
Appends the given byte array content to the resource.Optional<File>
asFile()
Gets the resource as aFile
.Optional<URI>
asURI()
As uri optional.Optional<URL>
asURL()
Gets the resource as aURL
.String
baseName()
Gets the name (file name or directory name) of this resource.boolean
canRead()
Returnstrue
if the resource is readable,false
if not.boolean
canWrite()
Returnstrue
if the resource is writable,false
if not.InputStream
createInputStream()
Create input stream input stream.OutputStream
createOutputStream()
Create output stream output stream.String
descriptor()
Descriptor string.boolean
equals(Object obj)
boolean
exists()
Exists boolean.Resource
getChild(String relativePath)
Creates a new Resource that is relative to this resource.List<Resource>
getChildren(Pattern filePattern, boolean recursive)
Lists all the resources that are directly under this resource.Resource
getParent()
Gets parent.int
hashCode()
boolean
isDirectory()
Is directory.boolean
mkdir()
Mkdir boolean.boolean
mkdirs()
Mkdirs boolean.String
path()
Gets path in the same mannar asFile.getPath()
-
Methods inherited from class com.gengoai.io.resource.BaseResource
compressed, getCharset, inputStream, isCompressed, outputStream, setCharset, setCompression, setIsCompressed, toString, uncompressed
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.gengoai.io.resource.Resource
append, asPath, childIterator, childIterator, copy, delete, delete, deleteOnExit, forEach, getChildren, getChildren, getChildren, getChildren, lines, readBytes, reader, readLines, readObject, readToString, write, write, writeObject, writer
-
-
-
-
Constructor Detail
-
ClasspathResource
public ClasspathResource(String resource)
Constructs a ClasspathResource resource with a given charset, compression and encoding setting- Parameters:
resource
- The resource
-
ClasspathResource
public ClasspathResource(String resource, @NonNull @NonNull ClassLoader classLoader)
Creates a classpath resource that uses the given class loader to load the resource.
- Parameters:
resource
- The path to the resource.classLoader
- The class loader to use.
-
-
Method Detail
-
append
public Resource append(byte[] byteArray) throws IOException
Description copied from interface:Resource
Appends the given byte array content to the resource.
- Parameters:
byteArray
- The content to append- Returns:
- the resource
- Throws:
IOException
- the io exception
-
asFile
public Optional<File> asFile()
Description copied from interface:Resource
Gets the resource as aFile
.- Specified by:
asFile
in interfaceResource
- Overrides:
asFile
in classBaseResource
- Returns:
- A
File
representing the resource.
-
asURI
public Optional<URI> asURI()
Description copied from interface:Resource
As uri optional.- Specified by:
asURI
in interfaceResource
- Overrides:
asURI
in classBaseResource
- Returns:
- the optional
-
asURL
public Optional<URL> asURL()
Description copied from interface:Resource
Gets the resource as aURL
.- Returns:
- A
URL
representing the resource.
-
baseName
public String baseName()
Description copied from interface:Resource
Gets the name (file name or directory name) of this resource.- Specified by:
baseName
in interfaceResource
- Overrides:
baseName
in classBaseResource
- Returns:
- The name of the file or directory
-
canRead
public boolean canRead()
Description copied from interface:Resource
Returnstrue
if the resource is readable,false
if not.- Specified by:
canRead
in interfaceResource
- Overrides:
canRead
in classBaseResource
- Returns:
- True if can read from the resource
-
canWrite
public boolean canWrite()
Description copied from interface:Resource
Returnstrue
if the resource is writable,false
if not.- Specified by:
canWrite
in interfaceResource
- Overrides:
canWrite
in classBaseResource
- Returns:
- True if can write to the resource
-
createInputStream
public InputStream createInputStream() throws IOException
Description copied from class:BaseResource
Create input stream input stream.- Overrides:
createInputStream
in classBaseResource
- Returns:
- the input stream
- Throws:
IOException
- the io exception
-
createOutputStream
public OutputStream createOutputStream() throws IOException
Description copied from class:BaseResource
Create output stream output stream.- Overrides:
createOutputStream
in classBaseResource
- Returns:
- the output stream
- Throws:
IOException
- the io exception
-
descriptor
public String descriptor()
Description copied from interface:Resource
Descriptor string.- Specified by:
descriptor
in interfaceResource
- Overrides:
descriptor
in classBaseResource
- Returns:
- The string representation of the resource with protocol
-
exists
public boolean exists()
Description copied from interface:Resource
Exists boolean.- Returns:
- True if the resource exists, False if the resource does not exist.
-
getChild
public Resource getChild(String relativePath)
Description copied from interface:Resource
Creates a new Resource that is relative to this resource.
- Parameters:
relativePath
- The relative path for the new resource.- Returns:
- A new resource that is relative to this resource.
-
getChildren
public List<Resource> getChildren(Pattern filePattern, boolean recursive)
Description copied from interface:Resource
Lists all the resources that are directly under this resource.
- Parameters:
filePattern
- The file matching patternrecursive
- Gets all children recursively- Returns:
- A list of all the resources one level under this resource.
-
getParent
public Resource getParent()
Description copied from interface:Resource
Gets parent.- Returns:
- The parent resource (directory for file, parent directory for a directory)
-
isDirectory
public boolean isDirectory()
Description copied from interface:Resource
Is directory.- Specified by:
isDirectory
in interfaceResource
- Overrides:
isDirectory
in classBaseResource
- Returns:
- True if the resource is a directory
-
mkdir
public boolean mkdir()
Description copied from interface:Resource
Mkdir boolean.- Returns:
- the boolean
- See Also:
java.io.File#mkdir()java.io.File#mkdir()java.io.File#mkdir()java.io.File#mkdir()
-
mkdirs
public boolean mkdirs()
Description copied from interface:Resource
Mkdirs boolean.- Returns:
- the boolean
- See Also:
java.io.File#mkdirs()java.io.File#mkdirs()java.io.File#mkdirs()java.io.File#mkdirs()
-
path
public String path()
Description copied from interface:Resource
Gets path in the same mannar asFile.getPath()
- Specified by:
path
in interfaceResource
- Overrides:
path
in classBaseResource
- Returns:
- The full path to the resource including name
-
-