# Class CharsetHolder

java.lang.Object
org.apache.tomcat.util.buf.CharsetHolder

public class CharsetHolder extends Object
Represents a character encoding to be used for a request or response.

Historically the Servlet API has used Strings for this information with lazy conversion to Charset. Sometimes the API required that an invalid name triggered an exception. Sometimes the invalid name was treated as if it had never been set. This resulted in classes storing both the String and, if the name was valid, the Charset with validation and conversion logic spread throughout those classes.

This class is an attempt to encapsulate that behaviour.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final CharsetHolder`
`EMPTY`
An empty holder with no charset set.

## Method Summary

Modifier and Type
Method
Description
`Charset`
`getCharset()`
Returns the Charset, `null` if no Charset has been specified, or `null` if the holder was created using the name of a Charset that the JRE does not recognise.
`static CharsetHolder`
`getInstance(String name)`
Creates a CharsetHolder from the given charset name.
`static CharsetHolder`
`getInstance(Charset encoding)`
Creates a CharsetHolder from the given Charset.
`String`
`getName()`
Returns the charset name.
`Charset`
`getValidatedCharset()`
Returns the Charset or `null` if no Charset has been specified.
`void`
`validate()`
Throws an exception if the instance holds a name that without a matching Charset.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### EMPTY

public static final CharsetHolder EMPTY
An empty holder with no charset set.

## Method Details

### getInstance

public static CharsetHolder getInstance(String name)
Creates a CharsetHolder from the given charset name.

Parameters:
  `name` - the charset name
Returns:
  the holder

### getInstance

public static CharsetHolder getInstance(Charset encoding)
Creates a CharsetHolder from the given Charset.

Parameters:
  `encoding` - the charset
Returns:
  the holder

### getName

public String getName()
Returns the charset name.

Returns:
  the charset name

### getCharset

public Charset getCharset()
Returns the Charset, `null` if no Charset has been specified, or `null` if the holder was created using the name of a Charset that the JRE does not recognise.

Returns:
  The Charset or `null` it is not set or invalid

### getValidatedCharset

public Charset getValidatedCharset() throws UnsupportedEncodingException
Returns the Charset or `null` if no Charset has been specified.

Returns:
  The validated Charset or `null` if is not set
Throws:
  `UnsupportedEncodingException` - if the holder was created using the name of a Charset that the JRE does not recognise

### validate

public void validate() throws UnsupportedEncodingException
Throws an exception if the instance holds a name that without a matching Charset.

Throws:
  `UnsupportedEncodingException` - if the holder contains a name without a matching Charset