sun.net.www.ParseUtil.decode() 与 java.net.URLDecoder.decode()
我有一些旧代码正在调用 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);
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很难说,因为
ParseUtil
的语义没有很好的文档记录。我认为会完全放弃代码,并将其替换为使用
URI
和File(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 theFile(URI)
constructor.我怀疑这段代码根本没有做任何有用的事情。当然,任何执行 Replace() 操作的代码都会立即受到怀疑。尝试 new File(url.toURI()).getPath() 或 .getAbsolutePath()。
下一个问题是它认为它正在做什么构建权限?那也不会带你去任何地方。安全管理器将在必要时自行执行此操作,并检查生成的权限,并在必要时抛出 SecurityException。自己构建权限并不会给您该权限。
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.