use*_*430 5 java character-encoding
我们有多线程 java 应用程序,它正在执行文件操作和初始化字符集编码,如下所示。
Charset charset;
CharsetDecoder decoder;
CharsetEncoder encoder;
String charsetCoding = CharsetUtil.getJVMCharset();
charset = Charset.forName(charsetCoding);
decoder = charset.newDecoder();
encoder = charset.newEncoder(); // Exception is thrown from this line
Run Code Online (Sandbox Code Playgroud)
我们最近开始在执行过程中随机看到下面的异常,当我们尝试重新处理同一个文件时,它被处理没有任何错误,谷歌没有帮助,因为我们找不到任何类似的错误,
Caused by: java.lang.IllegalArgumentException: Non-positive maxBytesPerChar
at java.nio.charset.CharsetEncoder.<init>(CharsetEncoder.java:175)
at java.nio.charset.CharsetEncoder.<init>(CharsetEncoder.java:209)
at sun.nio.cs.ISO_8859_1$Encoder.<init>(ISO_8859_1.java:116)
at sun.nio.cs.ISO_8859_1$Encoder.<init>(ISO_8859_1.java:113)
at sun.nio.cs.ISO_8859_1.newEncoder(ISO_8859_1.java:46)
at myClass.readFile
Run Code Online (Sandbox Code Playgroud)
感谢有人可以提供任何帮助,这方面的方向。
我似乎找不到 jdk 5 的完整源代码(我的源代码不包含 sun.* 包的代码)我反编译了 Encoder 类,但我看不出这怎么可能,因为代码正在传递硬编码值“1.0”在这里。
class ISO_8859_1$Encoder extends CharsetEncoder
{
private final Surrogate.Parser sgp = new Surrogate.Parser();
private ISO_8859_1$Encoder(Charset paramCharset)
{
super(paramCharset, 1.0F, 1.0F);
}
Run Code Online (Sandbox Code Playgroud)
我有 CharsetEncoder 的来源如下,即使编码器通过了 1.0,它的值也小于 0
protected CharsetEncoder(Charset cs,
float averageBytesPerChar,
float maxBytesPerChar)
{
this(cs,
averageBytesPerChar, maxBytesPerChar,
new byte[] { (byte)'?' });
}
Run Code Online (Sandbox Code Playgroud)
“这个”调用下面的函数
protected
CharsetEncoder(Charset cs,
float averageBytesPerChar,
float maxBytesPerChar,
byte[] replacement)
{
this.charset = cs;
if (averageBytesPerChar <= 0.0f)
throw new IllegalArgumentException("Non-positive "
+ "averageBytesPerChar");
if (maxBytesPerChar <= 0.0f)
throw new IllegalArgumentException("Non-positive "
+ "maxBytesPerChar");***
if (!Charset.atBugLevel("1.4")) {
if (averageBytesPerChar > maxBytesPerChar)
throw new IllegalArgumentException("averageBytesPerChar"
+ " exceeds "
+ "maxBytesPerChar");
Run Code Online (Sandbox Code Playgroud)
看一下:http://docs.oracle.com/javase/7/docs/api/java/nio/charset/CharsetEncoder.html
在 API 开头的文本末尾,它说:“此类的实例对于多个并发线程使用不安全。”
您是否在多个线程中使用单个 CharsetEncoder 对象?