Module hw06

Class FileContents

java.lang.Object
provided.utils.file.impl.FileContents
All Implemented Interfaces:
Serializable, IFileContents

public class FileContents extends Object implements IFileContents
A concrete implementation of IFileContents. For convenience and simplicity, this class defines equality if the names are equal, not the actual contents.
Author:
swong
See Also:
Serialized Form
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private byte[]
    The contents of the file in byte array form.
    private String
    The info associated with the file contents
    private String
    The name associated with the file contents
    private static long
    For serialization
  • Constructor Summary

    Constructors
    Constructor
    Description
    FileContents​(String name, String filename)
    Construct an instance with an associated name and the full filename (i.e.
    FileContents​(String name, String filename, String info)
    Construct an instance with an associated name, additional information about the file contents and the full filename (i.e.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals​(Object other)
    Overridden to perform equality based on the name only
    byte[]
    The internally stored raw byte stream
    Returns any additional information about the file contents.
    Create an InputStream that sources the file contents.
    A name associated with the file, e.g.
    int
    Overridden to return the hashCode() of the name.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For serialization
      See Also:
      Constant Field Values
    • name

      private String name
      The name associated with the file contents
    • info

      private String info
      The info associated with the file contents
    • fileContents

      private byte[] fileContents
      The contents of the file in byte array form.
  • Constructor Details

    • FileContents

      public FileContents(String name, String filename)
      Construct an instance with an associated name and the full filename (i.e. includes sufficient path information) to access the file's contents from the disk. This filename is NOT stored and the FileContent object is independent from where its contents were read. The stored info is set to an empty String.
      Parameters:
      name - The name to associate with the file contents, relative to the class that is invoking this constructor.
      filename - The filename of the file that sources the contents.
    • FileContents

      public FileContents(String name, String filename, String info)
      Construct an instance with an associated name, additional information about the file contents and the full filename (i.e. includes sufficient path information) to access the file's contents from the disk. This filename is NOT stored and the FileContent object is independent from where its contents were read.
      Parameters:
      name - The name to associate with the file contents.
      filename - The filename of the file that sources the contents.
      info - Additional information about the contents
  • Method Details

    • getName

      public String getName()
      Description copied from interface: IFileContents
      A name associated with the file, e.g. its filename without the rest of its path. This name is just for reference/identification purposes.
      Specified by:
      getName in interface IFileContents
      Returns:
      A name to be associated with the contents.
    • getInfo

      public String getInfo()
      Description copied from interface: IFileContents
      Returns any additional information about the file contents. A typical usage would for an image file, for the info to be "jpg" or "png" to tell what kind of image data was in the contents.
      Specified by:
      getInfo in interface IFileContents
      Returns:
      The stored info string.
    • getBytes

      public byte[] getBytes()
      Description copied from interface: IFileContents
      The internally stored raw byte stream
      Specified by:
      getBytes in interface IFileContents
      Returns:
      The byte stream of the file
    • getInputStream

      public InputStream getInputStream()
      Description copied from interface: IFileContents
      Create an InputStream that sources the file contents. The use of this stream simulates the reading of the file from the disk but without actually involving any such storage access. This feature would be used by the receiver of this object to load the file contents into something that normally wants to read that data from the disk.
      Specified by:
      getInputStream in interface IFileContents
      Returns:
      An InputStream
    • equals

      public boolean equals(Object other)
      Overridden to perform equality based on the name only
      Overrides:
      equals in class Object
      Returns:
      true if the names are equal
    • hashCode

      public int hashCode()
      Overridden to return the hashCode() of the name.
      Overrides:
      hashCode in class Object
      Returns:
      The hashCode() of the name.