Java.lang.IllegalArgumentException: Non-positive maxBytesPerChar

We have a multi-threaded Java application that works with files and initializes the encoding of the encoding, as shown below.

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 

Recently, we accidentally saw an exception below during execution, when we try to process the same file, it is processed without any errors, google does not help, since we could not find anything with a similar error,

 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 

Appreciate if someone can provide any help, direction in this.

I can not find the full source code for jdk 5 (the source, which I do not have code for sun. * Packages). I decompiled the Encoder class, and I don’t see how this is possible as the code progresses, the hard-coded value "1.0" here.

 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); } 

I have a CharsetEncoder source as shown below that gets a value <0 if the encoder passed 1.0

 protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar) { this(cs, averageBytesPerChar, maxBytesPerChar, new byte[] { (byte)'?' }); } 

"This" calls the function below

  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"); 
+4
source share
1 answer

Take a look: http://docs.oracle.com/javase/7/docs/api/java/nio/charset/CharsetEncoder.html

The end of the text at the beginning of the API says: "Instances of this class are unsafe for use by multiple parallel threads."

Are you using a single CharsetEncoder object in multiple threads?

0
source

All Articles