From 5c660ca3cd90722ebd6f7a0aea80e4e2804bbe02 Mon Sep 17 00:00:00 2001 From: fwonce Date: Wed, 3 Jan 2018 12:03:22 +0800 Subject: [PATCH] remove hardcoded header encoding, add MIMEConfig#setHeaderConfig() --- src/main/java/org/jvnet/mimepull/MIMEConfig.java | 12 +++++++++--- src/main/java/org/jvnet/mimepull/MIMEParser.java | 8 +++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jvnet/mimepull/MIMEConfig.java b/src/main/java/org/jvnet/mimepull/MIMEConfig.java index e4048dd..0df4a4c 100644 --- a/src/main/java/org/jvnet/mimepull/MIMEConfig.java +++ b/src/main/java/org/jvnet/mimepull/MIMEConfig.java @@ -70,20 +70,22 @@ public class MIMEConfig { File tempDir; String prefix; String suffix; + String headerEncoding; - private MIMEConfig(boolean parseEagerly, int chunkSize, - long inMemoryThreshold, String dir, String prefix, String suffix) { + private MIMEConfig(boolean parseEagerly, int chunkSize, long inMemoryThreshold, + String dir, String prefix, String suffix, String headerEncoding) { this.parseEagerly = parseEagerly; this.chunkSize = chunkSize; this.memoryThreshold = inMemoryThreshold; this.prefix = prefix; this.suffix = suffix; + this.headerEncoding = headerEncoding; setDir(dir); } public MIMEConfig() { this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null, - DEFAULT_FILE_PREFIX, null); + DEFAULT_FILE_PREFIX, null, null); } boolean isParseEagerly() { @@ -133,6 +135,10 @@ String getTempFileSuffix() { return suffix; } + public void setHeaderEncoding(String headerEncoding) { + this.headerEncoding = headerEncoding; + } + /** * @param directory * temp directory diff --git a/src/main/java/org/jvnet/mimepull/MIMEParser.java b/src/main/java/org/jvnet/mimepull/MIMEParser.java index 49ac436..5da02f3 100644 --- a/src/main/java/org/jvnet/mimepull/MIMEParser.java +++ b/src/main/java/org/jvnet/mimepull/MIMEParser.java @@ -70,7 +70,7 @@ class MIMEParser implements Iterable { private static final Logger LOGGER = Logger.getLogger(MIMEParser.class.getName()); private static final String HEADER_ENCODING = "ISO8859-1"; - + // Actually, the grammar doesn't support whitespace characters // after boundary. But the mail implementation checks for it. // We will only check for these many whitespace characters after boundary @@ -101,6 +101,7 @@ private enum STATE {START_MESSAGE, SKIP_PREAMBLE, START_PART, HEADERS, BODY, END private byte[] buf; private int len; private boolean bol; // beginning of the line + private String headerEncoding; /* * Parses the MIME content. At the EOF, it also closes input stream @@ -111,6 +112,7 @@ private enum STATE {START_MESSAGE, SKIP_PREAMBLE, START_PART, HEADERS, BODY, END bl = bndbytes.length; this.config = config; gss = new int[bl]; + this.headerEncoding = config.headerEncoding != null ? config.headerEncoding : HEADER_ENCODING; compileBoundaryPattern(); // \r\n + boundary + "--\r\n" + lots of LWSP @@ -357,7 +359,7 @@ private static byte[] getBytes(String s) { return bytes; } - /** + /** * Boyer-Moore search method. Copied from java.util.regex.Pattern.java * * Pre calculates arrays needed to generate the bad character @@ -512,7 +514,7 @@ public String readLine() throws IOException { return null; } - String hdr = new String(buf, offset, hdrLen, HEADER_ENCODING); + String hdr = new String(buf, offset, hdrLen, headerEncoding); offset += hdrLen+lwsp; return hdr; }