Make UTF-8 the default charset for LDIF
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 16 Mar 2017 09:55:51 +0000 (10:55 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 16 Mar 2017 09:55:51 +0000 (10:55 +0100)
org.argeo.enterprise/src/org/argeo/naming/LdifParser.java
org.argeo.enterprise/src/org/argeo/naming/LdifWriter.java

index 4aefc9a83b8c099564d7a74ec219f8828eb98c4f..9595b57f0b0d030e866b44856131ea3a6cfe5105 100644 (file)
@@ -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<LdapName, Attributes> res, int lineNumber, LdapName currentDn,
                        Attributes currentAttributes) {
@@ -47,11 +51,26 @@ public class LdifParser {
                }
        }
 
+       /** With UTF-8 charset */
        public SortedMap<LdapName, Attributes> 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<LdapName, Attributes> read(Reader reader) throws IOException {
                SortedMap<LdapName, Attributes> res = new TreeMap<LdapName, Attributes>();
                try {
                        List<String> 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);
@@ -136,7 +155,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;
        }
index 3a297e5db0c1a34e688dc0e69fafc1a3042f744f..892fa885d8f46611de39d0d173bda5edc944482d 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.Base64;
 import java.util.Map;
 
@@ -18,6 +20,7 @@ import org.argeo.osgi.useradmin.UserDirectoryException;
 
 /** Basic LDIF writer */
 public class LdifWriter {
+       private final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
        private final Writer writer;
 
        /** Writer must be closed by caller */
@@ -27,7 +30,7 @@ public class LdifWriter {
 
        /** Stream must be closed by caller */
        public LdifWriter(OutputStream out) {
-               this(new OutputStreamWriter(out));
+               this(new OutputStreamWriter(out, DEFAULT_CHARSET));
        }
 
        public void writeEntry(LdapName name, Attributes attributes) throws IOException {