This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit cdf27661c972ff0406f11b0242430fcbf639bfe1 Author: Benoit TELLIER <[email protected]> AuthorDate: Sat Nov 21 07:08:26 2026 +0100 [FIX] TextInputStream: flush the encoder at the end of the text fillBuffer called CharsetEncoder#encode(..., true) but never flush, which the CharsetEncoder contract requires to write the final bytes of a stateful encoding. A text body built from a String in ISO-2022-JP and ending in kanji, as when a mailet re-encodes a received part in its charset, was written without its final ESC ( B: the body ended in JIS X 0208 mode (RFC 1468: the text must end in ASCII). Once all the input is encoded, the encoder is flushed, and the stream ends only after the flush. reset() resets the encoder, which can then encode again from the mark. Fixes #142 --- .../apache/james/mime4j/io/TextInputStream.java | 35 +++++++++++++++---- .../james/mime4j/io/TextInputStreamTest.java | 40 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java b/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java index ed4e5257..c39b8f55 100644 --- a/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java @@ -39,6 +39,10 @@ class TextInputStream extends InputStream { private final ByteBuffer bbuf; private int mark = -1; + // encode(..., true) consumed all the input + private boolean encoded; + // flush wrote the final bytes of a stateful encoding, such as the ESC ( B of ISO-2022-JP + private boolean flushed; TextInputStream(final CharSequence s, final Charset charset, int bufferSize) { super(); @@ -52,13 +56,27 @@ class TextInputStream extends InputStream { private void fillBuffer() throws CharacterCodingException { this.bbuf.compact(); - CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); - if (result.isError()) { - result.throwException(); + if (!this.encoded) { + CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); + if (result.isError()) { + result.throwException(); + } + this.encoded = result.isUnderflow(); + } + if (this.encoded && !this.flushed) { + CoderResult result = this.encoder.flush(this.bbuf); + if (result.isError()) { + result.throwException(); + } + this.flushed = result.isUnderflow(); } this.bbuf.flip(); } + private boolean atEnd() { + return !this.bbuf.hasRemaining() && this.flushed; + } + @Override public int read(byte[] b, int off, int len) throws IOException { if (b == null) { @@ -67,7 +85,7 @@ class TextInputStream extends InputStream { if (off < 0 || len < 0 || off + len > b.length) { throw new IndexOutOfBoundsException(); } - if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + if (atEnd()) { return -1; } int bytesRead = 0; @@ -80,7 +98,7 @@ class TextInputStream extends InputStream { bytesRead += chunk; } else { fillBuffer(); - if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + if (atEnd()) { break; } } @@ -88,7 +106,7 @@ class TextInputStream extends InputStream { if (bytesRead > 0) { return bytesRead; } else { - if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + if (atEnd()) { return -1; } else { return bytesRead; @@ -103,7 +121,7 @@ class TextInputStream extends InputStream { return this.bbuf.get() & 0xFF; } else { fillBuffer(); - if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + if (atEnd()) { return -1; } } @@ -141,6 +159,9 @@ class TextInputStream extends InputStream { if (this.mark != -1) { this.cbuf.position(this.mark); this.mark = -1; + this.encoder.reset(); + this.encoded = false; + this.flushed = false; } } diff --git a/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java b/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java index 536574c3..9391182e 100644 --- a/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java +++ b/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java @@ -22,8 +22,10 @@ package org.apache.james.mime4j.io; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.security.SecureRandom; import org.apache.james.mime4j.Charsets; @@ -154,4 +156,42 @@ public class TextInputStreamTest { r.close(); } + @Test + public void testStatefulCharsetEndsInInitialState() throws Exception { + Charset iso2022jp = Charset.forName("ISO-2022-JP"); + String text = "\u3053\u3093"; + byte[] expected = text.getBytes(iso2022jp); // ESC $ B $3$s ESC ( B + + InputStream in = new TextInputStream(text, iso2022jp, 1024); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[16]; + int n; + while ((n = in.read(buf)) != -1) { + out.write(buf, 0, n); + } + Assert.assertArrayEquals(expected, out.toByteArray()); + + // byte by byte, with a buffer too small for the final escape sequence + in = new TextInputStream(text, iso2022jp, 4); + out.reset(); + int b; + while ((b = in.read()) != -1) { + out.write(b); + } + Assert.assertArrayEquals(expected, out.toByteArray()); + + // re-read after a reset + in = new TextInputStream(text, iso2022jp, 1024); + in.mark(0); + while (in.read() != -1) { + // drain + } + in.reset(); + out.reset(); + while ((n = in.read(buf)) != -1) { + out.write(buf, 0, n); + } + Assert.assertArrayEquals(expected, out.toByteArray()); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
