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
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 SummaryConstructorsConstructorDescriptionByteArrayResource(byte[] byteArray) Create a newByteArrayResource.ByteArrayResource(byte[] byteArray, @Nullable String description) Create a newByteArrayResourcewith a description.
- 
Method SummaryModifier and TypeMethodDescriptionlongThis implementation returns the length of the underlying byte array.booleanThis implementation compares the underlying byte array.booleanexists()This implementation always returnstrue.final byte[]Return the underlying byte array.byte[]Return the contents of this resource as a byte array.getContentAsString(Charset charset) Return the contents of this resource as a string, using the specified charset.This implementation returns a description that includes the passed-indescription, if any.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.AbstractResourcecreateRelative, getFile, getFileForLastModifiedCheck, getFilename, getURI, getURL, isFile, isOpen, isReadable, lastModified, readableChannel, toString
- 
Constructor Details- 
ByteArrayResourcepublic ByteArrayResource(byte[] byteArray) Create a newByteArrayResource.- Parameters:
- byteArray- the byte array to wrap
 
- 
ByteArrayResource
 
- 
- 
Method Details- 
getByteArraypublic final byte[] getByteArray()Return the underlying byte array.
- 
existspublic boolean exists()This implementation always returnstrue.- Specified by:
- existsin interface- Resource
- Overrides:
- existsin class- AbstractResource
 
- 
contentLengthpublic long contentLength()This implementation returns the length of the underlying byte array.- Specified by:
- contentLengthin interface- Resource
- Overrides:
- contentLengthin class- AbstractResource
- See Also:
 
- 
getInputStreamThis implementation returns a ByteArrayInputStream for the underlying byte array.- Returns:
- the input stream for the underlying resource (must not be null)
- Throws:
- IOException- if the content stream could not be opened
- See Also:
 
- 
getContentAsByteArrayDescription copied from interface:ResourceReturn the contents of this resource as a byte array.- Returns:
- the contents of this resource as byte array
- Throws:
- IOException- in case of general resolution/reading failures
 
- 
getContentAsStringDescription copied from interface:ResourceReturn 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:
- IOException- in case of general resolution/reading failures
 
- 
getDescriptionThis implementation returns a description that includes the passed-indescription, if any.- See Also:
 
- 
equalsThis implementation compares the underlying byte array.- Overrides:
- equalsin class- AbstractResource
- See Also:
 
- 
hashCodepublic int hashCode()This implementation returns the hash code based on the underlying byte array.- Overrides:
- hashCodein class- AbstractResource
- See Also:
 
 
-