Package org.springframework.core.io
Class ByteArrayResource
- java.lang.Object
- org.springframework.core.io.AbstractResource
- org.springframework.core.io.ByteArrayResource
- All Implemented Interfaces:
InputStreamSource,Resource
- Direct Known Subclasses:
TransformedResource
public class ByteArrayResource extends AbstractResource
Resourceimplementation for a given byte array.Creates a
ByteArrayInputStreamfor 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:
ByteArrayInputStream,InputStreamResource,MimeMessageHelper.addAttachment(String, InputStreamSource)
Constructor Summary
Constructors Constructor Description ByteArrayResource(byte[] byteArray)Create a newByteArrayResource.ByteArrayResource(byte[] byteArray, String description)Create a newByteArrayResourcewith a description.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcontentLength()This implementation returns the length of the underlying byte array.booleanequals(Object obj)This implementation compares the underlying byte array.booleanexists()This implementation always returnstrue.byte[]getByteArray()Return the underlying byte array.StringgetDescription()This implementation returns a description that includes the passed-indescription, if any.InputStreamgetInputStream()This implementation returns a ByteArrayInputStream for the underlying byte array.inthashCode()This implementation returns the hash code based on the underlying byte array.Methods inherited from class org.springframework.core.io.AbstractResource
createRelative, getFile, getFileForLastModifiedCheck, getFilename, getURI, getURL, isOpen, isReadable, lastModified, toString
Constructor Detail
ByteArrayResource
public ByteArrayResource(byte[] byteArray)
Create a newByteArrayResource.- Parameters:
byteArray- the byte array to wrap
ByteArrayResource
public ByteArrayResource(byte[] byteArray, String description)
Create a newByteArrayResourcewith a description.- Parameters:
byteArray- the byte array to wrapdescription- where the byte array comes from
Method Detail
getByteArray
public final byte[] getByteArray()
Return the underlying byte array.
exists
public boolean exists()
This implementation always returnstrue.- Specified by:
existsin interfaceResource- Overrides:
existsin classAbstractResource
contentLength
public long contentLength()
This implementation returns the length of the underlying byte array.- Specified by:
contentLengthin interfaceResource- Overrides:
contentLengthin classAbstractResource- See Also:
InputStreamSource.getInputStream()
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 doesn't existIOException- if the content stream could not be opened- See Also:
ByteArrayInputStream
getDescription
public String getDescription()
This implementation returns a description that includes the passed-indescription, if any.- See Also:
Object.toString()
equals
public boolean equals(Object obj)
This implementation compares the underlying byte array.- Overrides:
equalsin classAbstractResource- See Also:
Arrays.equals(byte[], byte[])
hashCode
public int hashCode()
This implementation returns the hash code based on the underlying byte array.- Overrides:
hashCodein classAbstractResource- See Also:
Resource.getDescription()