You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2018/04/09 14:09:34 UTC

[directory-ldap-api] branch master updated: Using StandardCharset.UTF_8 instead of "UTF-8"

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 1aea113  Using StandardCharset.UTF_8 instead of "UTF-8"
1aea113 is described below

commit 1aea113c1b34a39c4dad2a71d4bb38074766bb66
Author: Emmanuel Lécharny <el...@symas.com>
AuthorDate: Mon Apr 9 16:09:32 2018 +0200

    Using StandardCharset.UTF_8 instead of "UTF-8"
---
 .../directory/api/dsmlv2/engine/Dsmlv2Engine.java  |  3 +-
 .../ldap/client/template/MemoryClearingBuffer.java |  7 ++--
 .../directory/api/ldap/sp/JavaStoredProcUtils.java |  3 +-
 .../api/ldap/model/name/DnParserTest.java          | 12 +++---
 .../api/ldap/model/name/FastDnParserTest.java      | 12 +++---
 ...TeletexTerminalIdentifierSyntaxCheckerTest.java |  6 ++-
 .../syntaxes/TelexNumberSyntaxCheckerTest.java     |  4 +-
 .../directory/api/ldap/model/url/LdapUrlTest.java  |  3 +-
 .../ldap/schema/extractor/impl/ResourceMap.java    |  3 +-
 .../org/apache/directory/api/util/Strings.java     | 44 ++++++++++++++++++++++
 10 files changed, 76 insertions(+), 21 deletions(-)

diff --git a/dsml/engine/src/main/java/org/apache/directory/api/dsmlv2/engine/Dsmlv2Engine.java b/dsml/engine/src/main/java/org/apache/directory/api/dsmlv2/engine/Dsmlv2Engine.java
index a5d15d7..632530d 100644
--- a/dsml/engine/src/main/java/org/apache/directory/api/dsmlv2/engine/Dsmlv2Engine.java
+++ b/dsml/engine/src/main/java/org/apache/directory/api/dsmlv2/engine/Dsmlv2Engine.java
@@ -30,6 +30,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.codec.Charsets;
 import org.apache.directory.api.asn1.DecoderException;
 import org.apache.directory.api.asn1.EncoderException;
 import org.apache.directory.api.dsmlv2.DsmlDecorator;
@@ -259,7 +260,7 @@ public class Dsmlv2Engine
         {
             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
             processDSML( byteOut );
-            return new String( byteOut.toByteArray(), "UTF-8" );
+            return new String( byteOut.toByteArray(), Charsets.UTF_8 );
         }
         catch ( IOException e )
         {
diff --git a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/template/MemoryClearingBuffer.java b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/template/MemoryClearingBuffer.java
index 0dca769..2cb9ef6 100755
--- a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/template/MemoryClearingBuffer.java
+++ b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/template/MemoryClearingBuffer.java
@@ -22,7 +22,7 @@ package org.apache.directory.ldap.client.template;
 
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 
@@ -36,7 +36,6 @@ import java.util.Arrays;
  */
 public final class MemoryClearingBuffer
 {
-    private static final Charset UTF8 = Charset.forName( "UTF-8" );
     private byte[] computedBytes;
     private char[] computedChars;
     private byte[] originalBytes;
@@ -182,7 +181,7 @@ public final class MemoryClearingBuffer
     {
         if ( computedBytes == null )
         {
-            ByteBuffer byteBuffer = UTF8.encode(
+            ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(
                 CharBuffer.wrap( precomputedChars, 0, precomputedChars.length ) );
             computedBytes = new byte[byteBuffer.remaining()];
             byteBuffer.get( computedBytes );
@@ -207,7 +206,7 @@ public final class MemoryClearingBuffer
     {
         if ( computedChars == null )
         {
-            CharBuffer charBuffer = UTF8.decode(
+            CharBuffer charBuffer = StandardCharsets.UTF_8.decode(
                 ByteBuffer.wrap( originalBytes, 0, originalBytes.length ) );
             computedChars = new char[charBuffer.remaining()];
             charBuffer.get( computedChars );
diff --git a/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java b/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java
index a797312..02c6398 100644
--- a/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java
+++ b/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java
@@ -27,6 +27,7 @@ import java.io.InputStream;
 import java.io.Serializable;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 
 import javax.naming.NamingException;
 import javax.naming.directory.Attributes;
@@ -179,7 +180,7 @@ public final class JavaStoredProcUtils
             {
                 byte[] type;
                 byte[] value;
-                type = arguments[i].getClass().getName().getBytes( "UTF-8" );
+                type = arguments[i].getClass().getName().getBytes( StandardCharsets.UTF_8 );
                 value = SerializationUtils.serialize( ( Serializable ) arguments[i] );
                 req.addParameter( type, value );
             }
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/DnParserTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/DnParserTest.java
index c88be37..9314fcb 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/DnParserTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/DnParserTest.java
@@ -25,6 +25,8 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.nio.charset.StandardCharsets;
+
 import org.apache.directory.api.ldap.model.exception.LdapException;
 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
 import org.apache.directory.api.ldap.model.name.Dn;
@@ -523,7 +525,7 @@ public class DnParserTest
     {
         String cn = new String( new byte[]
             { 'c', 'n', '=', 0x4A, ( byte ) 0xC3, ( byte ) 0xA9, 0x72, ( byte ) 0xC3, ( byte ) 0xB4, 0x6D, 0x65 },
-            "UTF-8" );
+            StandardCharsets.UTF_8 );
 
         String result = new Dn( cn ).toString();
 
@@ -537,7 +539,7 @@ public class DnParserTest
         String cn = new String( new byte[]
             { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, ( byte ) 0xC3, ( byte ) 0x96, ( byte ) 0xC3, ( byte ) 0x9C,
                 ( byte ) 0xC3, ( byte ) 0x9F, ( byte ) 0xC3, ( byte ) 0xA4, ( byte ) 0xC3, ( byte ) 0xB6,
-                ( byte ) 0xC3, ( byte ) 0xBC }, "UTF-8" );
+                ( byte ) 0xC3, ( byte ) 0xBC }, StandardCharsets.UTF_8 );
 
         String result = new Dn( cn ).toString();
 
@@ -552,7 +554,7 @@ public class DnParserTest
             { 'c', 'n', '=', ( byte ) 0xC4, ( byte ) 0xB0, ( byte ) 0xC4, ( byte ) 0xB1, ( byte ) 0xC5, ( byte ) 0x9E,
                 ( byte ) 0xC5, ( byte ) 0x9F, ( byte ) 0xC3, ( byte ) 0x96, ( byte ) 0xC3, ( byte ) 0xB6,
                 ( byte ) 0xC3, ( byte ) 0x9C, ( byte ) 0xC3, ( byte ) 0xBC, ( byte ) 0xC4, ( byte ) 0x9E,
-                ( byte ) 0xC4, ( byte ) 0x9F }, "UTF-8" );
+                ( byte ) 0xC4, ( byte ) 0x9F }, StandardCharsets.UTF_8 );
 
         String result = new Dn( cn ).toString();
 
@@ -564,7 +566,7 @@ public class DnParserTest
     public void testAUmlautPlusBytes() throws Exception
     {
         String cn = new String( new byte[]
-            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, '\\', '2', 'B' }, "UTF-8" );
+            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, '\\', '2', 'B' }, StandardCharsets.UTF_8 );
 
         Dn dn = new Dn( cn );
 
@@ -577,7 +579,7 @@ public class DnParserTest
     public void testAUmlautPlusChar() throws Exception
     {
         String cn = new String( new byte[]
-            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0xA4, '\\', '+' }, "UTF-8" );
+            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0xA4, '\\', '+' }, StandardCharsets.UTF_8 );
 
         Dn dn = new Dn( cn );
 
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/FastDnParserTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/FastDnParserTest.java
index 2949c87..d5e3ab2 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/FastDnParserTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/name/FastDnParserTest.java
@@ -24,6 +24,8 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
+import java.nio.charset.StandardCharsets;
+
 import org.apache.directory.api.ldap.model.exception.LdapException;
 import org.apache.directory.api.ldap.model.name.Dn;
 import org.apache.directory.api.ldap.model.name.FastDnParser;
@@ -535,7 +537,7 @@ public class FastDnParserTest
     {
         String cn = new String( new byte[]
             { 'c', 'n', '=', 0x4A, ( byte ) 0xC3, ( byte ) 0xA9, 0x72, ( byte ) 0xC3, ( byte ) 0xB4, 0x6D, 0x65 },
-            "UTF-8" );
+            StandardCharsets.UTF_8 );
 
         String result = FastDnParser.parse( cn ).toString();
 
@@ -549,7 +551,7 @@ public class FastDnParserTest
         String cn = new String( new byte[]
             { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, ( byte ) 0xC3, ( byte ) 0x96, ( byte ) 0xC3, ( byte ) 0x9C,
                 ( byte ) 0xC3, ( byte ) 0x9F, ( byte ) 0xC3, ( byte ) 0xA4, ( byte ) 0xC3, ( byte ) 0xB6,
-                ( byte ) 0xC3, ( byte ) 0xBC }, "UTF-8" );
+                ( byte ) 0xC3, ( byte ) 0xBC }, StandardCharsets.UTF_8 );
 
         String result = FastDnParser.parse( cn ).toString();
 
@@ -568,7 +570,7 @@ public class FastDnParserTest
             { 'c', 'n', '=', ( byte ) 0xC4, ( byte ) 0xB0, ( byte ) 0xC4, ( byte ) 0xB1, ( byte ) 0xC5, ( byte ) 0x9E,
                 ( byte ) 0xC5, ( byte ) 0x9F, ( byte ) 0xC3, ( byte ) 0x96, ( byte ) 0xC3, ( byte ) 0xB6,
                 ( byte ) 0xC3, ( byte ) 0x9C, ( byte ) 0xC3, ( byte ) 0xBC, ( byte ) 0xC4, ( byte ) 0x9E,
-                ( byte ) 0xC4, ( byte ) 0x9F }, "UTF-8" );
+                ( byte ) 0xC4, ( byte ) 0x9F }, StandardCharsets.UTF_8 );
 
         String result = FastDnParser.parse( cn ).toString();
 
@@ -585,7 +587,7 @@ public class FastDnParserTest
     public void testAUmlautPlusBytes() throws Exception
     {
         String cn = new String( new byte[]
-            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, 0x5C, 0x32, 0x42 }, "UTF-8" );
+            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, 0x5C, 0x32, 0x42 }, StandardCharsets.UTF_8 );
 
         FastDnParser.parse( cn ).toString();
         fail( "DNs with special characters not supported by fast parser" );
@@ -600,7 +602,7 @@ public class FastDnParserTest
     public void testAUmlautPlusChar() throws Exception
     {
         String cn = new String( new byte[]
-            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, '\\', '+' }, "UTF-8" );
+            { 'c', 'n', '=', ( byte ) 0xC3, ( byte ) 0x84, '\\', '+' }, StandardCharsets.UTF_8 );
 
         FastDnParser.parse( cn ).toString();
         fail( "DNs with special characters not supported by fast parser" );
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TeletexTerminalIdentifierSyntaxCheckerTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TeletexTerminalIdentifierSyntaxCheckerTest.java
index c4a8f19..c95f0f1 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TeletexTerminalIdentifierSyntaxCheckerTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TeletexTerminalIdentifierSyntaxCheckerTest.java
@@ -23,6 +23,8 @@ package org.apache.directory.api.ldap.model.schema.syntaxes;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.nio.charset.StandardCharsets;
+
 import com.mycila.junit.concurrent.Concurrency;
 import com.mycila.junit.concurrent.ConcurrentJunitRunner;
 
@@ -62,7 +64,7 @@ public class TeletexTerminalIdentifierSyntaxCheckerTest
     {
         assertFalse( checker.isValidSyntax( "test$" ) );
         assertFalse( checker.isValidSyntax( new String( new byte[]
-            { 't', 'e', 's', 't', 0x00, 0x7F, ( byte ) 0x80, '$', 't', 'e', 's', 't' }, "UTF-8" ) ) );
+            { 't', 'e', 's', 't', 0x00, 0x7F, ( byte ) 0x80, '$', 't', 'e', 's', 't' }, StandardCharsets.UTF_8 ) ) );
         assertFalse( checker.isValidSyntax( "test$$" ) );
         assertFalse( checker.isValidSyntax( "test$a:b" ) );
         assertFalse( checker.isValidSyntax( "test$misc" ) );
@@ -87,7 +89,7 @@ public class TeletexTerminalIdentifierSyntaxCheckerTest
         assertTrue( checker.isValidSyntax( "test$private:abc" ) );
         assertTrue( checker.isValidSyntax( "test$private:abc$misc:def" ) );
         assertTrue( checker.isValidSyntax( "test$misc:" + new String( new byte[]
-            { 't', 'e', 's', 't', 0x00, 0x7F, ( byte ) 0xFF }, "UTF-8" ) ) );
+            { 't', 'e', 's', 't', 0x00, 0x7F, ( byte ) 0xFF }, StandardCharsets.UTF_8 ) ) );
         assertTrue( checker.isValidSyntax( "test$misc:a\\5c" ) );
         assertTrue( checker.isValidSyntax( "test$misc:a\\5C" ) );
         assertTrue( checker.isValidSyntax( "test$misc:a\\24" ) );
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelexNumberSyntaxCheckerTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelexNumberSyntaxCheckerTest.java
index cf84498..ae91bc0 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelexNumberSyntaxCheckerTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelexNumberSyntaxCheckerTest.java
@@ -23,6 +23,8 @@ package org.apache.directory.api.ldap.model.schema.syntaxes;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.nio.charset.StandardCharsets;
+
 import com.mycila.junit.concurrent.Concurrency;
 import com.mycila.junit.concurrent.ConcurrentJunitRunner;
 
@@ -67,7 +69,7 @@ public class TelexNumberSyntaxCheckerTest
         assertFalse( checker.isValidSyntax( "$test$test" ) );
         assertFalse( checker.isValidSyntax( "test$test$test$test" ) );
         assertFalse( checker.isValidSyntax( new String( new byte[]
-            { 't', 'e', 's', 't', '$', 0x00, 0x7F, ( byte ) 0x80, '$', 't', 'e', 's', 't' }, "UTF-8" ) ) );
+            { 't', 'e', 's', 't', '$', 0x00, 0x7F, ( byte ) 0x80, '$', 't', 'e', 's', 't' }, StandardCharsets.UTF_8 ) ) );
     }
 
 
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/url/LdapUrlTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/url/LdapUrlTest.java
index a57acc4..2a1109b 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/url/LdapUrlTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/url/LdapUrlTest.java
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -1562,7 +1563,7 @@ public class LdapUrlTest
             new byte[]
                 { ( byte ) 0xC3, ( byte ) 0x84, ( byte ) 0xC3, ( byte ) 0x96, ( byte ) 0xC3, ( byte ) 0x9C,
                     ( byte ) 0xC3, ( byte ) 0x9F, ( byte ) 0xC3, ( byte ) 0xA4, ( byte ) 0xC3, ( byte ) 0xB6,
-                    ( byte ) 0xC3, ( byte ) 0xBC }, "UTF-8" );
+                    ( byte ) 0xC3, ( byte ) 0xBC }, StandardCharsets.UTF_8 );
 
         LdapUrl url1 = new LdapUrl();
         url1.setHost( "localhost" );
diff --git a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java
index 427de69..51c5f0a 100644
--- a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java
+++ b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -221,7 +222,7 @@ public final class ResourceMap
                 {
                     index = indexes.nextElement();
                     InputStream in = index.openStream();
-                    BufferedReader reader = new BufferedReader( new InputStreamReader( in, "UTF-8" ) );
+                    BufferedReader reader = new BufferedReader( new InputStreamReader( in, StandardCharsets.UTF_8 ) );
                     String line = reader.readLine();
 
                     while ( line != null )
diff --git a/util/src/main/java/org/apache/directory/api/util/Strings.java b/util/src/main/java/org/apache/directory/api/util/Strings.java
index db9ec07..0887744 100644
--- a/util/src/main/java/org/apache/directory/api/util/Strings.java
+++ b/util/src/main/java/org/apache/directory/api/util/Strings.java
@@ -2335,6 +2335,34 @@ public final class Strings
      * default system encoding is used.
      *
      * @param data the byte array to be encoded
+     * @param offset the index of the first byte to encode
+     * @param length the number of bytes to encode
+     * @param charset the desired character encoding
+     * @return The result of the conversion.
+     * @since 3.0
+     */
+    public static String getString( final byte[] data, int offset, int length, Charset charset )
+    {
+        if ( data == null )
+        {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_17028_PARAMETER_CANT_BE_NULL ) );
+        }
+
+        if ( charset == null )
+        {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_17029_CHARSET_CANT_BE_NULL ) );
+        }
+
+        return new String( data, offset, length, charset );
+    }
+
+
+    /**
+     * From commons-httpclients. Converts the byte array of HTTP content
+     * characters to a string. If the specified charset is not supported,
+     * default system encoding is used.
+     *
+     * @param data the byte array to be encoded
      * @param charset the desired character encoding
      * @return The result of the conversion.
      * @since 3.0
@@ -2346,6 +2374,22 @@ public final class Strings
 
 
     /**
+     * From commons-httpclients. Converts the byte array of HTTP content
+     * characters to a string. If the specified charset is not supported,
+     * default system encoding is used.
+     *
+     * @param data the byte array to be encoded
+     * @param charset the desired character encoding
+     * @return The result of the conversion.
+     * @since 3.0
+     */
+    public static String getString( final byte[] data, Charset charset )
+    {
+        return getString( data, 0, data.length, charset );
+    }
+
+
+    /**
      * Create a new UUID using a long as the least significant bits
      * 
      * @param value The least significant bits.

-- 
To stop receiving notification emails like this one, please contact
elecharny@apache.org.