Class Resource

java.lang.Object
nl.siegmann.epublib.domain.Resource
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
LazyResource

public class Resource extends Object implements Serializable
Represents a resource that is part of the epub. A resource can be a html file, image, xml, etc.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • id

      private String id
    • title

      private String title
    • href

      private String href
    • originalHref

      protected String originalHref
    • mediaType

      private MediaType mediaType
    • inputEncoding

      private String inputEncoding
    • data

      protected byte[] data
  • Constructor Details

    • Resource

      public Resource(String href)
      Creates an empty Resource with the given href. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
      Parameters:
      href - The location of the resource within the epub. Example: "chapter1.html".
    • Resource

      public Resource(byte[] data, MediaType mediaType)
      Creates a Resource with the given data and MediaType. The href will be automatically generated. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
      Parameters:
      data - The Resource's contents
      mediaType - The MediaType of the Resource
    • Resource

      public Resource(byte[] data, String href)
      Creates a resource with the given data at the specified href. The MediaType will be determined based on the href extension. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
      Parameters:
      data - The Resource's contents
      href - The location of the resource within the epub. Example: "chapter1.html".
      See Also:
    • Resource

      public Resource(Reader in, String href) throws IOException
      Creates a resource with the data from the given Reader at the specified href. The MediaType will be determined based on the href extension.
      Parameters:
      in - The Resource's contents
      href - The location of the resource within the epub. Example: "cover.jpg".
      Throws:
      IOException
      See Also:
    • Resource

      public Resource(InputStream in, String href) throws IOException
      Creates a resource with the data from the given InputStream at the specified href. The MediaType will be determined based on the href extension.
      Parameters:
      in - The Resource's contents
      href - The location of the resource within the epub. Example: "cover.jpg".
      Throws:
      IOException
      See Also:
    • Resource

      public Resource(String id, byte[] data, String href, MediaType mediaType)
      Creates a resource with the given id, data, mediatype at the specified href. Assumes that if the data is of a text type (html/css/etc) then the encoding will be UTF-8
      Parameters:
      id - The id of the Resource. Internal use only. Will be auto-generated if it has a null-value.
      data - The Resource's contents
      href - The location of the resource within the epub. Example: "chapter1.html".
      mediaType - The resources MediaType
    • Resource

      public Resource(String id, byte[] data, String href, MediaType mediaType, String inputEncoding)
      Creates a resource with the given id, data, mediatype at the specified href. If the data is of a text type (html/css/etc) then it will use the given inputEncoding.
      Parameters:
      id - The id of the Resource. Internal use only. Will be auto-generated if it has a null-value.
      data - The Resource's contents
      href - The location of the resource within the epub. Example: "chapter1.html".
      mediaType - The resources MediaType
      inputEncoding - If the data is of a text type (html/css/etc) then it will use the given inputEncoding.
  • Method Details

    • getInputStream

      public InputStream getInputStream() throws IOException
      Gets the contents of the Resource as an InputStream.
      Returns:
      The contents of the Resource.
      Throws:
      IOException
    • getData

      public byte[] getData() throws IOException
      The contents of the resource as a byte[]
      Returns:
      The contents of the resource
      Throws:
      IOException
    • close

      public void close()
      Tells this resource to release its cached data. If this resource was not lazy-loaded, this is a no-op.
    • setData

      public void setData(byte[] data)
      Sets the data of the Resource. If the data is a of a different type then the original data then make sure to change the MediaType.
      Parameters:
      data -
    • getSize

      public long getSize()
      Returns the size of this resource in bytes.
      Returns:
      the size.
    • getTitle

      public String getTitle()
      If the title is found by scanning the underlying html document then it is cached here.
      Returns:
      the title
    • setId

      public void setId(String id)
      Sets the Resource's id: Make sure it is unique and a valid identifier.
      Parameters:
      id -
    • getId

      public String getId()
      The resources Id. Must be both unique within all the resources of this book and a valid identifier.
      Returns:
      The resources Id.
    • getHref

      public String getHref()
      The location of the resource within the contents folder of the epub file. Example:
      images/cover.jpg
      content/chapter1.xhtml
      Returns:
      The location of the resource within the contents folder of the epub file.
    • setHref

      public void setHref(String href)
      Sets the Resource's href.
      Parameters:
      href -
    • getInputEncoding

      public String getInputEncoding()
      The character encoding of the resource. Is allowed to be null for non-text resources like images.
      Returns:
      The character encoding of the resource.
    • setInputEncoding

      public void setInputEncoding(String encoding)
      Sets the Resource's input character encoding.
      Parameters:
      encoding -
    • getReader

      public Reader getReader() throws IOException
      Gets the contents of the Resource as Reader. Does all sorts of smart things (courtesy of apache commons io XMLStreamREader) to handle encodings, byte order markers, etc.
      Returns:
      the contents of the Resource as Reader.
      Throws:
      IOException
    • hashCode

      public int hashCode()
      Gets the hashCode of the Resource's href.
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object resourceObject)
      Checks to see of the given resourceObject is a resource and whether its href is equal to this one.
      Overrides:
      equals in class Object
      Returns:
      whether the given resourceObject is a resource and whether its href is equal to this one.
    • getMediaType

      public MediaType getMediaType()
      This resource's mediaType.
      Returns:
      This resource's mediaType.
    • setMediaType

      public void setMediaType(MediaType mediaType)
    • setTitle

      public void setTitle(String title)
    • toString

      public String toString()
      Overrides:
      toString in class Object