Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell11
提交
31280f9a
D
dragonwell11
项目概览
openanolis
/
dragonwell11
通知
7
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell11
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
31280f9a
编写于
6月 18, 2013
作者:
S
sundar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8008915: URLReader constructor should allow specifying encoding
Reviewed-by: hannesw, lagergren
上级
207b15cc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
128 addition
and
5 deletion
+128
-5
nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
...rn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
+3
-1
nashorn/src/jdk/nashorn/api/scripting/URLReader.java
nashorn/src/jdk/nashorn/api/scripting/URLReader.java
+43
-3
nashorn/src/jdk/nashorn/internal/runtime/Source.java
nashorn/src/jdk/nashorn/internal/runtime/Source.java
+82
-1
未找到文件。
nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
浏览文件 @
31280f9a
...
...
@@ -34,6 +34,7 @@ import java.io.InputStreamReader;
import
java.io.Reader
;
import
java.lang.reflect.Method
;
import
java.net.URL
;
import
java.nio.charset.Charset
;
import
java.security.AccessController
;
import
java.security.PrivilegedAction
;
import
java.security.PrivilegedActionException
;
...
...
@@ -121,7 +122,8 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C
try
{
if
(
reader
instanceof
URLReader
)
{
final
URL
url
=
((
URLReader
)
reader
).
getURL
();
return
evalImpl
(
compileImpl
(
new
Source
(
url
.
toString
(),
url
),
ctxt
),
ctxt
);
final
Charset
cs
=
((
URLReader
)
reader
).
getCharset
();
return
evalImpl
(
compileImpl
(
new
Source
(
url
.
toString
(),
url
,
cs
),
ctxt
),
ctxt
);
}
return
evalImpl
(
Source
.
readFully
(
reader
),
ctxt
);
}
catch
(
final
IOException
e
)
{
...
...
nashorn/src/jdk/nashorn/api/scripting/URLReader.java
浏览文件 @
31280f9a
...
...
@@ -25,10 +25,12 @@
package
jdk.nashorn.api.scripting
;
import
java.io.CharArrayReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.Reader
;
import
java.net.URL
;
import
java.nio.charset.Charset
;
import
jdk.nashorn.internal.runtime.Source
;
/**
* A Reader that reads from a URL. Used to make sure that the reader
...
...
@@ -36,7 +38,10 @@ import java.net.URL;
*/
public
final
class
URLReader
extends
Reader
{
// underlying URL
private
URL
url
;
private
final
URL
url
;
// Charset used to convert
private
final
Charset
cs
;
// lazily initialized underlying reader for URL
private
Reader
reader
;
...
...
@@ -44,9 +49,35 @@ public final class URLReader extends Reader {
* Constructor
*
* @param url URL for this URLReader
* @throws NullPointerException if url is null
*/
public
URLReader
(
final
URL
url
)
{
this
(
url
,
(
Charset
)
null
);
}
/**
* Constructor
*
* @param url URL for this URLReader
* @param charsetName Name of the Charset used to convert bytes to chars
* @throws NullPointerException if url is null
*/
public
URLReader
(
final
URL
url
,
final
String
charsetName
)
{
this
(
url
,
Charset
.
forName
(
charsetName
));
}
/**
* Constructor
*
* @param url URL for this URLReader
* @param cs Charset used to convert bytes to chars
* @throws NullPointerException if url is null
*/
public
URLReader
(
final
URL
url
,
final
Charset
cs
)
{
// null check
url
.
getClass
();
this
.
url
=
url
;
this
.
cs
=
cs
;
}
@Override
...
...
@@ -67,11 +98,20 @@ public final class URLReader extends Reader {
return
url
;
}
/**
* Charset used by this reader
*
* @return the Chartset used to convert bytes to chars
*/
public
Charset
getCharset
()
{
return
cs
;
}
// lazily initialize char array reader using URL content
private
Reader
getReader
()
throws
IOException
{
synchronized
(
lock
)
{
if
(
reader
==
null
)
{
reader
=
new
InputStreamReader
(
url
.
openStream
(
));
reader
=
new
CharArrayReader
(
Source
.
readFully
(
url
,
cs
));
}
}
...
...
nashorn/src/jdk/nashorn/internal/runtime/Source.java
浏览文件 @
31280f9a
...
...
@@ -116,7 +116,20 @@ public final class Source {
* @throws IOException if source cannot be loaded
*/
public
Source
(
final
String
name
,
final
URL
url
)
throws
IOException
{
this
(
name
,
baseURL
(
url
,
null
),
readFully
(
url
.
openStream
()),
url
);
this
(
name
,
baseURL
(
url
,
null
),
readFully
(
url
),
url
);
}
/**
* Constructor
*
* @param name source name
* @param url url from which source can be loaded
* @param cs Charset used to convert bytes to chars
*
* @throws IOException if source cannot be loaded
*/
public
Source
(
final
String
name
,
final
URL
url
,
final
Charset
cs
)
throws
IOException
{
this
(
name
,
baseURL
(
url
,
null
),
readFully
(
url
,
cs
),
url
);
}
/**
...
...
@@ -131,6 +144,19 @@ public final class Source {
this
(
name
,
dirName
(
file
,
null
),
readFully
(
file
),
getURLFromFile
(
file
));
}
/**
* Constructor
*
* @param name source name
* @param file file from which source can be loaded
* @param cs Charset used to convert bytes to chars
*
* @throws IOException if source cannot be loaded
*/
public
Source
(
final
String
name
,
final
File
file
,
final
Charset
cs
)
throws
IOException
{
this
(
name
,
dirName
(
file
,
null
),
readFully
(
file
,
cs
),
getURLFromFile
(
file
));
}
@Override
public
boolean
equals
(
final
Object
obj
)
{
if
(
this
==
obj
)
{
...
...
@@ -343,6 +369,53 @@ public final class Source {
return
byteToCharArray
(
Files
.
readAllBytes
(
file
.
toPath
()));
}
/**
* Read all of the source until end of file. Return it as char array
*
* @param file source file
* @param cs Charset used to convert bytes to chars
* @return source as content
*
* @throws IOException if source could not be read
*/
public
static
char
[]
readFully
(
final
File
file
,
final
Charset
cs
)
throws
IOException
{
if
(!
file
.
isFile
())
{
throw
new
IOException
(
file
+
" is not a file"
);
//TODO localize?
}
final
byte
[]
buf
=
Files
.
readAllBytes
(
file
.
toPath
());
if
(
cs
!=
null
)
{
return
new
String
(
buf
,
cs
).
toCharArray
();
}
else
{
return
byteToCharArray
(
buf
);
}
}
/**
* Read all of the source until end of stream from the given URL. Return it as char array
*
* @param url URL to read content from
* @return source as content
*
* @throws IOException if source could not be read
*/
public
static
char
[]
readFully
(
final
URL
url
)
throws
IOException
{
return
readFully
(
url
.
openStream
());
}
/**
* Read all of the source until end of file. Return it as char array
*
* @param url URL to read content from
* @param cs Charset used to convert bytes to chars
* @return source as content
*
* @throws IOException if source could not be read
*/
public
static
char
[]
readFully
(
final
URL
url
,
final
Charset
cs
)
throws
IOException
{
return
readFully
(
url
.
openStream
(),
cs
);
}
/**
* Get the base url. This is currently used for testing only
* @param url a URL
...
...
@@ -391,6 +464,14 @@ public final class Source {
return
(
idx
!=
-
1
)?
name
.
substring
(
0
,
idx
+
1
)
:
defaultValue
;
}
private
static
char
[]
readFully
(
final
InputStream
is
,
final
Charset
cs
)
throws
IOException
{
if
(
cs
!=
null
)
{
return
new
String
(
readBytes
(
is
),
cs
).
toCharArray
();
}
else
{
return
readFully
(
is
);
}
}
private
static
char
[]
readFully
(
final
InputStream
is
)
throws
IOException
{
return
byteToCharArray
(
readBytes
(
is
));
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录