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 2005/12/26 23:49:13 UTC
svn commit: r359117 - in
/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common:
schema/DeepTrimNormalizer.java schema/DeepTrimToLowerNormalizer.java
util/StringTools.java
Author: elecharny
Date: Mon Dec 26 14:49:06 2005
New Revision: 359117
URL: http://svn.apache.org/viewcvs?rev=359117&view=rev
Log:
- Added asn1-codec StringUtils methods to StringTools
- Used StringTools instead of StringUtils
Modified:
directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimNormalizer.java
directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimToLowerNormalizer.java
directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/StringTools.java
Modified: directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimNormalizer.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimNormalizer.java?rev=359117&r1=359116&r2=359117&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimNormalizer.java (original)
+++ directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimNormalizer.java Mon Dec 26 14:49:06 2005
@@ -19,7 +19,6 @@
import javax.naming.NamingException;
-import org.apache.asn1.codec.util.StringUtils;
import org.apache.ldap.common.util.StringTools;
@@ -42,7 +41,7 @@
if ( value instanceof byte[] )
{
- return StringTools.deepTrim( StringUtils.utf8ToString( (byte[])value ) );
+ return StringTools.deepTrim( StringTools.utf8ToString( (byte[])value ) );
}
else
{
Modified: directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimToLowerNormalizer.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimToLowerNormalizer.java?rev=359117&r1=359116&r2=359117&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimToLowerNormalizer.java (original)
+++ directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DeepTrimToLowerNormalizer.java Mon Dec 26 14:49:06 2005
@@ -19,7 +19,6 @@
import javax.naming.NamingException;
-import org.apache.asn1.codec.util.StringUtils;
import org.apache.ldap.common.util.StringTools;
@@ -43,7 +42,7 @@
if ( value instanceof byte[] )
{
- return StringTools.deepTrimToLower( StringUtils.utf8ToString( (byte[])value ) );
+ return StringTools.deepTrimToLower( StringTools.utf8ToString( (byte[])value ) );
}
else
{
Modified: directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/StringTools.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/StringTools.java?rev=359117&r1=359116&r2=359117&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/StringTools.java (original)
+++ directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/StringTools.java Mon Dec 26 14:49:06 2005
@@ -18,7 +18,10 @@
import java.io.File ;
+import java.io.UnsupportedEncodingException;
+import java.util.Iterator;
import java.util.List ;
+import java.util.Map;
import java.io.FileFilter ;
import java.util.ArrayList ;
@@ -518,17 +521,1528 @@
return l_list ;
}
+ //~ Static fields/initializers -----------------------------------------------------------------
+
+ /** Hex chars */
+ private static final byte[] HEX_CHAR =
+ new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+ private static int UTF8_MULTI_BYTES_MASK = 0x0080;
+
+ private static int UTF8_TWO_BYTES_MASK = 0x00E0;
+ private static int UTF8_TWO_BYTES = 0x00C0;
+
+ private static int UTF8_THREE_BYTES_MASK = 0x00F0;
+ private static int UTF8_THREE_BYTES = 0x00E0;
+
+ private static int UTF8_FOUR_BYTES_MASK = 0x00F8;
+ private static int UTF8_FOUR_BYTES = 0x00F0;
+
+ private static int UTF8_FIVE_BYTES_MASK = 0x00FC;
+ private static int UTF8_FIVE_BYTES = 0x00F8;
+
+ private static int UTF8_SIX_BYTES_MASK = 0x00FE;
+ private static int UTF8_SIX_BYTES = 0x00FC;
+
+ /** <alpha> ::= [0x41-0x5A] | [0x61-0x7A] */
+ public static final boolean[] ALPHA =
+ {
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, false, false, false,
+ false, false, false, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ false, false, false, false, false
+ };
+
+ /** <alpha> | <digit> | '-' */
+ public static final boolean[] CHAR =
+ {
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, true, false, false, true, true, true, true, true,
+ true, true, true, true, true, false, false, false, false, false, false, false, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, false, false, false, false, false,
+ false, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, true, false, false,
+ false, false, false
+ };
+
+ /** '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
+ public static final boolean[] DIGIT =
+ {
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, true, true, true, true,
+ true, true, true, true, true, true, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false,
+ };
+
+ /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+ private static final boolean[] HEX =
+ {
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, true, true, true, true,
+ true, true, true, true, true, true, false, false, false, false, false, false, false, true,
+ true, true, true, true, true, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, true, true, true, true, true, true, false, false, false,
+ false, false, false, false, false, false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false
+ };
+
+ /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+ public static final byte[] HEX_VALUE =
+ {
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00 -> 0F
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10 -> 1F
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20 -> 2F
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30 -> 3F ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 40 -> 4F ( A, B, C, D, E, F )
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 50 -> 5F
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 60 -> 6F ( a, b, c, d, e, f )
+ };
+
+ private static int CHAR_ONE_BYTE_MASK = 0xFFFFFF80;
+
+ private static int CHAR_TWO_BYTES_MASK = 0xFFFFF800;
+
+ private static int CHAR_THREE_BYTES_MASK = 0xFFFF0000;
+
+ private static int CHAR_FOUR_BYTES_MASK = 0xFFE00000;
+
+ private static int CHAR_FIVE_BYTES_MASK = 0xFC000000;
+
+ private static int CHAR_SIX_BYTES_MASK = 0x80000000;
+
+ public static final int NOT_EQUAL = -1;
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Helper function that dump a byte in hex form
+ *
+ * @param octet The byte to dump
+ * @return A string representation of the byte
+ */
+ public static String dumpByte( byte octet )
+ {
+ return new String(
+ new byte[] { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] } );
+ }
+
+ /**
+ * Helper function that returns a char from an hex
+ *
+ * @param octet The hex to dump
+ * @return A char representation of the hex
+ */
+ public static char dumpHex( byte hex )
+ {
+ return (char)HEX_CHAR[hex & 0x000F];
+ }
+
+ /**
+ * Helper function that dump an array of bytes in hex form
+ *
+ * @param buffer The bytes array to dump
+ * @return A string representation of the array of bytes
+ */
+ public static String dumpBytes( byte[] buffer )
+ {
+ if( buffer == null )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+
+ for (int i = 0; i < buffer.length ; i++)
+ {
+ sb.append("0x").append((char)(HEX_CHAR[( buffer[i] & 0x00F0 ) >> 4])).append((char)(HEX_CHAR[buffer[i] & 0x000F])).append(" ");
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Return the Unicode char which is coded in the bytes at position 0.
+ *
+ * @param bytes The byte[] represntation of an Unicode string.
+ * @return The first char found.
+ */
+ public static char bytesToChar(byte[] bytes)
+ {
+ return bytesToChar(bytes, 0);
+ }
+
+ /**
+ * Count the number of bytes needed to return an Unicode char. This
+ * can be from 1 to 6.
+ * @param bytes The bytes to read
+ * @param pos Position to start counting. It must be a valid start of a
+ * encoded char !
+ * @return The number of bytes to create a char, or -1 if the encoding is wrong.
+ *
+ * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+ */
+ public static int countBytesPerChar(byte[] bytes, int pos)
+ {
+ if( bytes == null )
+ {
+ return -1;
+ }
+
+ if ((bytes[pos] & UTF8_MULTI_BYTES_MASK) == 0)
+ {
+ return 1;
+ } else if ((bytes[pos] & UTF8_TWO_BYTES_MASK) == UTF8_TWO_BYTES)
+ {
+ return 2;
+ }
+ else if ((bytes[pos] & UTF8_THREE_BYTES_MASK) == UTF8_THREE_BYTES)
+ {
+ return 3;
+ }
+ else if ((bytes[pos] & UTF8_FOUR_BYTES_MASK) == UTF8_FOUR_BYTES)
+ {
+ return 4;
+ }
+ else if ((bytes[pos] & UTF8_FIVE_BYTES_MASK) == UTF8_FIVE_BYTES)
+ {
+ return 5;
+ }
+ else if ((bytes[pos] & UTF8_SIX_BYTES_MASK) == UTF8_SIX_BYTES)
+ {
+ return 6;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * Return the number of bytes that hold an Unicode char.
+ *
+ * @param car The character to be decoded
+ * @return The number of bytes to hold the char.
+ *
+ * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+ */
+ public static int countNbBytesPerChar( char car )
+ {
+ if ( ( car & CHAR_ONE_BYTE_MASK ) == 0 )
+ {
+ return 1;
+ }
+ else if ( ( car & CHAR_TWO_BYTES_MASK ) == 0 )
+ {
+ return 2;
+ }
+ else if ( ( car & CHAR_THREE_BYTES_MASK ) == 0 )
+ {
+ return 3;
+ }
+ else if ( ( car & CHAR_FOUR_BYTES_MASK ) == 0 )
+ {
+ return 4;
+ }
+ else if ( ( car & CHAR_FIVE_BYTES_MASK ) == 0 )
+ {
+ return 5;
+ }
+ else if ( ( car & CHAR_SIX_BYTES_MASK ) == 0 )
+ {
+ return 6;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * Count the number of bytes included in the given char[].
+ * @param chars The char array to decode
+ * @return The number of bytes in the char array
+ */
+ public static int countBytes(char[] chars)
+ {
+ if( chars == null )
+ {
+ return 0;
+ }
+
+ int nbBytes = 0;
+ int currentPos = 0;
+
+ while ( currentPos < chars.length )
+ {
+ int nbb = countNbBytesPerChar( chars[currentPos] );
+
+ // If the number of bytes necessary to encode a character is
+ // above 3, we will need two UTF-16 chars
+ currentPos += (nbb < 4 ? 1 : 2 );
+ nbBytes += nbb;
+ }
+
+ return nbBytes;
+ }
+
+ /**
+ * Return the Unicode char which is coded in the bytes at the given position.
+ * @param bytes The byte[] represntation of an Unicode string.
+ * @param pos The current position to start decoding the char
+ * @return The decoded char, or -1 if no char can be decoded
+ *
+ * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+ */
+ public static char bytesToChar(byte[] bytes, int pos)
+ {
+ if( bytes == null )
+ {
+ return ( char ) -1;
+ }
+
+ if ((bytes[pos] & UTF8_MULTI_BYTES_MASK) == 0)
+ {
+ return (char)bytes[pos];
+ }
+ else
+ {
+ if ((bytes[pos] & UTF8_TWO_BYTES_MASK) == UTF8_TWO_BYTES)
+ {
+ // Two bytes char
+ return (char)(
+ ( ( bytes[pos] & 0x1C ) << 6 ) + // 110x-xxyy 10zz-zzzz -> 0000-0xxx 0000-0000
+ ( ( bytes[pos] & 0x03 ) << 6 ) + // 110x-xxyy 10zz-zzzz -> 0000-0000 yy00-0000
+ ( bytes[pos + 1] & 0x3F ) // 110x-xxyy 10zz-zzzz -> 0000-0000 00zz-zzzz
+ ); // -> 0000-0xxx yyzz-zzzz (07FF)
+ }
+ else if ((bytes[pos] & UTF8_THREE_BYTES_MASK) == UTF8_THREE_BYTES)
+ {
+ // Three bytes char
+ return (char)(
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> tttt-0000-0000-0000
+ ( ( bytes[pos] & 0x0F) << 12 ) +
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-xxxx-0000-0000
+ ( ( bytes[pos + 1] & 0x3C) << 6 ) +
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-0000-yy00-0000
+ ( ( bytes[pos + 1] & 0x03) << 6 ) +
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-0000-00zz-zzzz
+ ( bytes[pos + 2] & 0x3F )
+ // -> tttt-xxxx yyzz-zzzz (FF FF)
+ );
+ }
+ else if ((bytes[pos] & UTF8_FOUR_BYTES_MASK) == UTF8_FOUR_BYTES)
+ {
+ // Four bytes char
+ return (char)(
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 000t-tt00 0000-0000 0000-0000
+ ( ( bytes[pos] & 0x07) << 18 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-00uu 0000-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x30) << 16 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000 vvvv-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x0F) << 12 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-xxxx 0000-0000
+ ( ( bytes[pos + 2] & 0x3C) << 6 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 yy00-0000
+ ( ( bytes[pos + 2] & 0x03) << 6 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 00zz-zzzz
+ ( bytes[pos + 3] & 0x3F )
+ // -> 000t-ttuu vvvv-xxxx yyzz-zzzz (1FFFFF)
+ );
+ }
+ else if ((bytes[pos] & UTF8_FIVE_BYTES_MASK) == UTF8_FIVE_BYTES)
+ {
+ // Five bytes char
+ return (char)(
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-00tt 0000-0000 0000-0000 0000-0000
+ ( ( bytes[pos] & 0x03) << 24 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 uuuu-uu00 0000-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x3F) << 18 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-00vv 0000-0000 0000-0000
+ ( ( bytes[pos + 2] & 0x30) << 12 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 wwww-0000 0000-0000
+ ( ( bytes[pos + 2] & 0x0F) << 12 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 0000-xxxx 0000-0000
+ ( ( bytes[pos + 3] & 0x3C) << 6 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 0000-0000 yy00-0000
+ ( ( bytes[pos + 3] & 0x03) << 6 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 0000-0000 00zz-zzzz
+ ( bytes[pos + 4] & 0x3F )
+ // -> 0000-00tt uuuu-uuvv wwww-xxxx yyzz-zzzz (03 FF FF FF)
+ );
+ }
+ else if ((bytes[pos] & UTF8_FIVE_BYTES_MASK) == UTF8_FIVE_BYTES)
+ {
+ // Six bytes char
+ return (char)(
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0s00-0000 0000-0000 0000-0000 0000-0000
+ ( ( bytes[pos] & 0x01) << 30 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 00tt-tttt 0000-0000 0000-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x3F) << 24 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 uuuu-uu00 0000-0000 0000-0000
+ ( ( bytes[pos + 2] & 0x3F) << 18 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-00vv 0000-0000 0000-0000
+ ( ( bytes[pos + 3] & 0x30) << 12 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 wwww-0000 0000-0000
+ ( ( bytes[pos + 3] & 0x0F) << 12 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 0000-xxxx 0000-0000
+ ( ( bytes[pos + 4] & 0x3C) << 6 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 0000-0000 yy00-0000
+ ( ( bytes[pos + 4] & 0x03) << 6 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 0000-0000 00zz-zzzz
+ ( bytes[pos + 5] & 0x3F )
+ // -> 0stt-tttt uuuu-uuvv wwww-xxxx yyzz-zzzz (7F FF FF FF)
+ );
+ }
+ else
+ {
+ return (char)-1;
+ }
+ }
+ }
+
+ /**
+ * Return the Unicode char which is coded in the bytes at the given position.
+ * @param bytes The byte[] represntation of an Unicode string.
+ * @param pos The current position to start decoding the char
+ * @return The decoded char, or -1 if no char can be decoded
+ *
+ * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+ */
+ public static byte[] charToBytes(char car)
+ {
+ byte[] bytes = new byte[ countNbBytesPerChar( car ) ];
+
+ if ( car <= 0x7F )
+ {
+ // Single byte char
+ bytes[0] = (byte)car;
+ return bytes;
+ }
+ else if ( car <= 0x7FF )
+ {
+ // two bytes char
+ bytes[0] = (byte)(0x00C0 + ( ( car & 0x07C0 ) >> 6 ) );
+ bytes[1] = (byte)(0x0080 + ( car & 0x3F ) );
+ }
+ else
+ {
+ // Three bytes char
+ bytes[0] = (byte)(0x00E0 + ( ( car & 0xF000 ) >> 12 ) );
+ bytes[1] = (byte)(0x0080 + ( ( car & 0x0FC0 ) >> 6 ) );
+ bytes[2] = (byte)(0x0080 + ( car & 0x3F ) );
+ }
+
+ return bytes;
+ }
+
+ /**
+ * Count the number of chars included in the given byte[].
+ * @param bytes The byte array to decode
+ * @return The number of char in the byte array
+ */
+ public static int countChars(byte[] bytes)
+ {
+ if( bytes == null )
+ {
+ return 0;
+ }
+
+ int nbChars = 0;
+ int currentPos = 0;
+
+ while (currentPos < bytes.length)
+ {
+ currentPos += countBytesPerChar(bytes, currentPos);
+ nbChars ++;
+ }
+
+ return nbChars;
+ }
+
/**
- * Forked version of a.o.commons.lang.StringUtils.isEmpty
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param text The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( byte[] byteArray, int index, String text )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( byteArray.length <= index ) ||
+ ( index < 0 ) || ( text == null ) )
+ {
+ return NOT_EQUAL;
+ }
+ else
+ {
+ try
+ {
+ byte[] data = text.getBytes( "UTF-8" );
+
+ return areEquals( byteArray, index, data );
+ }
+ catch ( UnsupportedEncodingException uee )
+ {
+ return NOT_EQUAL;
+ }
+ }
+ }
+
+ /**
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param charArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param text The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( char[] charArray, int index, String text )
+ {
+ if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) ||
+ ( index < 0 ) || ( text == null ) )
+ {
+ return NOT_EQUAL;
+ }
+ else
+ {
+ char[] data = text.toCharArray();
+
+ return areEquals( charArray, index, data );
+ }
+ }
+
+ /**
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param charArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param charArray2 The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( char[] charArray, int index, char[] charArray2 )
+ {
+
+ if ( ( charArray == null ) ||
+ ( charArray.length == 0 ) ||
+ ( charArray.length <= index ) ||
+ ( index < 0 ) ||
+ ( charArray2 == null ) ||
+ ( charArray2.length == 0 ) ||
+ ( charArray2.length > ( charArray.length + index ) ) )
+ {
+ return NOT_EQUAL;
+ }
+ else
+ {
+ for ( int i = 0; i < charArray2.length; i++ )
+ {
+ if ( charArray[index++] != charArray2[i] )
+ {
+ return NOT_EQUAL;
+ }
+ }
+
+ return index;
+ }
+ }
+
+ /**
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param byteArray2 The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( byte[] byteArray, int index, byte[] byteArray2 )
+ {
+
+ if ( ( byteArray == null ) ||
+ ( byteArray.length == 0 ) ||
+ ( byteArray.length <= index ) ||
+ ( index < 0 ) ||
+ ( byteArray2 == null ) ||
+ ( byteArray2.length == 0 ) ||
+ ( byteArray2.length > ( byteArray.length + index ) ) )
+ {
+ return NOT_EQUAL;
+ }
+ else
+ {
+ for ( int i = 0; i < byteArray2.length; i++ )
+ {
+ if ( byteArray[index++] != byteArray2[i] )
+ {
+ return NOT_EQUAL;
+ }
+ }
+
+ return index;
+ }
+ }
+
+ /**
+ * Test if the current character is equal to a specific character.
+ * This function works only for character between 0 and 127, as it
+ * does compare a byte and a char (which is 16 bits wide)
+ *
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param car The character we want to compare with the current buffer position
+ *
+ * @return <code>true</code> if the current character equals the given character.
+ */
+ public static boolean isCharASCII( byte[] byteArray, int index, char car )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( byteArray[index] == car ) ? true : false );
+ }
+ }
+
+ /**
+ * Test if the current character is equal to a specific character.
+ *
+ *
+ * @param chars The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param car The character we want to compare with the current buffer position
+ *
+ * @return <code>true</code> if the current character equals the given character.
+ */
+ public static boolean isCharASCII( char[] chars, int index, char car )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( chars[index] == car ) ? true : false );
+ }
+ }
+
+ /**
+ * Check if the current character is an Hex Char
+ * <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
*
- * Checks if a string is null or empty
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is a Hex Char
+ */
+ public static boolean isHex( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ byte c = byteArray[index];
+
+ if ( ( c > 127 ) || ( HEX[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Check if the current character is an Hex Char
+ * <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
*
- * @param string The string to be tested
- * @return <code>true</code> if the string is null or empty.
+ * @param chars The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is a Hex Char
+ */
+ public static boolean isHex( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ char c = chars[index];
+
+ if ( ( c > 127 ) || ( HEX[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+ *
+ * @param byteArray The buffer which contains the data
+ *
+ * @return <code>true</code> if the current character is a Digit
+ */
+ public static boolean isDigit( byte[] byteArray )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( byteArray[0] > 127 ) || ! DIGIT[byteArray[0]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Test if the current character is an Alpha character :
+ * <alpha> ::= [0x41-0x5A] | [0x61-0x7A]
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is an Alpha character
+ */
+ public static boolean isAlphaASCII( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ byte c = byteArray[index++];
+
+ if ( ( c > 127 ) || ( ALPHA[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Test if the current character is an Alpha character :
+ * <alpha> ::= [0x41-0x5A] | [0x61-0x7A]
+ *
+ * @param chars The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is an Alpha character
+ */
+ public static boolean isAlphaASCII( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ char c = chars[index++];
+
+ if ( ( c > 127 ) || ( ALPHA[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is a Digit
+ */
+ public static boolean isDigit( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( byteArray[index] > 127 ) || ! DIGIT[byteArray[index]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+ *
+ * @param chars The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is a Digit
+ */
+ public static boolean isDigit( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( chars[index] > 127 ) || ! DIGIT[chars[index]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+ *
+ * @param chars The buffer which contains the data
+ *
+ * @return <code>true</code> if the current character is a Digit
+ */
+ public static boolean isDigit( char[] chars )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( chars[0] > 127 ) || ! DIGIT[chars[0]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
+ * <char> ::= <alpha> | <digit> | '-'
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return The position of the next character, if the current one is a CHAR.
+ */
+ public static boolean isAlphaDigitMinus( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ byte c = byteArray[index++];
+
+ if ( ( c > 127 ) || ( CHAR[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
+ * <char> ::= <alpha> | <digit> | '-'
+ *
+ * @param chars The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return The position of the next character, if the current one is a CHAR.
+ */
+ public static boolean isAlphaDigitMinus( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ char c = chars[index++];
+
+ if ( ( c > 127 ) || ( CHAR[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ // The following methods are taken from org.apache.commons.lang.StringUtils
+
+ /**
+ * The empty String <code>""</code>.
+ * @since 2.0
+ */
+ public static final String EMPTY = "";
+
+ /**
+ * The empty byte[]
+ */
+ public static final byte[] EMPTY_BYTES = new byte[]{};
+
+ // Empty checks
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Checks if a String is empty ("") or null.</p>
+ *
+ * <pre>
+ * StringUtils.isEmpty(null) = true
+ * StringUtils.isEmpty("") = true
+ * StringUtils.isEmpty(" ") = false
+ * StringUtils.isEmpty("bob") = false
+ * StringUtils.isEmpty(" bob ") = false
+ * </pre>
+ *
+ * <p>NOTE: This method changed in Lang version 2.0.
+ * It no longer trims the String.
+ * That functionality is available in isBlank().</p>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is empty or null
+ */
+ public static boolean isEmpty(String str) {
+ return str == null || str.length() == 0;
+ }
+
+ /**
+ * Checks if a bytes array is empty or null.
+ * @param bytes The bytes array to check, may be null
+ * @return <code>true</code> if the bytes array is empty or null
+ */
+ public static boolean isEmpty(byte[] bytes) {
+ return bytes == null || bytes.length == 0;
+ }
+
+ /**
+ * <p>Checks if a String is not empty ("") and not null.</p>
+ *
+ * <pre>
+ * StringUtils.isNotEmpty(null) = false
+ * StringUtils.isNotEmpty("") = false
+ * StringUtils.isNotEmpty(" ") = true
+ * StringUtils.isNotEmpty("bob") = true
+ * StringUtils.isNotEmpty(" bob ") = true
+ * </pre>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is not empty and not null
+ */
+ public static boolean isNotEmpty(String str) {
+ return str != null && str.length() > 0;
+ }
+
+ /**
+ * <p>Removes spaces (char <= 32) from both start and
+ * ends of this String, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start and end characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trim(null) = null
+ * StringUtils.trim("") = ""
+ * StringUtils.trim(" ") = ""
+ * StringUtils.trim("abc") = "abc"
+ * StringUtils.trim(" abc ") = "abc"
+ * </pre>
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, <code>null</code> if null String input
+ */
+ public static String trim(String str) {
+ if ( isEmpty( str ) )
+ {
+ return "";
+ }
+
+ char[] array = str.toCharArray();
+ int start = 0;
+ int end = array.length;
+
+ while ( ( start < end ) && ( array[start] == ' ' ) )
+ {
+ start++;
+ }
+
+ while ( ( end > start ) && ( array[end - 1] == ' ' ) )
+ {
+ end--;
+ }
+
+ return new String( array, start, ( end - start ) );
+ }
+
+ /**
+ * <p>Removes spaces (char <= 32) from both start and
+ * ends of this bytes array, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start and end characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trim(null) = null
+ * StringUtils.trim("") = ""
+ * StringUtils.trim(" ") = ""
+ * StringUtils.trim("abc") = "abc"
+ * StringUtils.trim(" abc ") = "abc"
+ * </pre>
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, <code>null</code> if null String input
+ */
+ public static byte[] trim(byte[] bytes) {
+ if ( isEmpty( bytes ) )
+ {
+ return EMPTY_BYTES;
+ }
+
+ int start = trimLeft( bytes, 0 );
+ int end = trimRight( bytes, bytes.length - 1 );
+
+ int length = end - start + 1;
+
+ if ( length != 0 )
+ {
+ byte[] newBytes = new byte[ end - start + 1 ];
+
+ System.arraycopy( bytes, start, newBytes, 0, length );
+
+ return newBytes;
+ }
+ else
+ {
+ return EMPTY_BYTES;
+ }
+ }
+
+ /**
+ * <p>Removes spaces (char <= 32) from start
+ * of this String, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trimLeft(null) = null
+ * StringUtils.trimLeft("") = ""
+ * StringUtils.trimLeft(" ") = ""
+ * StringUtils.trimLeft("abc") = "abc"
+ * StringUtils.trimLeft(" abc ") = "abc "
+ * </pre>
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, <code>null</code> if null String input
+ */
+ public static String trimLeft( String str ) {
+ if ( isEmpty( str ) )
+ {
+ return "";
+ }
+
+ char[] array = str.toCharArray();
+ int start = 0;
+
+ while ( ( start < array.length ) && ( array[start] == ' ' ) )
+ {
+ start++;
+ }
+
+ return new String( array, start, array.length - start );
+ }
+ /**
+ * <p>Removes spaces (char <= 32) from start
+ * of this array, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trimLeft(null) = null
+ * StringUtils.trimLeft("") = ""
+ * StringUtils.trimLeft(" ") = ""
+ * StringUtils.trimLeft("abc") = "abc"
+ * StringUtils.trimLeft(" abc ") = "abc "
+ * </pre>
+ *
+ * @param chars the chars array to be trimmed, may be null
+ * @return the position of the first char which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimLeft( char[] chars, int pos ) {
+ if ( chars == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos < chars.length ) && ( chars[pos] == ' ' ) )
+ {
+ pos++;
+ }
+
+ return pos;
+ }
+
+ /**
+ * <p>Removes spaces (char <= 32) from start
+ * of this array, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trimLeft(null) = null
+ * StringUtils.trimLeft("") = ""
+ * StringUtils.trimLeft(" ") = ""
+ * StringUtils.trimLeft("abc") = "abc"
+ * StringUtils.trimLeft(" abc ") = "abc "
+ * </pre>
+ *
+ * @param bytes the byte array to be trimmed, may be null
+ * @return the position of the first byte which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimLeft( byte[] bytes, int pos ) {
+ if ( bytes == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos < bytes.length ) && ( bytes[pos] == ' ' ) )
+ {
+ pos++;
+ }
+
+ return pos;
+ }
+
+
+ /**
+ * <p>Removes spaces (char <= 32) from end
+ * of this String, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trimRight(null) = null
+ * StringUtils.trimRight("") = ""
+ * StringUtils.trimRight(" ") = ""
+ * StringUtils.trimRight("abc") = "abc"
+ * StringUtils.trimRight(" abc ") = " abc"
+ * </pre>
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, <code>null</code> if null String input
+ */
+ public static String trimRight( String str ) {
+ if ( isEmpty( str ) )
+ {
+ return "";
+ }
+
+ char[] array = str.toCharArray();
+ int start = 0;
+ int end = array.length;
+
+ while ( ( start < end ) && ( array[start] == ' ' ) )
+ {
+ start++;
+ }
+
+ return new String( array, start, ( end - start ) );
+ }
+
+ /**
+ * <p>Removes spaces (char <= 32) from end
+ * of this array, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trimRight(null) = null
+ * StringUtils.trimRight("") = ""
+ * StringUtils.trimRight(" ") = ""
+ * StringUtils.trimRight("abc") = "abc"
+ * StringUtils.trimRight(" abc ") = " abc"
+ * </pre>
+ *
+ * @param chars the chars array to be trimmed, may be null
+ * @return the position of the first char which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimRight( char[] chars, int pos ) {
+ if ( chars == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos > 0 ) && ( chars[pos - 1] == ' ' ) )
+ {
+ pos--;
+ }
+
+ return pos;
+ }
+
+ /**
+ * <p>Removes spaces (char <= 32) from end
+ * of this array, handling <code>null</code> by returning
+ * <code>null</code>.</p>
+ *
+ * Trim removes start characters <= 32.
+ *
+ * <pre>
+ * StringUtils.trimRight(null) = null
+ * StringUtils.trimRight("") = ""
+ * StringUtils.trimRight(" ") = ""
+ * StringUtils.trimRight("abc") = "abc"
+ * StringUtils.trimRight(" abc ") = " abc"
+ * </pre>
+ *
+ * @param bytes the chars array to be trimmed, may be null
+ * @return the position of the first char which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimRight( byte[] bytes, int pos ) {
+ if ( bytes == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos >= 0 ) && ( bytes[pos] == ' ' ) )
+ {
+ pos--;
+ }
+
+ return pos;
+ }
+
+ // Case conversion
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Converts a String to upper case as per {@link String#toUpperCase()}.</p>
+ *
+ * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <pre>
+ * StringUtils.upperCase(null) = null
+ * StringUtils.upperCase("") = ""
+ * StringUtils.upperCase("aBc") = "ABC"
+ * </pre>
+ *
+ * @param str the String to upper case, may be null
+ * @return the upper cased String, <code>null</code> if null String input
+ */
+ public static String upperCase(String str) {
+ if (str == null) {
+ return null;
+ }
+ return str.toUpperCase();
+ }
+
+ /**
+ * <p>Converts a String to lower case as per {@link String#toLowerCase()}.</p>
+ *
+ * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <pre>
+ * StringUtils.lowerCase(null) = null
+ * StringUtils.lowerCase("") = ""
+ * StringUtils.lowerCase("aBc") = "abc"
+ * </pre>
+ *
+ * @param str the String to lower case, may be null
+ * @return the lower cased String, <code>null</code> if null String input
+ */
+ public static String lowerCase(String str) {
+ if (str == null) {
+ return null;
+ }
+ return str.toLowerCase();
+ }
+
+ // Equals
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Compares two Strings, returning <code>true</code> if they are equal.</p>
+ *
+ * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
+ * references are considered to be equal. The comparison is case sensitive.</p>
+ *
+ * <pre>
+ * StringUtils.equals(null, null) = true
+ * StringUtils.equals(null, "abc") = false
+ * StringUtils.equals("abc", null) = false
+ * StringUtils.equals("abc", "abc") = true
+ * StringUtils.equals("abc", "ABC") = false
+ * </pre>
+ *
+ * @see java.lang.String#equals(Object)
+ * @param str1 the first String, may be null
+ * @param str2 the second String, may be null
+ * @return <code>true</code> if the Strings are equal, case sensitive, or
+ * both <code>null</code>
+ */
+ public static boolean equals(String str1, String str2) {
+ return str1 == null ? str2 == null : str1.equals(str2);
+ }
+
+ /**
+ * Return an UTF-8 encoded String
+ * @param bytes The byte array to be transformed to a String
+ * @return A String.
+ */
+ public static String utf8ToString( byte[] bytes )
+ {
+ if( bytes == null )
+ {
+ return "";
+ }
+
+ try
+ {
+ return new String( bytes, "UTF-8" );
+ }
+ catch ( UnsupportedEncodingException uee )
+ {
+ return "";
+ }
+ }
+
+ /**
+ * Return an UTF-8 encoded String
+ * @param bytes The byte array to be transformed to a String
+ * @param length The length of the byte array to be converted
+ * @return A String.
+ */
+ public static String utf8ToString( byte[] bytes, int length )
+ {
+ if( bytes == null )
+ {
+ return "";
+ }
+
+ try
+ {
+ return new String( bytes, 0, length, "UTF-8" );
+ }
+ catch ( UnsupportedEncodingException uee )
+ {
+ return "";
+ }
+ }
+
+ /**
+ * Return UTF-8 encoded byte[] representation of a String
+ * @param string The string to be transformed to a byte array
+ * @return The transformed byte array
+ */
+ public static byte[] getBytesUtf8( String string )
+ {
+ if( string == null )
+ {
+ return new byte[0];
+ }
+
+ try
+ {
+ return string.getBytes( "UTF-8" );
+ }
+ catch ( UnsupportedEncodingException uee )
+ {
+ return new byte[]{};
+ }
+ }
+
+ /**
+ * Utility method that return a String representation of a list
+ * @param list The list to transform to a string
+ * @return A csv string
+ */
+ public static String listToString( List list )
+ {
+ if ( ( list == null ) || ( list.size() == 0 ) )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+ boolean isFirst = true;
+
+ Iterator iter = list.iterator();
+
+ while ( iter.hasNext() )
+ {
+ if ( isFirst)
+ {
+ isFirst = false;
+ }
+ else
+ {
+ sb.append( ", " );
+ }
+
+ sb.append( iter.next() );
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Utility method that return a String representation of a list
+ * @param list The list to transform to a string
+ * @return A csv string
+ */
+ public static String listToString( List list, String tabs )
+ {
+ if ( ( list == null ) || ( list.size() == 0 ) )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+
+ Iterator iter = list.iterator();
+
+ while ( iter.hasNext() )
+ {
+ sb.append( tabs );
+ sb.append( iter.next() );
+ sb.append( '\n' );
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Utility method that return a String representation of a map. The elements
+ * will be represented as "key = value"
+ * @param map The map to transform to a string
+ * @return A csv string
+ */
+ public static String mapToString( Map map )
+ {
+ if ( ( map == null ) || ( map.size() == 0 ) )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+ boolean isFirst = true;
+
+ Iterator iter = map.keySet().iterator();
+
+ while ( iter.hasNext() )
+ {
+ if ( isFirst)
+ {
+ isFirst = false;
+ }
+ else
+ {
+ sb.append( ", " );
+ }
+
+ Object key = iter.next();
+ sb.append( key );
+ sb.append( " = '" ).append( map.get( key ) ).append( "'" );
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Utility method that return a String representation of a map. The elements
+ * will be represented as "key = value"
+ * @param map The map to transform to a string
+ * @return A csv string
*/
- public static boolean isEmpty(String string)
+ public static String mapToString( Map map, String tabs )
{
- return ( (string == null) || (string.length() == 0) );
+ if ( ( map == null ) || ( map.size() == 0 ) )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+
+ Iterator iter = map.keySet().iterator();
+
+ while ( iter.hasNext() )
+ {
+ Object key = iter.next();
+ sb.append( tabs );
+ sb.append( key );
+ Object value = map.get( key );
+
+ sb.append( " = '" ).append( value.toString() ).append( "'\n" );
+ }
+
+ return sb.toString();
}
}