michel-kraemer/bson4jackson — Issue #19: Threading problems with CharsetDecoder
Author: endasb
Created: 2012-01-27 20:23:22+00:00
State: open

I've encountered a another issue with the CharsetDecoder, I wrote a small test in my branch to reproduce the problem. When more than one thread is trying to decode UTF strings, the static decoder which is keeping state will throw the following error.

Caused by: java.lang.IllegalStateException: Current state = CODING_END, new state = CODING
at java.nio.charset.CharsetDecoder.throwIllegalStateException(CharsetDecoder.java:951)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:537)
at de.undercouch.bson4jackson.io.LittleEndianInputStream.readUTF(LittleEndianInputStream.java:312)
at de.undercouch.bson4jackson.io.LittleEndianInputStream.readUTF(LittleEndianInputStream.java:264)
at de.undercouch.bson4jackson.BsonParser.readString(BsonParser.java:506)
at de.undercouch.bson4jackson.BsonParser.nextToken(BsonParser.java:228)

I'm not sure what the penalty of instantiating a new decoder per instance is, so I made a change to make it ThreadLocal. If this solution makes sense to you, I can generate another pull request.