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 / July 2006

Tip: Looking for answers? Try searching our database.

How to detect the failure of byte[] => String conversion

Thread view: 
Sakagami Hiroki - 02 Jul 2006 16:18 GMT
Hi,

When I want to convert a byte array to a String object, I can use
String constructor with charset parameter:

 String result = new String(bytearray, charset);

Here, if the bytearray contains any illegal sequence of bytes in
charset, they seems to be replaced with `?'.  Is it possible to throw
Exception instead of using replacement characters?

Regards,

--
Sakagami Hiroki
Timo Stamm - 02 Jul 2006 17:37 GMT
Sakagami Hiroki schrieb:
> Hi,
>
[quoted text clipped - 6 lines]
> charset, they seems to be replaced with `?'.  Is it possible to throw
> Exception instead of using replacement characters?

Not using the String class. The String class uses the Charset and
CharsetDecoder classes and you can do so too:

  CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
  try {
    CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
    r.toString(); // your encoded String
  } catch(CharacterCodingException e) {
    // ...
  }

Timo
Sakagami Hiroki - 03 Jul 2006 15:39 GMT
> Not using the String class. The String class uses the Charset and
> CharsetDecoder classes and you can do so too:
[quoted text clipped - 6 lines]
>      // ...
>    }

Works fine.  Thanks!

--
Sakagami Hiroki


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



©2009 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.