Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

getBytesUTF8Encoded

Thread view: 
jeanlutrin@yahoo.fr - 02 Mar 2005 21:21 GMT
Hi all,

To obtain a byte[] in the UTF-8 encoding from a String
object, I use a getBytes("UTF-8").

This is cumbersome...

Firstly, it is error prone (I mistakenly wrote "UTF-9"
just today, which more or less decided me to post this
message).

Secondly, I'm forced to catch an "unsupported encoding"
exception. Which is completely dumb, as every J2SE JVM
is forced, by the specs, to offer the UTF-8 encoding.

So, I'd like to have a method getBytesUTF8Encoded() in
the String class. Without parameter, without the need
to catch a non-sense exception (a method returning
directly "UTF-8", without needing to pass a prone-to-
error string parameter, would not be able to throw
an UnsupportedEncodingException).

So:

getBytesUTF8Encoded()
getUTF8Bytes()

Whatever.

But something else than the current cumbersome and
prone to error getBytes("UTF-8") throwing a phantom
UnsupportedEncodingException.

Your thoughts on this?

  Jean

P.S: I'm not against adding getBytesISOLatin1() method
etc., as long as it's for encoding *mandatory by the
specs* to be in the JVM.
Thomas Weidenfeller - 03 Mar 2005 10:12 GMT
> So, I'd like to have a method getBytesUTF8Encoded() in
> the String class.

http://jcp.org/

Good luck,

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Antti S. Brax - 03 Mar 2005 11:50 GMT
jeanlutrin@yahoo.fr wrote in comp.lang.java.programmer:
> To obtain a byte[] in the UTF-8 encoding from a String
> object, I use a getBytes("UTF-8").

> Firstly, it is error prone (I mistakenly wrote "UTF-9"
> just today, which more or less decided me to post this
> message).

    Well, if you're worried about your ability to write string   
    constants correctly why don't you use constants for your
    strings?

    public static final String UTF8 = "UTF-8";
    ...
    str.getBytes(UTF8);

    Compiler will take care of typos for you (as long as you make
    sure you've written the constant correctly.

    Also, if that typo was hard to find then there is something
    wrong with your program design. Do you write tests for your
    code at all?

> So, I'd like to have a method getBytesUTF8Encoded() in
> the String class.

    Write a helper class with a static method that does what you
    need. It is trivial.

Signature

Antti S. Brax                  Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/         http://www.cs.helsinki.fi/u/abrax/hlb/
       "Disconnect this cable to shorten, re-connect to lengthen."
         -- Instructions on Logitech's USB mouse extension cord.

jeanlutrin@yahoo.fr - 04 Mar 2005 03:55 GMT
Thanks for your kind answer.

>... if you're worried about your ability to write string
> constants correctly...

no, you're oversimplifying what I explained in details.

I'm not "worried about my ability to write string constants
correctly", but thanks for the concern.

> Also, if that typo was hard to find then there is something
> wrong with your program design. Do you write tests for your
> code at all?

Woaw.

At first look, that paragraph contains at least two logical fallacies
(first hint: I never said the typo was hard to find; second
hint: you're trying to shift the discussion on program design).

But, on a second look, you weren't really asserting anything: it
was just a question! Which, ironically, the way you formulated
it, makes it possible to easily name /three/ logical fallacies
contained in a single, short, paragraph.

If you want to talk about "program design", shall we discuss
the logic behind having to catch an impossible exception?

What do you think about being forced to catch an exception that
is, by the specs, impossible to happen?

Because, you know, quite frankly I don't know which is more
absurd between having to catch an exception that can never
happen or Logitech's USB mouse extension cord instructions
that can be found in your sig (it's scary for sure ;)

;)

See you soon on c.l.j.p.,

Jean

P.S : and yes, I do use unit tests, yes, I caught the typo
on the first launch and, no, a static method in a helper
class isn't nowhere near as convenient as would be the
ability to call a method directly on a String object.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.