Class FileMethods
- java.lang.Object
-
- io.toro.martini.FileMethods
-
public final class FileMethods extends Object
Provides one-liners for performing file operations.
-
-
Constructor Summary
Constructors Constructor Description FileMethods(Path packagesHomePath)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcopy(InputStream inputStream, OutputStream outputStream, GloopExecutionContext context)Copies bytes from a large (over 2GB)InputStreamto anOutputStream.static longcopyLarge(InputStream inputStream, OutputStream outputStream, GloopExecutionContext context)Copies bytes from a large (over 2GB)InputStreamto anOutputStream.static GloopModelgetTextFileInputCursor(InputStream flatFileData, String encoding, String recordSeparator, GloopExecutionContext context)Read anInputStreamand return a Gloop cursor for reading from the resource.static OutputStreamoutputStream(GloopExecutionContext context, String file, boolean mkDirs, String... openOptions)Opens or creates a package file, returning an output stream that may be used to write bytes to the file.static OutputStreampackageOutputStream(GloopExecutionContext context, String file, String packageName, boolean mkDirs, String... openOptions)Opens or creates a package file, returning an output stream that may be used to write bytes to the file.static OutputStreampackageOutputStream(String file, String packageName, boolean mkDirs, OpenOption... openOption)Opens or creates a package file, returning an output stream that may be used to write bytes to the file.static StringreadAllContent(String resource, String packageName)Attempt to read a resource from the selected package context, returning anStringobject to read from the resource.static Stream<String>readContent(String resource, String packageName)Attempt to read a resource from the selected package context, returning aStreamobject to read from the resource.static voidreadContent(String packageName, String resource, Closure<String> closure)Attempt to read a resource from the selected package context and executes the closure for every line of the resource content.static GloopModelreadFileAsMultipart(String filePath, String contentType)Wrap a file path and return a multi-partGloopModel.static GloopModelreadLines(String resource, String packageName, String encoding, String lineSeparator, GloopExecutionContext context)Read a resource file residing in a Martini package and return a Gloop cursor for said file.static GloopModelreadPackageFileAsMultipart(String resourceName, String packageName, String contentType)Wrap a resource from a Martini package and return a multi-partGloopModel.static Pathresource(String resource, String packageName)Open a resource from the specified package context, returning aPathto read from the resource.static InputStreamresourceStream(String resource, String packageName)Open a resource from a specified package, returning anInputStreamto read from the resource.static InputStreamresourceStream(String resource, String packageName, GloopExecutionContext context)Open a resource from a specified package, returning anInputStreamto read from the resource.static voidwrite(byte[] data, String file, boolean mkDirs)Write abyte[]to a specified destination.static voidwrite(InputStream data, String file, boolean mkDirs)Write data from anInputStreamto a specified destination.static voidwriteToPackage(byte[] data, String file, String packageName, boolean mkDirs)Write abyte[]to a specified destination inside a Martini package.static voidwriteToPackage(InputStream data, String file, String packageName, boolean mkDirs)Write anInputStreamto a specified destination inside a Martini package.
-
-
-
Constructor Detail
-
FileMethods
public FileMethods(Path packagesHomePath)
-
-
Method Detail
-
resourceStream
public static final InputStream resourceStream(@GloopParameter(allowNull=false) String resource, String packageName, GloopExecutionContext context) throws IOException
Open a resource from a specified package, returning anInputStreamto read from the resource. The stream will not be buffered, and is not required to support the mark or reset methods. The stream will be safe for access by multiple concurrent threads.- Parameters:
packageName- the name of the package; if empty, the current package context is selectedresource- the name of the resource (e.g.conf/package.xml)context- the execution context (e.g.conf/package.xml)- Throws:
IOException- if an I/O error occursio.toro.martini.ipackage.exception.PackageRuntimeException- ifpackageNameis empty and current package context is not set properly- Since:
- 1.0
-
resourceStream
public static final InputStream resourceStream(String resource, String packageName) throws IOException
Open a resource from a specified package, returning anInputStreamto read from the resource. The stream will not be buffered, and is not required to support the mark or reset methods. The stream will be safe for access by multiple concurrent threads.- Parameters:
packageName- the name of the package; if empty, the current package context is selectedresource- the name of the resource (e.g.conf/package.xml)- Throws:
IOException- if an I/O error occursio.toro.martini.ipackage.exception.PackageRuntimeException- ifpackageNameis empty and current package context is not set properly- Since:
- 1.0
-
copyLarge
public static long copyLarge(@GloopParameter(allowNull=false) InputStream inputStream, @GloopParameter(allowNull=false) OutputStream outputStream, GloopExecutionContext context) throws IOException
Copies bytes from a large (over 2GB)
InputStreamto anOutputStream.This method buffers the input internally, so there is no need to use a
BufferedInputStream. The buffer size is given byIOUtils.DEFAULT_BUFFER_SIZE.- Parameters:
inputStream- theInputStreamto read fromoutputStream- theOutputStreamto write to- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output isnullIOException- if an I/O error occurs- Since:
- 1.0
-
copy
public static int copy(@GloopParameter(allowNull=false) InputStream inputStream, @GloopParameter(allowNull=false) OutputStream outputStream, GloopExecutionContext context) throws IOException
Copies bytes from a large (over 2GB)
InputStreamto anOutputStream.This method buffers the input internally, so there is no need to use a
BufferedInputStream.Large streams (over 2GB) will return a bytes copied value of
-1after the copy has completed since the correct number of bytes cannot be returned as anint. For large streams, use theIOUtils.copyLarge(InputStream, OutputStream)method.- Parameters:
inputStream- theInputStreamto read fromoutputStream- theOutputStreamto write to- Returns:
- the number of bytes copied, or
-1if greater thanInteger.MAX_VALUE - Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- 1.0
-
packageOutputStream
public static OutputStream packageOutputStream(String file, String packageName, boolean mkDirs, OpenOption... openOption) throws IOException
Opens or creates a package file, returning an output stream that may be used to write bytes to the file. The resulting stream will not be buffered. The stream will be safe for access by multiple concurrent threads. Whether the returned stream is asynchronously closeable and/or interruptible is highly file system provider specific and therefore not specified.
This method opens or creates a file in exactly the manner specified by the
newByteChannelmethod with the exception that theREADoption may not be present in the array of options. If no options are present then this method works as if theCREATE,TRUNCATE_EXISTING, andWRITEoptions are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existingregular-fileto a size of0if it exists.- Parameters:
file- the file path stringpackageName- package to write the file in; if this is empty, the current package context is selectedmkDirs- flag for creating the parent directories of the fileopenOption- options specifying how the file is opened- Returns:
- a new
OutputStream - Throws:
IllegalArgumentException- ifoptionscontains an invalid combination of optionsUnsupportedOperationException- if an unsupported option is specifiedIOException- if an I/O error occursSecurityException- In the case of the default provider, and a security manager is installed, thecheckWritemethod is invoked to check write access to the file. ThecheckDeletemethod is invoked to check delete access if the file is opened with theDELETE_ON_CLOSEoption.- Since:
- 1.0
-
packageOutputStream
public static OutputStream packageOutputStream(GloopExecutionContext context, @GloopParameter(allowNull=false) String file, String packageName, @GloopParameter(allowNull=false,defaultValue="true") boolean mkDirs, @GloopParameter(allowNull=true,allowOtherValues=false,choices={"WRITE","APPEND","TRUNCATE_EXISTING","CREATE","CREATE_NEW","DELETE_ON_CLOSE","SPARSE","SYNC","DSYNC"}) String... openOptions) throws IOException
Opens or creates a package file, returning an output stream that may be used to write bytes to the file. The resulting stream will not be buffered. The stream will be safe for access by multiple concurrent threads. Whether the returned stream is asynchronously closeable and/or interruptible is highly file system provider specific and therefore not specified.
This method opens or creates a file in exactly the manner specified by the
newByteChannelmethod with the exception that theREADoption may not be present in the array of options. If no options are present then this method works as if theCREATE,TRUNCATE_EXISTING, andWRITEoptions are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existingregular-fileto a size of0if it exists.- Parameters:
context- the execution contextfile- the file path stringpackageName- package to write the file in; if this is empty, the current package context is selectedmkDirs- flag for creating the parent directories of the fileopenOptions- options specifying how the file is opened- Returns:
- a new
OutputStream - Throws:
IOException- if an I/O error occurs- Since:
- 1.0
-
outputStream
public static OutputStream outputStream(GloopExecutionContext context, @GloopParameter(allowNull=false,value="filePath in string") String file, @GloopParameter(allowNull=false,defaultValue="true",value="create the parent directories of the file. Default is true") boolean mkDirs, @GloopParameter(allowNull=true,value="options specifying how the file is opened",allowOtherValues=false,choices={"WRITE","APPEND","TRUNCATE_EXISTING","CREATE","CREATE_NEW","DELETE_ON_CLOSE","SPARSE","SYNC","DSYNC"}) String... openOptions) throws IOException
Opens or creates a package file, returning an output stream that may be used to write bytes to the file. The resulting stream will not be buffered. The stream will be safe for access by multiple concurrent threads. Whether the returned stream is asynchronously closeable and/or interruptible is highly file system provider specific and therefore not specified.
This method opens or creates a file in exactly the manner specified by the
newByteChannelmethod with the exception that theREADoption may not be present in the array of options. If no options are present then this method works as if theCREATE,TRUNCATE_EXISTING, andWRITEoptions are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existingregular-fileto a size of0if it exists.- Parameters:
context- the execution contextfile- the file path stringmkDirs- flag for creating the parent directories of the fileopenOptions- options specifying how the file is opened- Returns:
- a new
OutputStream - Throws:
IOException- if an I/O error occurs- Since:
- 1.0
-
write
public static void write(@GloopParameter(allowNull=false) byte[] data, @GloopParameter(allowNull=false) String file, @GloopParameter(allowNull=false,defaultValue="true") boolean mkDirs) throws IOException
Write abyte[]to a specified destination.- Parameters:
data- the byte array to write; do not modify during outputfile- where to write the bytesmkDirs- flag for creating the parent directories of the file- Throws:
NullPointerException- if output is nullIOException- if an I/O error occurs- Since:
- 1.0
-
write
public static void write(@GloopParameter(allowNull=false) InputStream data, @GloopParameter(allowNull=false) String file, @GloopParameter(allowNull=false,defaultValue="true") boolean mkDirs) throws IOException
Write data from anInputStreamto a specified destination.- Parameters:
data- theInputStreamto read fromfile- where to write the datamkDirs- flag for creating the parent directories of the file- Throws:
NullPointerException- if output is nullIOException- if an I/O error occurs- Since:
- 1.0
-
writeToPackage
public static void writeToPackage(@GloopParameter(allowNull=false) byte[] data, @GloopParameter(allowNull=false) String file, String packageName, @GloopParameter(allowNull=false,defaultValue="true") boolean mkDirs) throws IOException
Write abyte[]to a specified destination inside a Martini package.- Parameters:
data- the byte array to write; do not modify during outputfile- where to write the datapackageName- package to write the file in; if this is empty, the current package context is selectedmkDirs- flag for creating the parent directories of the file- Throws:
NullPointerException- if output is nullIOException- if an I/O error occurs- Since:
- 1.0
-
writeToPackage
public static void writeToPackage(@GloopParameter(allowNull=false) InputStream data, @GloopParameter(allowNull=false) String file, String packageName, @GloopParameter(allowNull=false,defaultValue="true") boolean mkDirs) throws IOException
Write anInputStreamto a specified destination inside a Martini package.- Parameters:
data- theInputStreamto read fromfile- where to write the datapackageName- package to write the file in; if this is empty, the current package context is selectedmkDirs- flag for creating the parent directories of the file- Throws:
NullPointerException- if output is nullIOException- if an I/O error occurs- Since:
- 1.0
-
resource
public static final Path resource(@GloopParameter(allowNull=false) String resource, String packageName) throws IOException
Open a resource from the specified package context, returning aPathto read from the resource.- Parameters:
packageName- the name of the package to read the resource from; if this is empty, the current package context is selectedresource- the name of the resource (e.g.conf/package.xml)- Throws:
IOException- if an I/O error occursio.toro.martini.ipackage.exception.PackageRuntimeException- ifpackageNameis empty and current package context is not set properly- Since:
- 1.0
-
readContent
public static final Stream<String> readContent(@GloopParameter(allowNull=false) String resource, String packageName) throws IOException
Attempt to read a resource from the selected package context, returning aStreamobject to read from the resource. This method does not read all lines, but instead populates lazily as the stream is consumed.- Parameters:
packageName- the name of the package to read the resource from; if this is empty, the current package context is selectedresource- the name of the resource (e.g.conf/package.xml)- Throws:
IOException- if an I/O error occursio.toro.martini.ipackage.exception.PackageRuntimeException- ifpackageNameis empty and current package context is not set properly- Since:
- 1.0
-
readContent
public static final void readContent(String packageName, String resource, Closure<String> closure) throws IOException
Attempt to read a resource from the selected package context and executes the closure for every line of the resource content.- Parameters:
packageName- the name of the package to read the resource from; if this is empty, the current package context is selectedresource- the name of the resource (e.g.conf/package.xml)closure- the closure to execute- Throws:
IOException- if an I/O error occursio.toro.martini.ipackage.exception.PackageRuntimeException- ifpackageNameis empty and current package context is not set properly- Since:
- 1.0
-
readAllContent
public static final String readAllContent(@GloopParameter(allowNull=false) String resource, String packageName) throws IOException
Attempt to read a resource from the selected package context, returning anStringobject to read from the resource.- Parameters:
packageName- the name of the package to read the resource from; if this is empty, the current package context is selectedresource- the name of the resource (e.g.conf/package.xml)- Throws:
IOException- if an I/O error occursio.toro.martini.ipackage.exception.PackageRuntimeException- ifpackageNameis empty and current package context is not set properly- Since:
- 1.0
-
getTextFileInputCursor
@GloopObjectParameter("output{\n output{\n linesInputCursor[]\n }\n}") public static GloopModel getTextFileInputCursor(@GloopParameter(allowNull=false) InputStream flatFileData, @GloopParameter(defaultValue="UTF-8") String encoding, @GloopParameter(defaultValue="\n") String recordSeparator, GloopExecutionContext context)
Read anInputStreamand return a Gloop cursor for reading from the resource.- Parameters:
flatFileData- resourceInputStreamencoding- character encodingrecordSeparator- record delimitercontext- execution context- Returns:
- cursor for the provided stream
- Since:
- 1.0
-
readFileAsMultipart
@GloopObjectParameter("output{\n multipartFile#io.toro.martini.http.MultipartFile{\n }\n}") public static GloopModel readFileAsMultipart(@GloopParameter(allowNull=false) String filePath, @GloopParameter(allowNull=false) String contentType)
Wrap a file path and return a multi-partGloopModel.- Parameters:
filePath- the file pathcontentType- content type of file that conforms to HTTP specifications- Returns:
- the multi-part
GloopModel;nullif it does not exist - Since:
- 1.0
- See Also:
- HTTP specification
-
readPackageFileAsMultipart
@GloopObjectParameter("output{\n multipartFile#io.toro.martini.http.MultipartFile{\n }\n}") public static GloopModel readPackageFileAsMultipart(@GloopParameter(allowNull=false) String resourceName, String packageName, @GloopParameter(allowNull=false) String contentType)
Wrap a resource from a Martini package and return a multi-partGloopModel.- Parameters:
resourceName- the name of the resource (e.g.conf/package.xml)packageName- the name of the package to read the resource from; if this is empty, the current package context is selectedcontentType- content type of file that conforms HTTP specifications- Returns:
- the multi-part
GloopModel;nullif the file does not exist - Since:
- 1.0
- See Also:
- HTTP specification
-
readLines
@GloopObjectParameter("output{\n output{\n linesInputCursor[]\n }\n}") public static GloopModel readLines(@GloopParameter(allowNull=false) String resource, String packageName, @GloopParameter(defaultValue="UTF-8") String encoding, @GloopParameter(defaultValue="\n") String lineSeparator, GloopExecutionContext context) throws IOException
Read a resource file residing in a Martini package and return a Gloop cursor for said file.- Parameters:
resource- the name of the resource (e.g.conf/package.xml)packageName- the name of the package to read the resource from; if this is empty, the current package context is selectedencoding- character encodinglineSeparator- record delimitercontext- execution context- Returns:
- the Gloop cursor
- Throws:
IOException- if an I/O error occurs- Since:
- 1.0
-
-