CharsetEncoder encode(CharBuffer in) method in Java with Examples
The encode(CharBuffer input) method is a built-in method of the java.nio.charset.CharsetEncoder which encodes the content which is remaining of a single input character buffer to a newly-allocated byte-buffer. The encode() method in itslef implements an entire operation of encoding. This function should not be invoked if the operation is in progress.
Syntax:
public final ByteBuffer encode(CharBuffer input)
Parameters: The function accepts a mandatory parameter input which specifies the input character buffer.
Return Value: The function returns a newly-allocated byte buffer containing the result of the encoding operation.
Error and Exceptions: The function throws four exceptions which can be described as below:
- IllegalStateException: It is thrown if an encoding operation is already in progress.
- MalformedInputException: It is thrown if the character sequence starting at the input buffer’s current position is not a legal sixteen-bit Unicode sequence and the current malformed-input action is CodingErrorAction.REPORT.
- UnmappableCharacterException: It is thrown if the character sequence starting at the input buffer’s current position cannot be mapped to an equivalent byte sequence and the current unmappable-character action is CodingErrorAction.REPORT
- CharacterCodingException
Below is the implementation of the above function:
Program 1:
// Java program to implement // the above function import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; public class Main { public static void main(String[] args) throws Exception { // Gets the new encoder CharsetEncoder encoder = Charset.forName("UTF8").newEncoder(); // Encodes String res = "gfggfg"; System.out.println(encoder.encode(CharBuffer.wrap(res))); } } |
java.nio.HeapByteBuffer[pos=0 lim=6 cap=6]
Program 2:
// Java program to implement // the above function import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; public class Main { public static void main(String[] args) throws Exception { // Gets the new encoder CharsetEncoder encoder = Charset.forName("UTF16").newEncoder(); // Encodes String res = "gopal"; System.out.println(encoder.encode(CharBuffer.wrap(res))); } } |
java.nio.HeapByteBuffer[pos=0 lim=12 cap=21]
The exception programs cannot be demonstrated in programs.
Recommended Posts:
- CharsetEncoder averageBytesPerChar() method in Java with Examples
- CharsetEncoder malformedInputAction() method in Java with Examples
- CharsetEncoder isLegalReplacement() method in Java with Examples
- CharsetEncoder unmappableCharacterAction() method in Java with Examples
- CharsetEncoder maxBytesPerChar() method in Java with Examples
- CharsetEncoder replacement() method in Java with Examples
- CharsetEncoder reset() method in Java with Examples
- CharsetEncoder charset() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


