org.springframework.core.io.support

Class EncodedResource

  • java.lang.Object
    • org.springframework.core.io.support.EncodedResource
  • All Implemented Interfaces:
    InputStreamSource


    public class EncodedResource
    extends java.lang.Object
    implements InputStreamSource
    Holder that combines a Resource descriptor with a specific encoding or Charset to be used for reading from the resource.

    Used as an argument for operations that support reading content with a specific encoding, typically via a java.io.Reader.

    Since:
    1.2.6
    Author:
    Juergen Hoeller, Sam Brannen
    See Also:
    InputStreamSource.getInputStream(), Reader, Charset
    • Constructor Summary

      Constructors 
      Constructor and Description
      EncodedResource(Resource resource)
      Create a new EncodedResource for the given Resource, not specifying an explicit encoding or Charset.
      EncodedResource(Resource resource, java.nio.charset.Charset charset)
      Create a new EncodedResource for the given Resource, using the specified Charset.
      EncodedResource(Resource resource, java.lang.String encoding)
      Create a new EncodedResource for the given Resource, using the specified encoding.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      boolean equals(java.lang.Object other) 
      java.nio.charset.Charset getCharset()
      Return the Charset to use for reading from the resource, or null if none specified.
      java.lang.String getEncoding()
      Return the encoding to use for reading from the resource, or null if none specified.
      java.io.InputStream getInputStream()
      Open an InputStream for the specified resource, ignoring any specified Charset or encoding.
      java.io.Reader getReader()
      Open a java.io.Reader for the specified resource, using the specified Charset or encoding (if any).
      Resource getResource()
      Return the Resource held by this EncodedResource.
      int hashCode() 
      boolean requiresReader()
      Determine whether a Reader is required as opposed to an InputStream, i.e.
      java.lang.String toString() 
      • Methods inherited from class java.lang.Object

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

      • EncodedResource

        public EncodedResource(Resource resource)
        Create a new EncodedResource for the given Resource, not specifying an explicit encoding or Charset.
        Parameters:
        resource - the Resource to hold (never null)
      • EncodedResource

        public EncodedResource(Resource resource,
                               @Nullable
                               java.lang.String encoding)
        Create a new EncodedResource for the given Resource, using the specified encoding.
        Parameters:
        resource - the Resource to hold (never null)
        encoding - the encoding to use for reading from the resource
      • EncodedResource

        public EncodedResource(Resource resource,
                               @Nullable
                               java.nio.charset.Charset charset)
        Create a new EncodedResource for the given Resource, using the specified Charset.
        Parameters:
        resource - the Resource to hold (never null)
        charset - the Charset to use for reading from the resource
    • Method Detail

      • getResource

        public final Resource getResource()
        Return the Resource held by this EncodedResource.
      • getEncoding

        @Nullable
        public final java.lang.String getEncoding()
        Return the encoding to use for reading from the resource, or null if none specified.
      • getCharset

        @Nullable
        public final java.nio.charset.Charset getCharset()
        Return the Charset to use for reading from the resource, or null if none specified.
      • requiresReader

        public boolean requiresReader()
        Determine whether a Reader is required as opposed to an InputStream, i.e. whether an encoding or a Charset has been specified.
        See Also:
        getReader(), getInputStream()
      • getReader

        public java.io.Reader getReader()
                                 throws java.io.IOException
        Open a java.io.Reader for the specified resource, using the specified Charset or encoding (if any).
        Throws:
        java.io.IOException - if opening the Reader failed
        See Also:
        requiresReader(), getInputStream()
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Open an InputStream for the specified resource, ignoring any specified Charset or encoding.
        Specified by:
        getInputStream in interface InputStreamSource
        Returns:
        the input stream for the underlying resource (must not be null)
        Throws:
        java.io.IOException - if opening the InputStream failed
        See Also:
        requiresReader(), getReader()
      • equals

        public boolean equals(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

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