Error when writing string data to file

I’ve been having a really odd exception in the Java layer on iOS emulated by MOE runtime. Here is a stack trace below. I wonder anyone experienced anything similar and if there is any work around for this? Seem to be related to some buffering of encoded String data, but it doesn’t look related to any specific encoding in the content and just the size of the data written

The Write instance is created like this:

Writer w = new OutputStreamWriter(new FileOutputStream(fileName), "utf-8");

@kisg any suggestions?

Fatal Exception: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: Bad position (limit 8192): 8196
  at java.nio.Buffer.positionImpl(Buffer.java:351)
  at java.nio.Buffer.position(Buffer.java:345)
  at java.nio.charset.CharsetEncoderICU.setPosition(CharsetEncoderICU.java:234)
  at java.nio.charset.CharsetEncoderICU.encodeLoop(CharsetEncoderICU.java:183)
  at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:374)
  at java.io.OutputStreamWriter.convert(OutputStreamWriter.java:181)
  at java.io.OutputStreamWriter.write(OutputStreamWriter.java:327)
  at java.io.Writer.write(Writer.java:141)
 ...

I’m still seeing these exceptions. Anyone has any idea how to work around it or chase it down? It looks as a crash in native code when writing data to a file

Can you share a minimum project that can reproduce this problem?

Here you go. Create a blank new MOE / Java Single View application and replace the Main class with attached one. Then launch on iPad simulator (tried with iOS 8.2 and 12.1).

I’m always getting this:

java.lang.IllegalArgumentException: Bad position (limit 8192): 8196
	at java.nio.Buffer.positionImpl(Buffer.java:351)
	at java.nio.Buffer.position(Buffer.java:345)
	at java.nio.charset.CharsetEncoderICU.setPosition(CharsetEncoderICU.java:234)
	at java.nio.charset.CharsetEncoderICU.encodeLoop(CharsetEncoderICU.java:183)
	at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:374)
	at java.io.OutputStreamWriter.convert(OutputStreamWriter.java:181)
	at java.io.OutputStreamWriter.write(OutputStreamWriter.java:327)
	at java.io.Writer.write(Writer.java:141)
	at org.test.Main.main(Main.java:53)

PS: While making this I noticed that I can mitigate this crash by manually buffering chunks of output trough StringWriter. Not sure if that fixes issue or just making it less probable.

Main.txt (2.1 KB)