Class JBFile

java.lang.Object
com.jbstrap.core.filestore.JBFile
All Implemented Interfaces:
Serializable

public final class JBFile extends Object implements 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 Details

    • JBFile

      public JBFile(String filename, Long size, File file)
      Creates a JBfile, based on the specified file.
      Parameters:
      filename - The filename
      size - The filesize, in bytes
      file - The File, that contains the file
      Throws:
      NullPointerException - If the filename was specified as null, or if the specified file was null
  • Method Details

    • getFileName

      public String getFileName()
      Gets the filename
      Returns:
      The filename
    • setSize

      public JBFile setSize(long size)
      Sets the filesize
      Parameters:
      size - The filesize in bytes
      Returns:
      The JBFile object
    • getSize

      public Long getSize()
      Gets the filesize, in bytes
      Returns:
      The filesize in bytes, or null, if it wasn't specified
    • getHumanReadableSize

      public String 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

      public File getFile()
      Gets the File object that is assigned to the JBFile.
      Returns:
      The File object that is assigned to the JBFile.
    • getFileContent

      public byte[] getFileContent() throws IOException
      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

      public String 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

      public String 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, otherwise false
    • store

      public JBFile store()
      Stores the file in the previously specified filestore.
      Returns:
      The stored JBFile object
      Throws:
      NullPointerException - If the JBFile contains no real File
      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

      public void sendToUI(UI ui)

      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 as null
    • sendToClient

      public void sendToClient(Client client)

      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 as null
    • sendToClient

      public void sendToClient(String clientId)

      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 as null, 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

      public String 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.
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)

      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
      Overrides:
      equals in class Object
      Returns:
      true if they are equal, otherwise false
    • createJBFileFromContent

      public static JBFile createJBFileFromContent(byte[] content, String filename) 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 format
      filename - 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 format
      filename - The name of the new file
      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.
    • 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 format
      filename - The name of the new file
      size - 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.