maxCharsPerByte()方法是java.nio.charset.CharsetDecoder类的内置方法,该方法返回为每个输入字节生成的最大字符数。该值可用于计算给定输入序列所需的输出缓冲区的最坏情况大小。
用法:
public final float maxCharsPerByte()
参数:该函数不接受任何参数。
返回值:该函数返回每个输入字节产生的最大字符数。
下面是上述功能的实现:
示例1:
// Java program to demonstrate
// the above function
import java.nio.charset.*;
import java.util.Iterator;
import java.util.Map;
public class GFG {
public static void main(String[] args)
{
// Gets the charset
Charset charset = Charset.forName("ISO-2022-CN");
// Get the CharsetDecoder
CharsetDecoder decoder = charset.newDecoder();
// Prints the maxCharsPerByte
System.out.println("The maxCharsPerByte for "
+ "ISO-2022-CN is "
+ decoder.maxCharsPerByte());
}
}
输出:
The maxCharsPerByte for ISO-2022-CN is 1.0
示例2:
// Java program to demonstrate
// the above function
import java.nio.charset.*;
import java.util.Iterator;
import java.util.Map;
public class GFG {
public static void main(String[] args)
{
// Gets the charset
Charset charset = Charset.forName("x-windows-949");
// Get the CharsetDecoder
CharsetDecoder decoder = charset.newDecoder();
// Prints the maxCharsPerByte
System.out.println("The maxCharsPerByte for "
+ "ISO-2022-CN is "
+ decoder.maxCharsPerByte());
}
}
输出:
The maxCharsPerByte for ISO-2022-CN is 1.0