mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix issue#IAO9UZ
This commit is contained in:
parent
a2e8c9d28f
commit
98055b50ad
@ -16,14 +16,9 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.io.resource;
|
package org.dromara.hutool.core.io.resource;
|
||||||
|
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
|
||||||
import org.dromara.hutool.core.io.IoUtil;
|
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,12 +27,13 @@ import java.nio.charset.Charset;
|
|||||||
* @author looly
|
* @author looly
|
||||||
* @since 5.5.2
|
* @since 5.5.2
|
||||||
*/
|
*/
|
||||||
public class CharSequenceResource implements Resource, Serializable {
|
public class CharSequenceResource extends BytesResource {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final CharSequence data;
|
/**
|
||||||
private final CharSequence name;
|
* 由于{@link Charset} 无法序列化,此处使用编码名称
|
||||||
private final Charset charset;
|
*/
|
||||||
|
private final String charsetName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造,使用UTF8编码
|
* 构造,使用UTF8编码
|
||||||
@ -65,45 +61,35 @@ public class CharSequenceResource implements Resource, Serializable {
|
|||||||
* @param name 资源名称
|
* @param name 资源名称
|
||||||
* @param charset 编码
|
* @param charset 编码
|
||||||
*/
|
*/
|
||||||
public CharSequenceResource(final CharSequence data, final CharSequence name, final Charset charset) {
|
public CharSequenceResource(final CharSequence data, final String name, final Charset charset) {
|
||||||
this.data = data;
|
super(ByteUtil.toBytes(data, charset), name);
|
||||||
this.name = name;
|
this.charsetName = charset.name();
|
||||||
this.charset = charset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String getName() {
|
* 读取为字符串
|
||||||
return StrUtil.toStringOrNull(this.name);
|
*
|
||||||
|
* @return 字符串
|
||||||
|
*/
|
||||||
|
public String readStr() {
|
||||||
|
return readStr(getCharset());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public URL getUrl() {
|
* 获取编码名
|
||||||
return null;
|
*
|
||||||
|
* @return 编码名
|
||||||
|
*/
|
||||||
|
public String getCharsetName() {
|
||||||
|
return this.charsetName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public long size() {
|
* 获取编码
|
||||||
return data.length();
|
*
|
||||||
|
* @return 编码
|
||||||
|
*/
|
||||||
|
public Charset getCharset() {
|
||||||
|
return CharsetUtil.charset(this.charsetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream getStream() {
|
|
||||||
return new ByteArrayInputStream(readBytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BufferedReader getReader(final Charset charset) {
|
|
||||||
return IoUtil.toBuffered(new StringReader(this.data.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String readStr(final Charset charset) throws IORuntimeException {
|
|
||||||
return this.data.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] readBytes() throws IORuntimeException {
|
|
||||||
return ByteUtil.toBytes(this.data, this.charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user