Class BinData
- All Implemented Interfaces:
AutoCloseable
Build, transform, encode, decode, inspect, and save binary data.
Chilkat.BinData is a general-purpose mutable byte container used
throughout Chilkat for working with binary content in memory. It can load
bytes from files, byte arrays, strings, and encoded text; append additional
data in many forms; retrieve content as raw bytes, chunks, text, or encoded
strings; compute hashes; convert character encodings; validate UTF-8; remove
byte ranges; and write the final result to files.
Load binary content
Load data from files, byte arrays, strings, encoded strings, or other Chilkat objects that produce binary output.
Append and build data
Append bytes, strings, integers, BOMs, counted strings, padded fields,
encoded data, and other BinData objects.
Encode and decode
Convert binary data to and from text encodings such as Base64, hex, URL encoding, and other supported binary encodings.
Text and charset handling
Interpret bytes as text, append strings using a specified charset, convert character encodings, and validate UTF-8 content.
Inspect and modify bytes
Get byte counts, extract chunks, remove ranges, compare data, and work with specific portions of a binary buffer.
Hash and save output
Compute hashes for the contained data and write the resulting bytes to files or pass them to other Chilkat classes.
Common pattern:
Load or append binary data, perform any needed encoding, decoding, hashing,
charset conversion, or byte-level edits, then retrieve the result as bytes,
text, an encoded string, or a file. Use BinData whenever a
Chilkat method needs a mutable in-memory binary buffer.
For an extended overview, see BinData Class Overview.
</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 TypeMethodDescriptionvoidAppends the contents of another BinData to this object.voidAppends the appropriate BOM (byte order mark), also known as apreamble, for the given charset.voidappendByte(int byteValue)Appends a single byte.voidappendCountedString(int numCountBytes, boolean bigEndian, String str, String charset)Appends a byte count followed by the string in the desired character encoding, such asutf-8.voidappendData(byte[] data)Appends binary data to the current contents.voidappendEncoded(String encData, String encoding)Appends encoded binary data to the current data.voidappendEncodedSb(StringBuilder sb, String encoding)Decodes the contents ofsband appends the decoded bytes to this object.voidappendInt2(int value, boolean littleEndian)Appends a 16-bit integer (2 bytes).voidappendInt4(int value, boolean littleEndian)Appends a 32-bit integer (4 bytes).voidappendPadded(String str, String charset, boolean padWithSpace, int fieldLen)Appends a string to this object, padded to thefieldLenwith NULL or SPACE chars.voidappendSb(StringBuilder sb, String charset)Appends the contents of a StringBuilder to this object.voidappendString(String str, String charset)Appends a string to this object.voidbyteSwap(int size)Does either 16-bit or 32-bit byte swapping of the entire contents.voidcharsetConvert(String fromCharset, String toCharset, boolean allOrNone)This method converts the character encoding of an object's contents from one format to another, such as from Windows-1252 to UTF-8.voidclear()Clears the contents.booleancontentsEqual(BinData binData)Returntrueif the contents of this object equals the contents ofbinData.intfindString(String str, int startIdx, String charset)Return the index where the first occurrence ofstris found.intgetByte(int index)Returns the value of the byte at the givenindex.byte[]getData()Retrieves the binary data contained within the object.byte[]getDataChunk(int offset, int numBytes)Retrieves a chunk of the binary data contained within the object.getEncoded(String encoding)Retrieves the binary data as an encoded string.getEncodedChunk(int offset, int numBytes, String encoding)Retrieves a chunk of the binary data and returns it in encoded form.voidgetEncodedSb(String encoding, StringBuilder sb)Writes the encoded data to a StringBuilder.Returns the hash of the contents of this object.intgetInt2(int index, boolean littleEndian)Returns the value of the 16-bit signed integer stored in big-endian or little-endian byte ordering at the given index.intgetInt4(int index, boolean littleEndian)Returns the value of the 32-bit signed integer stored in big-endian or little-endian byte ordering at the given index.intThe number of bytes contained within the object.Interprets the bytes according tocharsetand returns the string.getTextChunk(int startIdx, int numBytes, String charset)ReturnsnumBytesbytes starting atstartIdx.longgetUInt2(int index, boolean littleEndian)Returns the value of the 16-bit unsigned integer stored in big-endian or little-endian byte ordering at the given index.longgetUInt4(int index, boolean littleEndian)Returns the value of the 32-bit unsigned integer stored in big-endian or little-endian byte ordering at the given index.booleanReturnstrueif this object contains valid utf-8 byte sequences.voidloadEncoded(String encData, String encoding)Loads binary data from an encoded string, replacing the current contents, if any.voidLoads data from a file.voidremoveByteVal(int value)Removes bytes having a specified value (0-255).voidremoveChunk(int offset, int numBytes)Removes a chunk of bytes from the binary data.voidSecurely clears the contents by writing 0 bytes to the memory prior to deallocating the internal memory.voidwriteAppendFile(String path)Appends the contents of this object to a file.voidWrites the contents to a file.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
BinData
public BinData()Creates a newBinDataobject.
-
-
Method Details
-
getNumBytes
public int getNumBytes()The number of bytes contained within the object.- Returns:
- the value of the
NumBytesproperty (read-only)
-
appendBd
Appends the contents of another BinData to this object.- Parameters:
binData- thebinDataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendBom
Appends the appropriate BOM (byte order mark), also known as apreamble, for the given charset. If thecharsethas no defined BOM, then nothing is appended. An application would typically call this to append the utf-8, utf-16, or utf-32 BOM.- Parameters:
charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendByte
public void appendByte(int byteValue)Appends a single byte. ThebyteValueshould be a value from 0 to 255.- Parameters:
byteValue- thebyteValueargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendCountedString
Appends a byte count followed by the string in the desired character encoding, such asutf-8. ThenumCountBytesis the size in bytes of the integer to represent the byte count. If 0, then a minimal number of bytes is used. IfbigEndianistrue, the byte count is appended in big endian byte ordering, otherwise little-ending byte ordering. Thestris the string to be appended. Thecharsetis the character encoding, such asutf-8,utf-16,windows-1252, etc.- Parameters:
numCountBytes- thenumCountBytesargumentbigEndian- thebigEndianargumentstr- thestrargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendData
public void appendData(byte[] data)Appends binary data to the current contents.- Parameters:
data- thedataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendEncoded
Appends encoded binary data to the current data. Theencodingmay beBase64,modBase64,base64Url,Base32,Base58,QP(for quoted-printable),URL(for url-encoding),Hex, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
encData- theencDataargument (notnull)encoding- theencodingargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendEncodedSb
Decodes the contents ofsband appends the decoded bytes to this object. Theencodingmay beBase64,modBase64,base64Url,Base32,Base58,QP(for quoted-printable),URL(for url-encoding),Hex, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
sb- thesbargument (notnull)encoding- theencodingargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendInt2
public void appendInt2(int value, boolean littleEndian)Appends a 16-bit integer (2 bytes). IflittleEndianistrue, then the integer bytes are appended in little-endian byte order, otherwise big-endian byte order is used.- Parameters:
value- thevalueargumentlittleEndian- thelittleEndianargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendInt4
public void appendInt4(int value, boolean littleEndian)Appends a 32-bit integer (4 bytes). IflittleEndianistrue, then the integer bytes are appended in little-endian byte order, otherwise big-endian byte order is used.- Parameters:
value- thevalueargumentlittleEndian- thelittleEndianargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendPadded
Appends a string to this object, padded to thefieldLenwith NULL or SPACE chars. IfpadWithSpaceistrue, then SPACE chars are used and the string is not null-terminated. IffieldLenisfalse, then null bytes are used. Thecharsetcontrols the byte representation to use, such asutf-8.Note: This call will always append a total number of bytes equal to
fieldLen. If thestris longer thanfieldLen, the method returnsfalseto indicate failure and nothing is appended.- Parameters:
str- thestrargument (notnull)charset- thecharsetargument (notnull)padWithSpace- thepadWithSpaceargumentfieldLen- thefieldLenargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendSb
Appends the contents of a StringBuilder to this object.- Parameters:
sb- thesbargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
appendString
Appends a string to this object. (This does not append the BOM. If a BOM is required, the AppendBom method can be called to append the appropriate BOM.)- Parameters:
str- thestrargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
byteSwap
public void byteSwap(int size)Does either 16-bit or 32-bit byte swapping of the entire contents. Ifsizeequals 16, then swaps in 2 bytes (e.g., 0x1234 → 0x3412). Ifsizeequals 32, then swaps 4 bytes (e.g., 0x12345678 → 0x78563412)- Parameters:
size- thesizeargument
-
charsetConvert
This method converts the character encoding of an object's contents from one format to another, such as from Windows-1252 to UTF-8. TheallOrNoneparameter determines error handling for unrepresentable bytes. IfallOrNoneistrue, the object's contents remain unchanged when errors occur. Iffalse, error bytes are discarded. The method returnstrueif successful andfalseif errors occur.- Parameters:
fromCharset- thefromCharsetargument (notnull)toCharset- thetoCharsetargument (notnull)allOrNone- theallOrNoneargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
clear
public void clear()Clears the contents.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
contentsEqual
Returntrueif the contents of this object equals the contents ofbinData.- Parameters:
binData- thebinDataargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
findString
Return the index where the first occurrence ofstris found. Return -1 if not found. ThestartIdxindicates the byte index where the search begins. Thecharsetspecifies the byte representation ofstrthat is to be searched. For example, it can beutf-8,windows-1252,ansi,utf-16, etc.- Parameters:
str- thestrargument (notnull)startIdx- thestartIdxargumentcharset- thecharsetargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getByte
public int getByte(int index)Returns the value of the byte at the givenindex. The returned value is an integer from 0 to 255.- Parameters:
index- theindexargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getData
public byte[] getData()Retrieves the binary data contained within the object.- Returns:
- the bytes (an empty array when there are none)
-
getDataChunk
public byte[] getDataChunk(int offset, int numBytes)Retrieves a chunk of the binary data contained within the object.- Parameters:
offset- theoffsetargumentnumBytes- thenumBytesargument- Returns:
- the bytes (an empty array when there are none)
-
getEncoded
Retrieves the binary data as an encoded string. Theencodingmay beBase64,modBase64,base64Url,Base32,Base58,QP(for quoted-printable),URL(for url-encoding),Hex, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
encoding- theencodingargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getEncodedChunk
Retrieves a chunk of the binary data and returns it in encoded form. Theencodingmay beBase64,modBase64,base64Url,Base32,Base58,QP(for quoted-printable),URL(for url-encoding),Hex, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
offset- theoffsetargumentnumBytes- thenumBytesargumentencoding- theencodingargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getEncodedSb
Writes the encoded data to a StringBuilder. Theencodingmay beBase64,modBase64,base64Url,Base32,Base58,QP(for quoted-printable),URL(for url-encoding),Hex, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
encoding- theencodingargument (notnull)sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getHash
Returns the hash of the contents of this object. Thealgorithmis the hash algorithm, and can besha1,sha256,sha384,sha512,sha3-224,sha3-256,sha3-384,sha3-512,md2,md5,ripemd128,ripemd160,ripemd256, orripemd320. Theencodingcan bebase64,modBase64,base64Url,base32,base58,qp(for quoted-printable),url(for url-encoding),hex,hexLower, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
algorithm- thealgorithmargument (notnull)encoding- theencodingargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getInt2
public int getInt2(int index, boolean littleEndian)Returns the value of the 16-bit signed integer stored in big-endian or little-endian byte ordering at the given index.- Parameters:
index- theindexargumentlittleEndian- thelittleEndianargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getInt4
public int getInt4(int index, boolean littleEndian)Returns the value of the 32-bit signed integer stored in big-endian or little-endian byte ordering at the given index.- Parameters:
index- theindexargumentlittleEndian- thelittleEndianargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getString
Interprets the bytes according tocharsetand returns the string. Thecharsetcan beutf-8,utf-16,ansi,iso-8859-*,windows-125*, or any of the supported character encodings listed in the link below.- Parameters:
charset- thecharsetargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getTextChunk
ReturnsnumBytesbytes starting atstartIdx. The bytes are interpreted according tocharset(for example,utf-8,ansi,utf-16,windows-1252, etc.)- Parameters:
startIdx- thestartIdxargumentnumBytes- thenumBytesargumentcharset- thecharsetargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getUInt2
public long getUInt2(int index, boolean littleEndian)Returns the value of the 16-bit unsigned integer stored in big-endian or little-endian byte ordering at the given index.- Parameters:
index- theindexargumentlittleEndian- thelittleEndianargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getUInt4
public long getUInt4(int index, boolean littleEndian)Returns the value of the 32-bit unsigned integer stored in big-endian or little-endian byte ordering at the given index.- Parameters:
index- theindexargumentlittleEndian- thelittleEndianargument- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
isValidUtf8
public boolean isValidUtf8()Returnstrueif this object contains valid utf-8 byte sequences. Any invalid byte sequence that is not valid utf-8 will cause this function to returnfalse.- Returns:
- the result; this method never throws
ChilkatException
-
loadEncoded
Loads binary data from an encoded string, replacing the current contents, if any. Theencodingmay beBase64,modBase64,base64Url,Base32,Base58,QP(for quoted-printable),URL(for url-encoding),Hex, or any of the encodings found at Chilkat Binary Encodings List.- Parameters:
encData- theencDataargument (notnull)encoding- theencodingargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadFile
Loads data from a file.- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
removeByteVal
public void removeByteVal(int value)Removes bytes having a specified value (0-255). For example, to remove all null bytes, pass 0 invalue.- Parameters:
value- thevalueargument
-
removeChunk
public void removeChunk(int offset, int numBytes)Removes a chunk of bytes from the binary data.- Parameters:
offset- theoffsetargumentnumBytes- thenumBytesargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
secureClear
public void secureClear()Securely clears the contents by writing 0 bytes to the memory prior to deallocating the internal memory.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
writeAppendFile
Appends the contents of this object to a file.- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
writeFile
Writes the contents to a file.- Parameters:
path- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-