X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Fnaming%2FLdifParser.java;h=86392b345174cb9392a1cb5360fe439470e777b4;hb=5fbf45d98df87b3d3d1e24fd7f3073acb0d6f840;hp=4aefc9a83b8c099564d7a74ec219f8828eb98c4f;hpb=b334cd41b64d0658aae9125c58d6a1194eccb087;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java b/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java index 4aefc9a83..86392b345 100644 --- a/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java +++ b/org.argeo.enterprise/src/org/argeo/naming/LdifParser.java @@ -4,6 +4,9 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; import java.util.List; @@ -26,6 +29,7 @@ import org.argeo.osgi.useradmin.UserDirectoryException; /** Basic LDIF parser. */ public class LdifParser { private final static Log log = LogFactory.getLog(LdifParser.class); + private final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; protected Attributes addAttributes(SortedMap res, int lineNumber, LdapName currentDn, Attributes currentAttributes) { @@ -47,11 +51,26 @@ public class LdifParser { } } + /** With UTF-8 charset */ public SortedMap read(InputStream in) throws IOException { + try (Reader reader = new InputStreamReader(in, DEFAULT_CHARSET)) { + return read(reader); + } finally { + try { + in.close(); + } catch (IOException e) { + if (log.isTraceEnabled()) + log.error("Cannot close stream", e); + } + } + } + + /** Will close the reader. */ + public SortedMap read(Reader reader) throws IOException { SortedMap res = new TreeMap(); try { List lines = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) { + try (BufferedReader br = new BufferedReader(reader)) { String line; while ((line = br.readLine()) != null) { lines.add(line); @@ -95,6 +114,7 @@ public class LdifParser { } String attributeId = attrId.toString(); + // TODO should we really trim the end of the string as well? String cleanValueStr = currentEntry.toString().trim(); Object attributeValue = isBase64 ? Base64.getDecoder().decode(cleanValueStr) : cleanValueStr; @@ -136,7 +156,12 @@ public class LdifParser { currentEntry.append(line); } } finally { - in.close(); + try { + reader.close(); + } catch (IOException e) { + if (log.isTraceEnabled()) + log.error("Cannot close stream", e); + } } return res; }