Class Resource

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    LazyResource

    public class Resource
    extends java.lang.Object
    implements java.io.Serializable
    Represents a resource that is part of the epub. A resource can be a html file, image, xml, etc.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Resource​(byte[] data, java.lang.String href)
      Creates a resource with the given data at the specified href.
      Resource​(byte[] data, MediaType mediaType)
      Creates a Resource with the given data and MediaType.
      Resource​(java.io.InputStream in, java.lang.String href)
      Creates a resource with the data from the given InputStream at the specified href.
      Resource​(java.io.Reader in, java.lang.String href)
      Creates a resource with the data from the given Reader at the specified href.
      Resource​(java.lang.String href)
      Creates an empty Resource with the given href.
      Resource​(java.lang.String id, byte[] data, java.lang.String href, MediaType mediaType)
      Creates a resource with the given id, data, mediatype at the specified href.
      Resource​(java.lang.String id, byte[] data, java.lang.String href, MediaType mediaType, java.lang.String inputEncoding)
      Creates a resource with the given id, data, mediatype at the specified href.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Tells this resource to release its cached data.
      boolean equals​(java.lang.Object resourceObject)
      Checks to see of the given resourceObject is a resource and whether its href is equal to this one.
      byte[] getData()
      The contents of the resource as a byte[]
      java.lang.String getHref()
      The location of the resource within the contents folder of the epub file.
      java.lang.String getId()
      The resources Id.
      java.lang.String getInputEncoding()
      The character encoding of the resource.
      java.io.InputStream getInputStream()
      Gets the contents of the Resource as an InputStream.
      MediaType getMediaType()
      This resource's mediaType.
      java.io.Reader getReader()
      Gets the contents of the Resource as Reader.
      long getSize()
      Returns the size of this resource in bytes.
      java.lang.String getTitle()
      If the title is found by scanning the underlying html document then it is cached here.
      int hashCode()
      Gets the hashCode of the Resource's href.
      void setData​(byte[] data)
      Sets the data of the Resource.
      void setHref​(java.lang.String href)
      Sets the Resource's href.
      void setId​(java.lang.String id)
      Sets the Resource's id: Make sure it is unique and a valid identifier.
      void setInputEncoding​(java.lang.String encoding)
      Sets the Resource's input character encoding.
      void setMediaType​(MediaType mediaType)  
      void setTitle​(java.lang.String title)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • id

        private java.lang.String id
      • title

        private java.lang.String title
      • href

        private java.lang.String href
      • originalHref

        protected java.lang.String originalHref
      • inputEncoding

        private java.lang.String inputEncoding
      • data

        protected byte[] data
    • Constructor Detail

      • Resource

        public Resource​(java.lang.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,
                        java.lang.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:
        MediatypeService.determineMediaType(String)
      • Resource

        public Resource​(java.io.Reader in,
                        java.lang.String href)
                 throws java.io.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:
        java.io.IOException
        See Also:
        MediatypeService.determineMediaType(String)
      • Resource

        public Resource​(java.lang.String id,
                        byte[] data,
                        java.lang.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​(java.lang.String id,
                        byte[] data,
                        java.lang.String href,
                        MediaType mediaType,
                        java.lang.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 Detail

      • getInputStream

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

        public byte[] getData()
                       throws java.io.IOException
        The contents of the resource as a byte[]
        Returns:
        The contents of the resource
        Throws:
        java.io.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 java.lang.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​(java.lang.String id)
        Sets the Resource's id: Make sure it is unique and a valid identifier.
        Parameters:
        id -
      • getId

        public java.lang.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 java.lang.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​(java.lang.String href)
        Sets the Resource's href.
        Parameters:
        href -
      • getInputEncoding

        public java.lang.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​(java.lang.String encoding)
        Sets the Resource's input character encoding.
        Parameters:
        encoding -
      • getReader

        public java.io.Reader getReader()
                                 throws java.io.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:
        java.io.IOException
      • hashCode

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

        public boolean equals​(java.lang.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 java.lang.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​(java.lang.String title)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object