I've written some tests in which I use org.springframework.core.io.ClassPathResource to get file that will be sent to API. One to the requirements were to support unicode characters in the filename so I've loaded file containing random mix of unicode characters and it failed to decode URI. After some diagnostics it seems like this cute boy of character "" (cat emoji, U+1F431) is breaking it. I've encoded it using unicode escape ("\u") for Java literals in case something is wrong with IDE encoding/view. But nothing changed. So I've created the demo using Spring Boot initializr in case some packages had any effect and began debugging. From testing it seems that characters that can be encoded using single "\u" escape sequence work just fine (GAMMA did work) but those that need to use surrogate pairs don't eg. being "\uD83D\uDC31".
URL encoded emoji/filename in question looks like this %ed%a0%bd%ed%b0%b1 comes from sun.net.www.ParseUtil.encodePath(String path, boolean flag), while error is thrown in the same class but decode(String path) method. I couldn't use ParseUtil myself directly because it wasn't accessible even though it's public. Can anyone explain what is going on here? Is it just a bug in ParseUtils/Java or I'm missing something?
I'm using Java 11 zulu on Windows 10.
Demo that I used:
@SpringBootApplication
public class DemoApplication {
private static final String GAMMA = "\u03DD"; //ϝ greek small letter digamma
private static final String CAT = "\uD83D\uDC31"; // cat emoji
public static void main(String[] args) throws IOException {
var c = new ClassPathResource(CAT).lastModified();
System.out.println("cat: " + c);
}
}
Exception from demo program, it's exactly the same as in my real tests but with shorter stack.
Exception in thread "main" java.lang.IllegalArgumentException: Error decoding percent encoded characters
at java.base/sun.net.www.ParseUtil.decode(ParseUtil.java:214)
at java.base/sun.net.www.protocol.file.Handler.openConnection(Handler.java:82)
at java.base/sun.net.www.protocol.file.Handler.openConnection(Handler.java:72)
at java.base/java.net.URL.openConnection(URL.java:1074)
at org.springframework.core.io.AbstractFileResolvingResource.lastModified(AbstractFileResolvingResource.java:272)
at com.example.demo.DemoApplication.main(DemoApplication.java:34)
PS. both 'ϝ' and '' are legit windows file names so if gamma was visible so cat should be as they were next to each other so they are visible. It shouldn't be problem of missing file.