Class Charset
- All Implemented Interfaces:
AutoCloseable
Legacy character-set conversion utilities. Migrate before Chilkat v12.0.0.
Do not use Chilkat.Charset in new development.
The entire class is deprecated and is scheduled to be removed in Chilkat
v12.0.0. Existing applications should migrate now. Equivalent functionality
is available in the free Chilkat.BinData and
Chilkat.StringBuilder classes.
Chilkat.Charset is a legacy class for converting text between
character encodings, reading and writing encoded text files, inspecting or
rewriting HTML charset declarations, encoding and decoding HTML entities,
validating byte sequences, and performing several older text utilities.
It remains documented only to support maintenance and migration of existing
applications.
Use BinData for bytes
Store raw bytes, load and save files, append byte-order marks, and move encoded data between files, memory, and other Chilkat objects.
Use StringBuilder for text
Hold Unicode text and perform charset encoding or decoding, URL processing, HTML entity conversion, case conversion, and related text operations.
Prefer UTF-8
For new text formats and interchange, use UTF-8 unless a protocol or legacy system explicitly requires another encoding.
Keep bytes and characters distinct
A charset defines how text characters map to bytes. Binary encodings such as Base64 or hexadecimal solve a different problem and are not character sets.
Legacy HTML helpers
The class can inspect and rewrite HTML charset metadata, but these helpers do not validate that declared metadata matches the actual bytes.
Maintenance only
Continue using this class only while maintaining older code, and plan a tested migration before adopting Chilkat v12.
Migration pattern:
load encoded bytes into BinData, decode them into a
StringBuilder using the known source charset, work with Unicode
text, then encode back into BinData using the required
destination charset. This makes the boundary between bytes and text explicit
and replaces the stateful FromCharset/ToCharset
workflow.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcharsetToCodePage(String charsetName)Returns the numeric Windows code-page identifier corresponding tocharsetName.codePageToCharset(int codePage)Returns the charset name corresponding to the numeric code-page identifier incodePage.voidconvertFile(String inPath, String destPath)Reads the file atinPath, converts its text fromtogetFromCharset(), and writes the converted bytes to the file atgetToCharset()destPath.voidconvertFileNoPreamble(String inPath, String destPath)Reads the file atinPath, converts its text fromtogetFromCharset(), and writes the converted bytes togetToCharset()destPathwithout adding a byte-order mark or other encoding preamble.voidconvertHtmlFile(String inPath, String destPath)entityEncodeDec(String str)Returnsstrwith each non-US-ASCII character replaced by a decimal HTML/XML numeric character reference.entityEncodeHex(String str)Returnsstrwith each non-US-ASCII character replaced by a hexadecimal HTML/XML numeric character reference.Specifies the alternate destination character encoding used only whenisgetErrorAction()6.intControls what happens when an input character has been decoded successfully but cannot be represented in the destination charset.Specifies the character encoding of the input bytes for conversion operations.getHtmlFileCharset(String htmlFilePath)Examines the HTML file athtmlFilePathand returns the charset name declared by an HTML<meta>element, when present.Returns the input bytes captured for the most recent conversion performed whilewas enabled, represented as hexadecimal text.getSaveLast()Returns the input bytes captured for the most recent conversion performed whilewas enabled, represented as quoted-printable text.getSaveLast()Returns the output bytes captured for the most recent conversion performed whilewas enabled, represented as hexadecimal text.getSaveLast()Returns the output bytes captured for the most recent conversion performed whilewas enabled, represented as quoted-printable text.getSaveLast()booleanWhentrue, retains the input and output byte sequences from each conversion so they can be inspected through,getLastInputAsHex(),getLastInputAsQP(), andgetLastOutputAsHex().getLastOutputAsQP()Specifies the destination character encoding for conversion operations.htmlDecodeToStr(String inStr)Decodes HTML named entities and numeric character references ininStrand returns the resulting Unicode string.voidhtmlEntityDecodeFile(String inPath, String destPath)Reads the file atinPath, decodes HTML named entities and numeric character references, and writes the transformed content todestPath.Returns the lowercase form ofinStrusing Chilkat’s Unicode case conversion.readFileToString(String path, String charset)Reads the text file atpath, decodes its bytes using the charset named bycharset, and returns the resulting Unicode string.voidsetAltToCharset(String value)Sets theAltToCharsetproperty; seegetAltToCharset().voidsetErrorAction(int value)Sets theErrorActionproperty; seegetErrorAction().voidsetErrorString(String str, String charset)Configures the replacement used whenisgetErrorAction()1.voidsetFromCharset(String value)Sets theFromCharsetproperty; seegetFromCharset().voidsetSaveLast(boolean value)Sets theSaveLastproperty; seegetSaveLast().voidsetToCharset(String value)Sets theToCharsetproperty; seegetToCharset().Returns the uppercase form ofinStrusing Chilkat’s Unicode case conversion.urlDecodeStr(String inStr)Percent-decodesinStrand interprets the resulting bytes using the platform’s legacy ANSI character encoding.booleanverifyFile(String charset, String path)Returnstruewhen the contents of the file atpathform a valid byte sequence for the charset named bycharset; otherwise returnsfalse.voidwriteStringToFile(String textData, String path, String charset)Encodes the Unicode text intextDatausing the charset named bycharsetand writes the resulting bytes to the file atpath.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Charset
public Charset()Creates a newCharsetobject.
-
-
Method Details
-
getAltToCharset
Specifies the alternate destination character encoding used only whenisgetErrorAction()6.If a decoded input character cannot be represented in
, Chilkat attempts to encode that character using this alternate charset. If the alternate conversion also fails, the character is omitted from the output.getToCharset()Mixed-encoding output: The result can contain a mixture of byte encodings. Such output is usually unsuitable for interchange unless the receiving format explicitly supports this behavior.
- Returns:
- the value of the
AltToCharsetproperty
-
setAltToCharset
Sets theAltToCharsetproperty; seegetAltToCharset().- Parameters:
value- the new value (notnull)
-
getErrorAction
public int getErrorAction()Controls what happens when an input character has been decoded successfully but cannot be represented in the destination charset.Value Action
0Drop the unrepresentable character.1Insert the replacement configured by.setErrorString(String, String)2Emit a hexadecimal numeric character reference such as€.3–5Reserved. Do not use.6Encode the character using; drop it if that also fails.getAltToCharset()7Pass the nonconvertible data through unchanged.Data-integrity warning: Values
0,6, and7can silently lose data or produce output that is not valid in the declared destination charset. For new code, prefer an explicit failure or replacement policy in the replacement API.- Returns:
- the value of the
ErrorActionproperty
-
setErrorAction
public void setErrorAction(int value)Sets theErrorActionproperty; seegetErrorAction().- Parameters:
value- the new value
-
getFromCharset
Specifies the character encoding of the input bytes for conversion operations. Chilkat decodes the source bytes using this charset before encoding the resulting Unicode text using.getToCharset()Family Recognized names
Unicode and ASCII
us-ascii,utf-7,utf-8,unicode/utf-16/utf-16le,unicodefffe/utf-16be,utf-32,utf-32beISO and Windows
iso-8859-1,-2,-3,-4,-5,-6,-7,-8,-9,-13,-15;windows-874,windows-1250throughwindows-1258East Asian
shift_jis,gb2312,ks_c_5601-1987,big5,iso-2022-jp,iso-2022-kr,euc-jp,euc-krMacintosh
macintosh,x-mac-japanese,x-mac-chinesetrad,x-mac-korean,x-mac-arabic,x-mac-hebrew,x-mac-greek,x-mac-cyrillic,x-mac-chinesesimp,x-mac-romanian,x-mac-ukrainian,x-mac-thai,x-mac-ce,x-mac-icelandic,x-mac-turkish,x-mac-croatianDOS, IBM, and legacy
ebcdic,asmo-708,dos-720,dos-862,ibm01140throughibm01149,ibm037,ibm437,ibm500,ibm737,ibm775,ibm850,ibm852,ibm855,ibm857,ibm00858,ibm860,ibm861,ibm863,ibm864,ibm865,cp866,ibm869,ibm870,cp875,koi8-r,koi8-uSource encoding:
unicode,utf-16, andutf-16leidentify little-endian UTF-16.unicodefffeandutf-16beidentify big-endian UTF-16. Except where a method explicitly documents HTML metadata detection, set the source charset explicitly rather than assuming automatic detection.- Returns:
- the value of the
FromCharsetproperty
-
setFromCharset
Sets theFromCharsetproperty; seegetFromCharset().- Parameters:
value- the new value (notnull)
-
getLastInputAsHex
Returns the input bytes captured for the most recent conversion performed whilewas enabled, represented as hexadecimal text.getSaveLast()Diagnostic snapshot: This is a diagnostic representation of the raw bytes. It does not perform another charset conversion, and the quoted-printable form is intended to make otherwise nonprintable bytes easier to inspect.
- Returns:
- the value of the
LastInputAsHexproperty (read-only)
-
getLastInputAsQP
Returns the input bytes captured for the most recent conversion performed whilewas enabled, represented as quoted-printable text.getSaveLast()Diagnostic snapshot: This is a diagnostic representation of the raw bytes. It does not perform another charset conversion, and the quoted-printable form is intended to make otherwise nonprintable bytes easier to inspect.
- Returns:
- the value of the
LastInputAsQPproperty (read-only)
-
getLastOutputAsHex
Returns the output bytes captured for the most recent conversion performed whilewas enabled, represented as hexadecimal text.getSaveLast()Diagnostic snapshot: This is a diagnostic representation of the raw bytes. It does not perform another charset conversion, and the quoted-printable form is intended to make otherwise nonprintable bytes easier to inspect.
- Returns:
- the value of the
LastOutputAsHexproperty (read-only)
-
getLastOutputAsQP
Returns the output bytes captured for the most recent conversion performed whilewas enabled, represented as quoted-printable text.getSaveLast()Diagnostic snapshot: This is a diagnostic representation of the raw bytes. It does not perform another charset conversion, and the quoted-printable form is intended to make otherwise nonprintable bytes easier to inspect.
- Returns:
- the value of the
LastOutputAsQPproperty (read-only)
-
getSaveLast
public boolean getSaveLast()Whentrue, retains the input and output byte sequences from each conversion so they can be inspected through,getLastInputAsHex(),getLastInputAsQP(), andgetLastOutputAsHex().getLastOutputAsQP()Debugging only: Enable this only while troubleshooting. Retaining both sides of a large conversion increases memory use and may keep sensitive plaintext in memory longer than necessary.
- Returns:
- the value of the
SaveLastproperty
-
setSaveLast
public void setSaveLast(boolean value)Sets theSaveLastproperty; seegetSaveLast().- Parameters:
value- the new value
-
getToCharset
Specifies the destination character encoding for conversion operations. After decoding the source bytes using, Chilkat encodes the resulting Unicode text in this charset.getFromCharset()Family Recognized names
Unicode and ASCII
us-ascii,utf-7,utf-8,unicode/utf-16/utf-16le,unicodefffe/utf-16be,utf-32,utf-32beISO and Windows
iso-8859-1,-2,-3,-4,-5,-6,-7,-8,-9,-13,-15;windows-874,windows-1250throughwindows-1258East Asian
shift_jis,gb2312,ks_c_5601-1987,big5,iso-2022-jp,iso-2022-kr,euc-jp,euc-krMacintosh
macintosh,x-mac-japanese,x-mac-chinesetrad,x-mac-korean,x-mac-arabic,x-mac-hebrew,x-mac-greek,x-mac-cyrillic,x-mac-chinesesimp,x-mac-romanian,x-mac-ukrainian,x-mac-thai,x-mac-ce,x-mac-icelandic,x-mac-turkish,x-mac-croatianDOS, IBM, and legacy
ebcdic,asmo-708,dos-720,dos-862,ibm01140throughibm01149,ibm037,ibm437,ibm500,ibm737,ibm775,ibm850,ibm852,ibm855,ibm857,ibm00858,ibm860,ibm861,ibm863,ibm864,ibm865,cp866,ibm869,ibm870,cp875,koi8-r,koi8-uChoosing an output encoding: For new text interchange,
utf-8is normally the preferred destination. Legacy single-byte encodings cannot represent every Unicode character;controls how such conversion failures are handled.getErrorAction()- Returns:
- the value of the
ToCharsetproperty
-
setToCharset
Sets theToCharsetproperty; seegetToCharset().- Parameters:
value- the new value (notnull)
-
charsetToCodePage
Returns the numeric Windows code-page identifier corresponding tocharsetName. For example,iso-8859-1maps to28591, andutf-8maps to65001.Code pages: A code page number is a legacy Windows identifier for a character encoding. Internet protocols and modern APIs normally use charset names such as
utf-8rather than numeric code pages.- Parameters:
charsetName- thecharsetNameargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
codePageToCharset
Returns the charset name corresponding to the numeric code-page identifier incodePage. For example,65001maps toutf-8. The operation fails when the code page is not recognized.Name normalization: Different aliases can identify the same encoding. The returned value is Chilkat’s canonical name for the recognized code page, not necessarily the alias originally used by another application.
- Parameters:
codePage- thecodePageargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
convertFile
Reads the file atinPath, converts its text fromtogetFromCharset(), and writes the converted bytes to the file atgetToCharset()destPath.When the destination charset is
utf-8orutf-16, this method writes the corresponding byte-order mark (BOM) at the beginning of the output. Usewhen the output must not contain a BOM.convertFileNoPreamble(String, String)BOM behavior: A BOM is a leading byte sequence that can identify a Unicode encoding or byte order. UTF-8 does not require a BOM, and many modern formats prefer UTF-8 without one.
- Parameters:
inPath- theinPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
convertFileNoPreamble
Reads the file atinPath, converts its text fromtogetFromCharset(), and writes the converted bytes togetToCharset()destPathwithout adding a byte-order mark or other encoding preamble.UTF-8 output: This is generally the appropriate choice for UTF-8 files unless the consuming system specifically requires a UTF-8 BOM.
- Parameters:
inPath- theinPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
convertHtmlFile
Converts the HTML file atinPathtoand writes the result togetToCharset()destPath. Chilkat also updates the HTML<meta>charset declaration to identify the destination encoding.If
is empty, Chilkat attempts to obtain the source charset from an HTMLgetFromCharset()<meta>declaration. Otherwise, the explicitly configured source charset is used.Detection limitation: A declared charset is metadata, not proof of the file’s actual encoding. Incorrect or missing declarations can cause decoding errors; use an explicit source charset when it is known.
- Parameters:
inPath- theinPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
entityEncodeDec
Returnsstrwith each non-US-ASCII character replaced by a decimal HTML/XML numeric character reference. For example,ébecomesé.Entity encoding: Numeric character references describe Unicode characters; they are not a character-set conversion and do not make arbitrary binary data safe for HTML.
- Parameters:
str- thestrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
entityEncodeHex
Returnsstrwith each non-US-ASCII character replaced by a hexadecimal HTML/XML numeric character reference. For example,ébecomesé.Entity encoding: Decimal and hexadecimal numeric references represent the same Unicode character; only the textual notation differs.
- Parameters:
str- thestrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getHtmlFileCharset
Examines the HTML file athtmlFilePathand returns the charset name declared by an HTML<meta>element, when present.Metadata only: The method reads document metadata only. A missing, stale, or incorrect declaration does not reveal the file’s true encoding.
- Parameters:
htmlFilePath- thehtmlFilePathargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
htmlDecodeToStr
Decodes HTML named entities and numeric character references ininStrand returns the resulting Unicode string. Examples include<,&,é, andé.Security context: Entity decoding is a text transformation, not HTML parsing or sanitization. Decoding untrusted text can reintroduce markup-significant characters such as
<,>, and&.- Parameters:
inStr- theinStrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
htmlEntityDecodeFile
Reads the file atinPath, decodes HTML named entities and numeric character references, and writes the transformed content todestPath.Content transformation: The output may contain literal markup-significant characters that were previously escaped. This operation does not parse, validate, or sanitize HTML.
- Parameters:
inPath- theinPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
lowerCase
Returns the lowercase form ofinStrusing Chilkat’s Unicode case conversion.Unicode casing: Case conversion can change string length and is not the same operation as locale-aware collation or case-insensitive comparison.
- Parameters:
inStr- theinStrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
readFileToString
Reads the text file atpath, decodes its bytes using the charset named bycharset, and returns the resulting Unicode string.Charset must match the file: The caller must supply the actual source encoding. Choosing the wrong charset can produce replacement characters or silently incorrect text even when the file can be decoded.
- Parameters:
path- thepathargument (notnull)charset- thecharsetargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setErrorString
Configures the replacement used whenisgetErrorAction()1.stris the replacement text, andcharsetidentifies the charset used to encode that replacement into the conversion output.Replacement behavior: Choose replacement text that is representable in the specified charset. A common replacement is
?, but explicit error handling is safer when data loss is unacceptable.- Parameters:
str- thestrargument (notnull)charset- thecharsetargument (notnull)
-
upperCase
Returns the uppercase form ofinStrusing Chilkat’s Unicode case conversion.Unicode casing: Case conversion can change string length and is not the same operation as locale-aware collation or case-insensitive comparison.
- Parameters:
inStr- theinStrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
urlDecodeStr
Percent-decodesinStrand interprets the resulting bytes using the platform’s legacy ANSI character encoding.For example, in a Western Windows code page,
%C9may decode asÉ. UTF-8 input would encode the same character as%C3%89and is therefore not handled correctly by this legacy assumption.Legacy ANSI behavior: Do not use this method for modern URLs or form data unless the data is known to use the applicable ANSI code page. Use
StringBuilderURL decoding with an explicit charset, normallyutf-8.- Parameters:
inStr- theinStrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
verifyFile
Returnstruewhen the contents of the file atpathform a valid byte sequence for the charset named bycharset; otherwise returnsfalse.Validation limitation: A successful result means only that the bytes are structurally valid for that encoding. It does not identify the encoding or confirm that the decoded text is semantically correct.
- Parameters:
charset- thecharsetargument (notnull)path- thepathargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
writeStringToFile
Encodes the Unicode text intextDatausing the charset named bycharsetand writes the resulting bytes to the file atpath.Destination encoding: The charset argument is the third parameter. Select
utf-8for modern interchange unless a receiving system requires a specific legacy encoding.- Parameters:
textData- thetextDataargument (notnull)path- thepathargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-