This method is a bit special. The exception swallowing was done in purpose, as it's guaranteed that the conversion will not fail.
However, I agree with Stefan : it would probably a better idea to thow a RuntimeException in this case. Feel free to modify the code accordingly. On Sat, Mar 6, 2010 at 2:25 PM, Stefan Seelmann <[email protected]> wrote: > Felix Knecht wrote: > >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> IMO it's ok to return a default value in case of exception, but it's >> hard to find the error if you can't find anything in the log. ( in case >> of encoding exception) >> >> Is this so on purpose or is the error logged anywhere else (not obvious >> to me)? >> > > I think it that case we could wrap the UnsupportedEncodingException into a > RuntimeException. We know that UTF-8 is always supported, and in case it is > not there then something really strange is going on. > > My 2 cents. > Stefan > > > >> Felix >> >> >> >> shared-ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java >> [3144 ff] >> >> public static final String utf8ToString( byte[] bytes, int start, >> int length ) >> { >> if ( bytes == null ) >> { >> return ""; >> } >> >> try >> { >> return new String( bytes, start, 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 final byte[] getBytesUtf8( String string ) >> { >> if ( string == null ) >> { >> return new byte[0]; >> } >> >> try >> { >> return string.getBytes( "UTF-8" ); >> } >> catch ( UnsupportedEncodingException uee ) >> { >> return new byte[] >> {}; >> } >> } >> -----BEGIN PGP SIGNATURE----- >> Version: GnuPG v2.0.14 (GNU/Linux) >> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ >> >> iEYEARECAAYFAkuSVDMACgkQ2lZVCB08qHG6KQCgsbKUAX/ny4sK6Vht0fusXd4H >> dh4AoOq1KOKM6siPVeP4oakmSUqAyBVP >> =MUhh >> -----END PGP SIGNATURE----- >> > > -- Regards, Cordialement, Emmanuel Lécharny www.iktek.com
