JAMES-2199 Extract duplicate code to a helper method Also, use StandardCharsets.UTF-8 and catch Exception rather than Throwable.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c3c3ee00 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c3c3ee00 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c3c3ee00 Branch: refs/heads/master Commit: c3c3ee00d0df5aabd585cc785faee39dc3df013b Parents: e5506d2 Author: Daniel Trebbien <[email protected]> Authored: Fri Oct 20 09:09:12 2017 -0500 Committer: Antoine Duprat <[email protected]> Committed: Mon Dec 18 20:31:50 2017 +0100 ---------------------------------------------------------------------- .../transport/mailets/ICalendarParser.java | 29 ++++++++------------ 1 file changed, 12 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c3c3ee00/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/ICalendarParser.java ---------------------------------------------------------------------- diff --git a/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/ICalendarParser.java b/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/ICalendarParser.java index a571248..68da345 100644 --- a/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/ICalendarParser.java +++ b/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/ICalendarParser.java @@ -21,7 +21,7 @@ package org.apache.james.transport.mailets; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.stream.Stream; @@ -137,28 +137,23 @@ public class ICalendarParser extends GenericMailet { return Stream.of(Pair.of(key, builder.build(inputStream))); } catch (IOException e) { if (LOGGER.isErrorEnabled()) { - String icsContentAsString; - try { - icsContentAsString = new String(icsContent, Charset.forName("UTF-8")); - } catch (Throwable t) { - LOGGER.error("Error while decoding ics content", t); - icsContentAsString = new String(Hex.encodeHex(icsContent)); - } - LOGGER.error("Error while reading input: " + icsContentAsString, e); + LOGGER.error("Error while reading input: " + icsContentToString(icsContent), e); } return Stream.of(); } catch (ParserException e) { if (LOGGER.isErrorEnabled()) { - String icsContentAsString; - try { - icsContentAsString = new String(icsContent, Charset.forName("UTF-8")); - } catch (Throwable t) { - LOGGER.error("Error while decoding ics content", t); - icsContentAsString = new String(Hex.encodeHex(icsContent)); - } - LOGGER.error("Error while parsing ICal object: " + icsContentAsString, e); + LOGGER.error("Error while parsing ICal object: " + icsContentToString(icsContent), e); } return Stream.of(); } } + + private static String icsContentToString(byte[] icsContent) { + try { + return new String(icsContent, StandardCharsets.UTF_8); + } catch (Exception e) { + LOGGER.error("Error while decoding ics content", e); + } + return new String(Hex.encodeHex(icsContent)); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
