Top Description Fields Constructors Methods
org.springframework.core.io

public Class ByteArrayResource

extends AbstractResource
Class Inheritance
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, java.nio.charset.Charset, java.util.Arrays, org.springframework.lang.Nullable, org.springframework.util.Assert

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.

Authors
Juergen Hoeller, Sam Brannen
Since
1.2.3
See Also
java.io.ByteArrayInputStream, InputStreamResource, org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)

Field Summary

Modifier and TypeField and Description
private final byte[]
private final String

Constructor Summary

AccessConstructor and Description
public
ByteArrayResource(byte[]
the byte array to wrap
byteArray
)

Create a new ByteArrayResource.

public
ByteArrayResource(byte[]
the byte array to wrap
byteArray
,
String
where the byte array comes from
description
)

Create a new ByteArrayResource with a description.

Method Summary

Modifier and TypeMethod and Description
public long
contentLength()

Overrides org.springframework.core.io.AbstractResource.contentLength.

Implements org.springframework.core.io.Resource.contentLength.

This implementation returns the length of the underlying byte array.
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying byte array.
public boolean
exists()

Overrides org.springframework.core.io.AbstractResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation always returns true.
public final byte[]
getByteArray()

Return the underlying byte array.

public byte[]
getContentAsByteArray()

Overrides default org.springframework.core.io.Resource.getContentAsByteArray.

Return the contents of this resource as a byte array.
public String
getContentAsString(Charset
the charset to use for decoding
charset
)

Overrides default org.springframework.core.io.Resource.getContentAsString.

Return the contents of this resource as a string, using the specified charset.
public String
getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the passed-in description, if any.
public InputStream
getInputStream()

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation returns a ByteArrayInputStream for the underlying byte array.
public int
hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code based on the underlying byte array.
Inherited from org.springframework.core.io.AbstractResource:
createRelativegetFilegetFileForLastModifiedCheckgetFilenamegetURIgetURLisFileisOpenisReadablelastModifiedreadableChanneltoString

Field Detail

byteArrayback to summary
private final byte[] byteArray
descriptionback to summary
private final String description

Constructor Detail

ByteArrayResourceback to summary
public ByteArrayResource(byte[] byteArray)

Create a new ByteArrayResource.

Parameters
byteArray:byte[]

the byte array to wrap

ByteArrayResourceback to summary
public ByteArrayResource(byte[] byteArray, String description)

Create a new ByteArrayResource with a description.

Parameters
byteArray:byte[]

the byte array to wrap

description:String

where the byte array comes from

Method Detail

contentLengthback to summary
public long contentLength()

Overrides org.springframework.core.io.AbstractResource.contentLength.

Implements org.springframework.core.io.Resource.contentLength.

This implementation returns the length of the underlying byte array.

Annotations
@Override
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.core.io.AbstractResource.equals.

This implementation compares the underlying byte array.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
See Also
java.util.Arrays#equals(byte[], byte[])
existsback to summary
public boolean exists()

Overrides org.springframework.core.io.AbstractResource.exists.

Implements org.springframework.core.io.Resource.exists.

This implementation always returns true.

Annotations
@Override
getByteArrayback to summary
public final byte[] getByteArray()

Return the underlying byte array.

getContentAsByteArrayback to summary
public byte[] getContentAsByteArray() throws IOException

Overrides default org.springframework.core.io.Resource.getContentAsByteArray.

Doc from org.springframework.core.io.Resource.getContentAsByteArray.

Return the contents of this resource as a byte array.

Returns:byte[]

the contents of this resource as byte array

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getContentAsStringback to summary
public String getContentAsString(Charset charset) throws IOException

Overrides default org.springframework.core.io.Resource.getContentAsString.

Doc from org.springframework.core.io.Resource.getContentAsString.

Return the contents of this resource as a string, using the specified charset.

Parameters
charset:Charset

the charset to use for decoding

Returns:String

the contents of this resource as a String

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getDescriptionback to summary
public String getDescription()

Implements org.springframework.core.io.Resource.getDescription.

This implementation returns a description that includes the passed-in description, if any.

Annotations
@Override
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.springframework.core.io.InputStreamSource.getInputStream.

This implementation returns a ByteArrayInputStream for the underlying byte array.

Returns:InputStream

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

the input stream for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

if the content stream could not be opened

See Also
java.io.ByteArrayInputStream
hashCodeback to summary
public int hashCode()

Overrides org.springframework.core.io.AbstractResource.hashCode.

This implementation returns the hash code based on the underlying byte array.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override