Package org.springframework.core.io.support

# Class EncodedResource

java.lang.Object
org.springframework.core.io.support.EncodedResource

All Implemented Interfaces:
  `InputStreamSource`

public class EncodedResource extends 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, Arjen Poutsma
See Also:
  - `InputStreamSource.getInputStream()`
  - `Reader`
  - `Charset`

## Constructor Summary

Constructors
Constructor
Description
`EncodedResource(Resource resource)`
Create a new `EncodedResource` for the given `Resource`, not specifying an explicit encoding or `Charset`.
`EncodedResource(Resource resource, String encoding)`
Create a new `EncodedResource` for the given `Resource`, using the specified `encoding`.
`EncodedResource(Resource resource, Charset charset)`
Create a new `EncodedResource` for the given `Resource`, using the specified `Charset`.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`equals(Object other)`
`final Charset`
`getCharset()`
Return the `Charset` to use for reading from the resource, or `null` if none specified.
`String`
`getContentAsString()`
Returns the contents of the specified resource as a string, using the specified `Charset` or encoding (if any).
`final String`
`getEncoding()`
Return the encoding to use for reading from the resource, or `null` if none specified.
`InputStream`
`getInputStream()`
Open an `InputStream` for the specified resource, ignoring any specified `Charset` or encoding.
`Reader`
`getReader()`
Open a `java.io.Reader` for the specified resource, using the specified `Charset` or encoding (if any).
`final 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.
`String`
`toString()`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### 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 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 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 Details

### getResource

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

### getEncoding

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

### getCharset

@Nullable public final 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 Reader getReader() throws IOException
Open a `java.io.Reader` for the specified resource, using the specified `Charset` or encoding (if any).

Throws:
  `IOException` - if opening the Reader failed
See Also:
  - `requiresReader()`
  - `getInputStream()`

### getInputStream

public InputStream getInputStream() throws 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:
  `IOException` - if opening the InputStream failed
See Also:
  - `requiresReader()`
  - `getReader()`

### getContentAsString

public String getContentAsString() throws IOException
Returns the contents of the specified resource as a string, using the specified `Charset` or encoding (if any).

Throws:
  `IOException` - if opening the resource failed
Since:
  6.0.5
See Also:
  - `Resource.getContentAsString(Charset)`

### equals

public boolean equals(@Nullable Object other)

Overrides:
  `equals` in class `Object`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`

### toString

public String toString()

Overrides:
  `toString` in class `Object`