Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35800

Re: java.net.URLEncoder.encode + UTF-8 (rant)

Path csiph.com!weretis.net!feeder4.news.weretis.net!news.albasani.net!.POSTED!not-for-mail
From j4n bur53 <janburse@fastmail.fm>
Newsgroups comp.lang.java.programmer
Subject Re: java.net.URLEncoder.encode + UTF-8 (rant)
Date Mon, 17 Oct 2016 16:55:58 +0200
Organization albasani.net
Lines 56
Message-ID <nu2opu$iq2$1@news.albasani.net> (permalink)
References <slrno09hi9.456.avl@login.logic.tuwien.ac.at>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net 8fGHxSY21NFlke5vk2YQEK0HRgtLCoOV9KVMxInpwpizr/AVb/3tEDfvto3ue0k+zWmTFsuopNj+FlydX7oRcpEmZ85AgRx5UToPpZ5PIKPvZdRf7n5VPh+2ZNAMeFAY
NNTP-Posting-Date Mon, 17 Oct 2016 14:55:58 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="8mgkNJ24blADDqMMGrgtXzQTS3aLCZvRPGnm8tcP8/afFUE52xKfNF0zBFSaUO/oyU6yVGt6624Q+J91uHaC6+JTy1k3a1LripvrTLJqGvKCJObBJDuf53H+isxOvuSF"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40
In-Reply-To <slrno09hi9.456.avl@login.logic.tuwien.ac.at>
Cancel-Lock sha1:NjbIPjHYHQDrxCCIMSyUGGmLUfM=
Xref csiph.com comp.lang.java.programmer:35800

Show key headers only | View raw


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


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