sun.net.www.ParseUtil.decode() 与 java.net.URLDecoder.decode()

发布于 2024-11-26 22:33:45 字数 697 浏览 0 评论 0原文

我有一些旧代码正在调用 sun.net.www.ParseUtil.decode()。我想避免调用供应商特定的函数,因此我想用其他东西替换该调用。

我可以使用 java.net.URLDecoder.decode() 作为直接替代品还是有什么我应该注意的?

该调用用于将文件 URL 转换为自定义类加载器内的正常路径:(

URL url = //...
if(url.getProtocol().equals("file"))) {
    String path = url.getFile().replace('/', File.separatorChar);
    path = ParseUtil.decode(path);
    if (path.endsWith(File.separator)){
       path += "-";
    }
    p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
 }

请注意,这与 java.net.UrlClassLoader.getPermissions())

I have some legacy code that is calling sun.net.www.ParseUtil.decode(). I would like to avoid calling vendor specific functions, so I want to replace the call with something else.

Can I use java.net.URLDecoder.decode() as a drop-in replacement or is there something I should look out for?

The call is used to convert a file URL to a normal path inside a custom classloader:

URL url = //...
if(url.getProtocol().equals("file"))) {
    String path = url.getFile().replace('/', File.separatorChar);
    path = ParseUtil.decode(path);
    if (path.endsWith(File.separator)){
       path += "-";
    }
    p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
 }

(Note that this is almost exactly the same code as in java.net.UrlClassLoader.getPermissions())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-12-03 22:33:45

很难说,因为 ParseUtil 的语义没有很好的文档记录。

我认为会完全放弃代码,并将其替换为使用 URIFile(URI) 构造函数的代码。

It is hard to say since the semantics of ParseUtil are not well documented.

I think would ditch the code entirely and replace it with code that uses URI and the File(URI) constructor.

剪不断理还乱 2024-12-03 22:33:45
URL url = //...
if(url.getProtocol().equals("file"))) {
    String path = url.getFile().replace('/', File.separatorChar);
    path = ParseUtil.decode(path);
    if (path.endsWith(File.separator)){
       path += "-";
    }
    p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
 }

我怀疑这段代码根本没有做任何有用的事情。当然,任何执行 Replace() 操作的代码都会立即受到怀疑。尝试 new File(url.toURI()).getPath() 或 .getAbsolutePath()。

下一个问题是它认为它正在做什么构建权限?那也不会带你去任何地方。安全管理器将在必要时自行执行此操作,并检查生成的权限,并在必要时抛出 SecurityException。自己构建权限并不会给您该权限。

URL url = //...
if(url.getProtocol().equals("file"))) {
    String path = url.getFile().replace('/', File.separatorChar);
    path = ParseUtil.decode(path);
    if (path.endsWith(File.separator)){
       path += "-";
    }
    p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
 }

I doubt that this code is doing anything useful at all. Certainly any code that does that replace() operation is immediately suspect. Try new File(url.toURI()).getPath() or .getAbsolutePath().

The next question is what does it think it is doing constructing a Permission? That doesn't get you anywhere either. The Security Manager will do that itself when necessary and check the resulting permission and throw a SecurityException if necessary. Constructing a Permission yourself doesn't give you that permission.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文