Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35800
| From | j4n bur53 <janburse@fastmail.fm> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: java.net.URLEncoder.encode + UTF-8 (rant) |
| Date | 2016-10-17 16:55 +0200 |
| Organization | albasani.net |
| Message-ID | <nu2opu$iq2$1@news.albasani.net> (permalink) |
| References | <slrno09hi9.456.avl@login.logic.tuwien.ac.at> |
Andreas Leitgeb schrieb:
> <rant target="java.net.URLEncoder.encode(String s, String enc)">
>
> This method should (according to the w3c) only be used with UTF-8
> and UTF-8 is always supposed to be supported by Java.
>
> Why is there no java.net.URLEncoder.encodeUTF8(String s) that
> would free me from that boring
> try {...} catch (UnsupportedEncodingException ign) {...}
> boilerplate? (What code do good(tm) Java programmers even put
> into this catch clause, that is expected to never run, anyway?)
>
> Other JSL methods dealing with encodings at least let me specify the
> encoding as an instance of Charset, so I can pass StandardCharsets.UTF_8
> there and be safe against typos leading to an exception at runtime -
> one, whose only fix would be correcting the source.
>
> For URLEncoder it would even make sense to "re-precate" the plain
> encoding(String s) to always use UTF-8 instead of native encoding.
> It surely would fix more ancient applications than it would break.
>
> </rant>
>
Since JDK 1.8 or so UTF-8 is required to exist.
Annoying indeed, so I rolled my own(*):
- Some methods such as encode() or decode() still
throw an UnsupportedEncodingException exception.
- Other methods such as sysUriHash() etc.. that
assume UTF-8, don't throw it.
- MalformedURLException and CharacterCodingException
are still thrown, have some special role here.
To answer your question, I am using the following idiom:
try {
[...]
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(SHOULDNT_HAPPEN, x);
}
i.e. map the checked exception, to an unchecked exception.
Could also map to Error, but RuntimeException is less severe.
Bye
(*)
https://github.com/jburse/jekejeke-devel/blob/master/utils/headless/matula/util/system/ForeignUri.java
Disclaimer: There is no warranty for the excerpts.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
java.net.URLEncoder.encode + UTF-8 (rant) Andreas Leitgeb <avl@logic.at> - 2016-10-17 12:38 +0000
Re: java.net.URLEncoder.encode + UTF-8 (rant) j4n bur53 <janburse@fastmail.fm> - 2016-10-17 16:55 +0200
Re: java.net.URLEncoder.encode + UTF-8 (rant) Arne Vajhøj <arne@vajhoej.dk> - 2016-10-19 21:35 -0400
Re: java.net.URLEncoder.encode + UTF-8 (rant) Daniele Futtorovic <da.futt.news@laposte.net> - 2016-11-14 20:05 +0100
Re: java.net.URLEncoder.encode + UTF-8 (rant) Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-11-14 14:45 -0500
csiph-web