Class ByteArrayResource

java.lang.Object
org.springframework.core.io.AbstractResource
org.springframework.core.io.ByteArrayResource
All Implemented Interfaces:
InputStreamSource, Resource
Direct Known Subclasses:
TransformedResource, TransformedResource

public class ByteArrayResource extends AbstractResource
Resource implementation for a given byte array.

Creates a ByteArrayInputStream for the given byte array.

Useful for loading content from any given byte array, without having to resort to a single-use InputStreamResource. Particularly useful for creating mail attachments from local content, where JavaMail needs to be able to read the stream multiple times.

Since:
1.2.3
Author:
Juergen Hoeller, Sam Brannen
See Also:
  • Constructor Details

    • ByteArrayResource

      public ByteArrayResource(byte[] byteArray)
      Create a new ByteArrayResource.
      Parameters:
      byteArray - the byte array to wrap
    • ByteArrayResource

      public ByteArrayResource(byte[] byteArray, @Nullable String description)
      Create a new ByteArrayResource with a description.
      Parameters:
      byteArray - the byte array to wrap
      description - where the byte array comes from
  • Method Details

    • getByteArray

      public final byte[] getByteArray()
      Return the underlying byte array.
    • exists

      public boolean exists()
      This implementation always returns true.
      Specified by:
      exists in interface Resource
      Overrides:
      exists in class AbstractResource
    • contentLength

      public long contentLength()
      This implementation returns the length of the underlying byte array.
      Specified by:
      contentLength in interface Resource
      Overrides:
      contentLength in class AbstractResource
      See Also:
    • getInputStream

      public InputStream getInputStream() throws IOException
      This implementation returns a ByteArrayInputStream for the underlying byte array.
      Returns:
      the input stream for the underlying resource (must not be null)
      Throws:
      FileNotFoundException - if the underlying resource does not exist
      IOException - if the content stream could not be opened
      See Also:
    • getContentAsByteArray

      public byte[] getContentAsByteArray() throws IOException
      Description copied from interface: Resource
      Return the contents of this resource as a byte array.
      Returns:
      the contents of this resource as byte array
      Throws:
      FileNotFoundException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
      IOException - in case of general resolution/reading failures
    • getContentAsString

      public String getContentAsString(Charset charset) throws IOException
      Description copied from interface: Resource
      Return the contents of this resource as a string, using the specified charset.
      Parameters:
      charset - the charset to use for decoding
      Returns:
      the contents of this resource as a String
      Throws:
      FileNotFoundException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
      IOException - in case of general resolution/reading failures
    • getDescription

      public String getDescription()
      This implementation returns a description that includes the passed-in description, if any.
      See Also:
    • equals

      public boolean equals(@Nullable Object other)
      This implementation compares the underlying byte array.
      Overrides:
      equals in class AbstractResource
      See Also:
    • hashCode

      public int hashCode()
      This implementation returns the hash code based on the underlying byte array.
      Overrides:
      hashCode in class AbstractResource
      See Also: