UnsupportedEncodingException

  • Thread starter Bill Lattery via JavaKB.com
  • Start date
B

Bill Lattery via JavaKB.com

I am using java 1.4.2 with Sun One Studio 5.

I am using a getBytes() function:

String mac = "1.2.3.4"
byte[] bv = mac.getBytes("UTF-16");

When I try to compile I get this message:

"unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown"

Why do I get this?

Please help.

Thanks.

_____________________
Message posted via http://www.javakb.com
 
A

Ann

Bill Lattery via JavaKB.com said:
I am using java 1.4.2 with Sun One Studio 5.

I am using a getBytes() function:

String mac = "1.2.3.4"
byte[] bv = mac.getBytes("UTF-16");

When I try to compile I get this message:

"unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown"

Why do I get this?

You need a semicolon on the first line.
 
A

Andrew Thompson

T

Tor Iver Wilhelmsen

Bill Lattery via JavaKB.com said:
When I try to compile I get this message:

"unreported exception java.io.UnsupportedEncodingException; must be
caught or declared to be thrown"

Why do I get this?

Because you call a method that is DOCUMENTED to throw an unchecked
exception by that name. You either need to declare your method to
throw it, or surround the call to getBytes() with try/catch.
 
B

Boudewijn Dijkstra

Andrew Thompson said:
On Fri, 19 Nov 2004 22:38:44 GMT, Bill Lattery via JavaKB.com wrote:

// as Ann mentioned, this statement will not compile..
// please copy/paste code.
String mac = "1.2.3.4"

String mac = "1.2.3.4";
try {
byte[] bv = mac.getBytes("UTF-16");

} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}

I think it would be better to
throw new InternalError("missing required encoding: UTF-16");
in the catch block.
 
J

John C. Bollinger

Tor said:
Because you call a method that is DOCUMENTED to throw an unchecked
exception by that name. You either need to declare your method to
throw it, or surround the call to getBytes() with try/catch.

Documentation has little to do with it. UnsupportedEncodingException is
a *checked* exception that the method being invoked is declared to be
capable of throwing, therefore the method that invokes it must either
catch that exception or declare that it throws it. This is _never_ the
case for unchecked exceptions, whether they are declared in a throws
clause or not.


John Bollinger
(e-mail address removed)
 
B

Bill Lattery via JavaKB.com

Thanks Andrew!

You method works.

Now, I have another problem.

I am working with a Java-based program that performs SNMP. One of the MIB variables is a 6 byte octet message stream. The program retrieves the information and saves it on a table as a String. My part of the code must get the info as a String. I need to convert the String to a byte buffer.

The problem I have is that not all of the data is encoded properly.

For example: The MIB variable message is (in decimal): 00 04 -127 12 -115 -114

When I use the getBytes() function, the byte buffer contains (in decimal): 00 04 63 12 63 63.

I have used the Character Encoding Sets US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16BE, UTF-16LE, and windows-1252. None have given me the result I need.

What Character Encoding Set should I use to get the right values?

Thanks.
 
A

Andrew Thompson

Thanks Andrew!

You method works.

You're welcome.
Now, I have another problem. ....
The problem I have is that not all of the data is encoded properly.

Unfortunately encodings are a bit out of my field of expertise.
I'll wait to hear what the experts say.
 
T

Tor Iver Wilhelmsen

John C. Bollinger said:
Documentation has little to do with it. UnsupportedEncodingException
is a *checked* exception that the method being invoked is declared to
be capable of throwing, therefore the method that invokes it must
either catch that exception or declare that it throws it.

Sorry, yes, typed a bit fast there: I meant to write checked.
 
J

John C. Bollinger

Bill said:
I am working with a Java-based program that performs SNMP. One of the MIB variables is a 6 byte octet message stream. The program retrieves the information and saves it on a table as a String. My part of the code must get the info as a String. I need to convert the String to a byte buffer.

The MIB / SNMP terminology appears to be "octet _string_" (emphasis
mine) not "octet message stream".
The problem I have is that not all of the data is encoded properly.

That's rather unlikely. It is entirely possible that you don't know how
it is encoded, however.
For example: The MIB variable message is (in decimal): 00 04 -127 12 -115 -114

When I use the getBytes() function, the byte buffer contains (in decimal): 00 04 63 12 63 63.

To do this you have already coerced the byte sequence into a String
according to some encoding. The byte 63 (decimal) is a '?' in Unicode;
it signals that the charset you used does not map the current byte(s) to
a character.
I have used the Character Encoding Sets US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16BE, UTF-16LE, and windows-1252. None have given me the result I need.

How would you recognize the result when you got it? If you know what
the correct answer is, then that would be very pertinent.
What Character Encoding Set should I use to get the right values?

That may be device-dependent. MIB specifies data types in ASN.1, and
ASN.1 apparently does not define encodings to use (that's part of the
"Abstract" in "Abstract Syntax Notation"). The information is probably
carried by some other item in the MIB.

Have you considered the possibility that the string is empty? The
initial zero byte is very suspicious in that regard. If you are getting
fixed-length strings with variable-length content then there will be
some kind of fence byte to indicate the end of the meaningful data -- at
least when the string is shorter than the maximum length. Compare to C
strings.

If that's not it then you'll probably need to pass on more information.
For what it's worth, the charsets you have already tried cover all the
likely ones.


John Bollinger
(e-mail address removed)
 
B

Boudewijn Dijkstra

Bill Lattery via JavaKB.com said:
Thanks Andrew!

You method works.

Now, I have another problem.

I am working with a Java-based program that performs SNMP. One of the MIB
variables is a 6 byte octet message stream. The program retrieves the
information and saves it on a table as a String. My part of the code must
get the info as a String. I need to convert the String to a byte buffer.

The problem I have is that not all of the data is encoded properly.

For example: The MIB variable message is (in decimal): 00 04 -127 12
-115 -114

If you interpret these as characters (which they are obviously *not*), you
get:
0000 <control> NULL
0004 <control> END OF TEXT
FF81 HALFWIDTH KATAKANA LETTER TI
000C <control> FORM FEED (FF)
FF8D HALFWIDTH KATAKANA LETTER HE
FF8E HALFWIDTH KATAKANA LETTER HO

Don't expect any encoding to live up to this. Either create the byte array
yourself, or do &0xFF on every char and then use getBytes with ISO-8859-1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top