Class JBFile
- All Implemented Interfaces:
Serializable
Class representing a file for the JBStrap framework.
The class contains a file, and the methods that are necessary for handling the file. It also provides a way to create new files,
store files with the help of the FileStoreAPI
, to delete files, and to download them.
- Since:
- 4.0
- Author:
- JBStrap
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic JBFile
createJBFileFromContent
(byte[] content, String filename) Creates a new JBFile, based on the specified byte array's contents.static JBFile
createJBFileFromContent
(byte[] content, String filename, Long size, String fileStoreName) Creates a new JBFile, based on the specified byte array's contents.static JBFile
createJBFileFromContent
(byte[] content, String filename, String fileStoreName) Creates a new JBFile, based on the specified byte array's contents.void
delete()
Deletes the file from the filestoreboolean
Determines if two JBFiles are equal or not.getFile()
Gets theFile
object that is assigned to the JBFile.byte[]
Gets the file content in a byte arrayGets the file's unique IDGets the filenameGets the name of the filestore, where the file is stored, or the filestore that was assigned to the file.Gets the filesize, in a human readable formatgetSize()
Gets the filesize, in bytesboolean
Determines if the file is marked for deletionboolean
Determines if the file was persisted(saved).void
sendToClient
(Client client) Sends the file to the specified clientvoid
sendToClient
(String clientId) Sends the file to the specified clientvoid
Sends the file to the specifiedUI
setSize
(long size) Sets the filesizestore()
Stores the file in the previously specified filestore.toString()
Displays the file as string.
-
Constructor Details
-
JBFile
Creates a JBfile, based on the specified file.- Parameters:
filename
- The filenamesize
- The filesize, in bytesfile
- TheFile
, that contains the file- Throws:
NullPointerException
- If the filename was specified asnull
, or if the specified file wasnull
-
-
Method Details
-
getFileName
Gets the filename- Returns:
- The filename
-
setSize
Sets the filesize- Parameters:
size
- The filesize in bytes- Returns:
- The JBFile object
-
getSize
Gets the filesize, in bytes- Returns:
- The filesize in bytes, or
null
, if it wasn't specified
-
getHumanReadableSize
Gets the filesize, in a human readable format
The filesize will be in the most suitable units. This means that the return value will be:
- In bytes, if the file is less than 1 kilobyte
- In kilobytes if it's more than 1 kilobyte, but less than 1 megabyte
- In megabytes if it's more than 1 megabyte, but less than 1 gigabyte
- In gigabytes if it's more than 1 gigabyte, but less than 1 terabyte
- In terabytes, if it's greater than 1 terabyte.
- Returns:
- The filesize in human readable format, in a string, or
null
, if the size wasn't specified
-
getFile
Gets theFile
object that is assigned to the JBFile.- Returns:
- The
File
object that is assigned to the JBFile.
-
getFileContent
Gets the file content in a byte array- Returns:
- The file content in a byte array
- Throws:
IOException
- If the file contents couldn't be read.
-
getFileStoreName
Gets the name of the filestore, where the file is stored, or the filestore that was assigned to the file.- Returns:
- The name of the filestore, or
null
, if the file isn't saved to a filestore, and it wasn't specified.
-
getFileId
Gets the file's unique ID- Returns:
- The file's unique ID or
null
, if the file wasn't saved to a filestore yet.
-
isPersisted
public boolean isPersisted()Determines if the file was persisted(saved).- Returns:
true
, if it was, otherwisefalse
-
store
Stores the file in the previously specified filestore.- Returns:
- The stored JBFile object
- Throws:
NullPointerException
- If the JBFile contains no realFile
FileStoreException
- If the specified Filestore is not found, or if the file couldn't be written to the filestore folder, or if the folder doesn't exist, or if the application lacks proper access to the folder.
-
delete
public void delete()Deletes the file from the filestore- Throws:
FileStoreException
- If the specified Filestore is not found, or if the file couldn't be written to the filestore folder, or if the folder doesn't exist, or if the application lacks proper access to the folder.
-
sendToUI
Sends the file to the specified
UI
By calling this method, the file will be automatically downloaded to the client, where the specified UI is currently opened.
- Parameters:
ui
- The UI instance, where the file will be sent to.- Throws:
NullPointerException
- If the UI was specified asnull
-
sendToClient
Sends the file to the specified client
By calling this method, the file will be automatically downloaded to the client, regardless of what UI or page is currently opened.
- Parameters:
client
- The client where the file will be sent to- Throws:
NullPointerException
- If the client was specified asnull
-
sendToClient
Sends the file to the specified client
By calling this method, the file will be automatically downloaded to the client, regardless of what UI or page is currently opened.
- Parameters:
clientId
- The ID of the client- Throws:
NullPointerException
- If the client ID was specified asnull
, or if the client is not found
-
isDeleted
public boolean isDeleted()Determines if the file is marked for deletion- Returns:
true
, if it is marked for deletion, but not deleted yet.
-
toString
Displays the file as string. It will be marked as a JBFile, and it will contain the file's name size and path in a human readable format. -
equals
Determines if two JBFiles are equal or not.
They are considered equal if:
- The specified object is not
null
- The specified object is a JBFile
- If both are persisted, and their unique IDs match.
- If neither are persisted, and their physical files are a match
- The specified object is not
-
createJBFileFromContent
Creates a new JBFile, based on the specified byte array's contents. Files created like this are not stored, it must be manually called if you wish to store/persist them.- Parameters:
content
- The file content, in a byte array formatfilename
- The name of the new file- Returns:
- The created JBFile
- Throws:
IOException
- If an error was encountered during the creation of the file, or during the writing of its contents.
-
createJBFileFromContent
public static JBFile createJBFileFromContent(byte[] content, String filename, String fileStoreName) throws IOException Creates a new JBFile, based on the specified byte array's contents. Files created like this are not stored, it must be manually called if you wish to store/persist them.- Parameters:
content
- The file content, in a byte array formatfilename
- The name of the new filefileStoreName
- The name of the filestore, where the file will be saved to.- Returns:
- The created JBFile
- Throws:
IOException
- If an error was encountered during the creation of the file, or during the writing of its contents.
-
createJBFileFromContent
public static JBFile createJBFileFromContent(byte[] content, String filename, Long size, String fileStoreName) throws IOException Creates a new JBFile, based on the specified byte array's contents. Files created like this are not stored, it must be manually called if you wish to store/persist them.- Parameters:
content
- The file content, in a byte array formatfilename
- The name of the new filesize
- The size of the new file. If not specified, the size will be based on the specified content.fileStoreName
- The name of the filestore, where the file will be saved to.- Returns:
- The created JBFile
- Throws:
IOException
- If an error was encountered during the creation of the file, or during the writing of its contents.
-