users@jersey.java.net

.toASCIIString instead of .toString

From: Max Scheffler <max.scheffler_at_freiheit.com>
Date: Tue, 17 Jun 2008 11:44:54 +0200

Hi there,

I found some code snippets in the jersey examples which look like the
following:

  URI userUri = ub.
      path(userEntity.getUserid()).
      build();
  uriArray.put(userUri.toString());

which means nothing else than using the .toString method from the URI class.

I found the .toASCIIString() method of URI. The JavaDoc of this method
tells us:

  Returns the content of this URI as a US-ASCII string.

  If this URI does not contain any characters in the other category then
  an invocation of this method will return the same value as an
  invocation of the toString method. Otherwise this method works as if
  by invoking that method and then encoding the result.

  @return The string form of this URI, encoded as needed
           so that it only contains characters in the US-ASCII
           charset

IMHO is the .toASCIIString() method the more right one to generate a
String from a URI.

To prove that I'm right i wrote this little test:

  public static void main ( String[] args ) throws URISyntaxException {
    URI uri = new URI( "http://www.example.org/äö" );
    System.out.println( uri.toString() );
    System.out.println( uri.toASCIIString() );
  }

Which returns the following to me:

  http://www.example.org/?
  http://www.example.org/%C3%A4%C3%B6

Greetings
        Max
-- 
Max Scheffler
Dipl.-Inf.
freiheit.com technologies gmbh
Straßenbahnring 22 / 20251 Hamburg, Germany
fon       +49 (0)40 / 890584-0
fax       +49 (0)40 / 890584-20
HRB Hamburg 70814
585C 6EC1 6C6A CE17 FF25 36B0 27EA BC20 4610 5BB0
Geschäftsführer: Claudia Dietze, Stefan Richter, Jörg Kirchhof