Interface Resource
-
- All Known Subinterfaces:
NonTraversableResource
,ReadOnlyResource
,WriteOnlyResource
- All Known Implementing Classes:
BaseResource
,ByteArrayResource
,ClasspathResource
,EmptyResource
,FileResource
,InputStreamResource
,OutputStreamResource
,ReaderResource
,StdinResource
,StdoutResource
,StringResource
,URIResource
,URLResource
,WriterResource
,ZipResource
public interface Resource
Information about a resource, which abstracts away the specific details on working with the resource. Gives the ability to open an
InputStream
andOutputStream
as well as manipulate the resource. Manipulation is implementation specific.- Author:
- David Bracewell
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Resource.Deserializer
-
Field Summary
Fields Modifier and Type Field Description static Pattern
ALL_FILE_PATTERN
The constant ALL_FILE_PATTERN.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Resource
append(byte[] byteArray)
Appends the given byte array content to the resource.default Resource
append(String content)
Appends the given string content to the resource.Optional<File>
asFile()
Gets the resource as aFile
.default Optional<Path>
asPath()
As path optional.Optional<URI>
asURI()
As uri optional.default 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.default Iterator<Resource>
childIterator(boolean recursive)
Iterator over the childrendefault Iterator<Resource>
childIterator(String pattern, boolean recursive)
Iterator over the childrenResource
compressed()
Sets is compressed.default void
copy(Resource copyTo)
Copies the contents of this resource to anotherdefault boolean
delete()
Deletes the resourcedefault boolean
delete(boolean recursively)
Deletes the resourcedefault Resource
deleteOnExit()
Deletes the resource on extString
descriptor()
Descriptor string.boolean
exists()
Exists boolean.default void
forEach(@NonNull SerializableConsumer<String> consumer)
For each.Charset
getCharset()
Gets the charset for reading and writing.Resource
getChild(String relativePath)
Creates a new Resource that is relative to this resource.default List<Resource>
getChildren()
Lists all the resources that are directly under this resource.default List<Resource>
getChildren(boolean recursive)
Lists all the resources that are directly under this resource.default List<Resource>
getChildren(String pattern)
Lists all the resources that are directly under this resource.default List<Resource>
getChildren(String pattern, boolean recursive)
Lists all the resources that are directly under this resource.default List<Resource>
getChildren(Pattern pattern, boolean recursive)
Lists all the resources that are directly under this resource.Resource
getParent()
Gets parent.InputStream
inputStream()
Opens an input stream over this resource.boolean
isCompressed()
Is compressed.boolean
isDirectory()
Is directory.default MStream<String>
lines()
Creates anMStream
over the lines in the resource.default boolean
mkdir()
Mkdir boolean.default boolean
mkdirs()
Mkdirs boolean.OutputStream
outputStream()
Opens an output stream over this resource.String
path()
Gets path in the same mannar asFile.getPath()
default byte[]
readBytes()
Reads the resource into an array of bytes.default Reader
reader()
Opens a reader using guessing the encoding and falling back to the default on the resource.default List<String>
readLines()
Reads the complete resource in as text breaking it into lines based on the newline characterdefault <T> T
readObject()
Deserializes an object from a resourcedefault String
readToString()
Reads in the resource as a String using UTF-8.Resource
setCharset(Charset charset)
Sets the charset for reading and writing.Resource
setCompression(Compression compression)
Sets the compression algorithm.Resource
setIsCompressed(boolean isCompressed)
Sets is compressed.Resource
uncompressed()
Uncompressed resource.default Resource
write(byte[] content)
Writes the given byte array to the resource overwriting any existing content.default Resource
write(String content)
Writes the given string to the resource overwriting any existing content.default Resource
writeObject(Object object)
Serializes an object to the resource using Java Serialization.default Writer
writer()
Opens a writer for writing for writing to the resource
-
-
-
Field Detail
-
ALL_FILE_PATTERN
static final Pattern ALL_FILE_PATTERN
The constant ALL_FILE_PATTERN.
-
-
Method Detail
-
append
default Resource append(String content) throws IOException
Appends the given string content to the resource.
- Parameters:
content
- The content to append- Returns:
- the resource
- Throws:
IOException
- the io exception
-
append
Resource append(byte[] byteArray) throws IOException
Appends the given byte array content to the resource.
- Parameters:
byteArray
- The content to append- Returns:
- the resource
- Throws:
IOException
- the io exception
-
asFile
Optional<File> asFile()
Gets the resource as aFile
.- Returns:
- A
File
representing the resource.
-
asURL
default Optional<URL> asURL()
Gets the resource as aURL
.- Returns:
- A
URL
representing the resource.
-
baseName
String baseName()
Gets the name (file name or directory name) of this resource.- Returns:
- The name of the file or directory
-
canRead
boolean canRead()
Returnstrue
if the resource is readable,false
if not.- Returns:
- True if can read from the resource
-
canWrite
boolean canWrite()
Returnstrue
if the resource is writable,false
if not.- Returns:
- True if can write to the resource
-
childIterator
default Iterator<Resource> childIterator(String pattern, boolean recursive)
Iterator over the children- Parameters:
pattern
- The pattern to determine what files we wantrecursive
- should we iterator recursively?- Returns:
- An iterator over the children
-
childIterator
default Iterator<Resource> childIterator(boolean recursive)
Iterator over the children- Parameters:
recursive
- should we iterator recursively?- Returns:
- An iterator over the children
-
compressed
Resource compressed()
Sets is compressed.- Returns:
- the resource
-
copy
default void copy(Resource copyTo) throws IOException
Copies the contents of this resource to another- Parameters:
copyTo
- The resource to copy to- Throws:
IOException
- Something went wrong copying.
-
delete
default boolean delete()
Deletes the resource- Returns:
- true if the deletion was successful
-
delete
default boolean delete(boolean recursively)
Deletes the resource- Parameters:
recursively
- true if should recursively delete everything under this resource- Returns:
- true if the deletion was successful
-
deleteOnExit
default Resource deleteOnExit()
Deletes the resource on ext- Returns:
- the resource
-
descriptor
String descriptor()
Descriptor string.- Returns:
- The string representation of the resource with protocol
-
exists
boolean exists()
Exists boolean.- Returns:
- True if the resource exists, False if the resource does not exist.
-
forEach
default void forEach(@NonNull @NonNull SerializableConsumer<String> consumer) throws IOException
For each.- Parameters:
consumer
- the consumer- Throws:
IOException
- the io exception
-
getCharset
Charset getCharset()
Gets the charset for reading and writing. The charset if not specified will be automatically determined during read.
- Returns:
- The charset used for writing and default when reading
-
getChild
Resource getChild(String relativePath)
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
default List<Resource> getChildren()
Lists all the resources that are directly under this resource.
- Returns:
- A list of all the resources one level under this resource.
-
getChildren
default List<Resource> getChildren(boolean recursive)
Lists all the resources that are directly under this resource.
- Parameters:
recursive
- Gets all children recursively- Returns:
- A list of all the resources one level under this resource.
-
getChildren
default List<Resource> getChildren(String pattern)
Lists all the resources that are directly under this resource.
- Parameters:
pattern
- The file matching pattern- Returns:
- A list of all the resources one level under this resource.
-
getChildren
default List<Resource> getChildren(String pattern, boolean recursive)
Lists all the resources that are directly under this resource.
- Parameters:
pattern
- The file matching patternrecursive
- Gets all children recursively- Returns:
- A list of all the resources one level under this resource.
-
getChildren
default List<Resource> getChildren(Pattern pattern, boolean recursive)
Lists all the resources that are directly under this resource.
- Parameters:
pattern
- The file matching patternrecursive
- Gets all children recursively- Returns:
- A list of all the resources one level under this resource.
-
getParent
Resource getParent()
Gets parent.- Returns:
- The parent resource (directory for file, parent directory for a directory)
-
inputStream
InputStream inputStream() throws IOException
Opens an input stream over this resource.
- Returns:
- An input stream over this resource.
- Throws:
IOException
- the io exception
-
isCompressed
boolean isCompressed()
Is compressed.- Returns:
- True if the resources is gzipped compressed
-
isDirectory
boolean isDirectory()
Is directory.- Returns:
- True if the resource is a directory
-
lines
default MStream<String> lines() throws IOException
Creates anMStream
over the lines in the resource.- Returns:
- the stream of lines
- Throws:
IOException
- Something went wrong reading from the resource
-
mkdir
default boolean mkdir()
Mkdir boolean.- Returns:
- the boolean
- See Also:
java.io.File#mkdir()java.io.File#mkdir()java.io.File#mkdir()java.io.File#mkdir()
-
mkdirs
default boolean mkdirs()
Mkdirs boolean.- Returns:
- the boolean
- See Also:
java.io.File#mkdirs()java.io.File#mkdirs()java.io.File#mkdirs()java.io.File#mkdirs()
-
outputStream
OutputStream outputStream() throws IOException
Opens an output stream over this resource.
- Returns:
- An output stream over this resource.
- Throws:
IOException
- the io exception
-
path
String path()
Gets path in the same mannar asFile.getPath()
- Returns:
- The full path to the resource including name
-
readBytes
default byte[] readBytes() throws IOException
Reads the resource into an array of bytes.
- Returns:
- An array of bytes representing the content of the resource.
- Throws:
IOException
- the io exception
-
readLines
default List<String> readLines() throws IOException
Reads the complete resource in as text breaking it into lines based on the newline character- Returns:
- A list of string representing the contents of the file.
- Throws:
IOException
- the io exception
-
readObject
default <T> T readObject() throws IOException
Deserializes an object from a resource- Type Parameters:
T
- the type parameter- Returns:
- the t
- Throws:
IOException
- the exception
-
readToString
default String readToString() throws IOException
Reads in the resource as a String using UTF-8.
- Returns:
- A string representing the contents of the file.
- Throws:
IOException
- the io exception
-
reader
default Reader reader() throws IOException
Opens a reader using guessing the encoding and falling back to the default on the resource.- Returns:
- A reader
- Throws:
IOException
- the io exception
-
setCharset
Resource setCharset(Charset charset)
Sets the charset for reading and writing.
- Parameters:
charset
- The charset to use- Returns:
- the charset
-
setCompression
Resource setCompression(Compression compression)
Sets the compression algorithm.- Parameters:
compression
- the compression algorithm- Returns:
- this Resource
-
setIsCompressed
Resource setIsCompressed(boolean isCompressed)
Sets is compressed.- Parameters:
isCompressed
- the is compressed- Returns:
- the is compressed
-
uncompressed
Resource uncompressed()
Uncompressed resource.- Returns:
- the resource
-
write
default Resource write(byte[] content) throws IOException
Writes the given byte array to the resource overwriting any existing content.
- Parameters:
content
- The content to write.- Returns:
- the resource
- Throws:
IOException
- the io exception
-
write
default Resource write(String content) throws IOException
Writes the given string to the resource overwriting any existing content.
- Parameters:
content
- The content to write.- Returns:
- the resource
- Throws:
IOException
- the io exception
-
writeObject
default Resource writeObject(Object object) throws IOException
Serializes an object to the resource using Java Serialization.- Parameters:
object
- The object to serialize- Returns:
- the resource
- Throws:
IOException
- the exception
-
writer
default Writer writer() throws IOException
Opens a writer for writing for writing to the resource- Returns:
- A writer
- Throws:
IOException
- the io exception
-
-