cn.hutool.core.io.resource.CharSequenceResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.core.io.resource;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.net.URL;
import java.nio.charset.Charset;
/**
* {@link CharSequence}资源,字符串做为资源
*
* @author looly
* @since 5.5.2
*/
public class CharSequenceResource implements Resource, Serializable {
private static final long serialVersionUID = 1L;
private final CharSequence data;
private final CharSequence name;
private final Charset charset;
/**
* 构造,使用UTF8编码
*
* @param data 资源数据
*/
public CharSequenceResource(CharSequence data) {
this(data, null);
}
/**
* 构造,使用UTF8编码
*
* @param data 资源数据
* @param name 资源名称
*/
public CharSequenceResource(CharSequence data, String name) {
this(data, name, CharsetUtil.CHARSET_UTF_8);
}
/**
* 构造
*
* @param data 资源数据
* @param name 资源名称
* @param charset 编码
*/
public CharSequenceResource(CharSequence data, CharSequence name, Charset charset) {
this.data = data;
this.name = name;
this.charset = charset;
}
@Override
public String getName() {
return StrUtil.str(this.name);
}
@Override
public URL getUrl() {
return null;
}
@Override
public InputStream getStream() {
return new ByteArrayInputStream(readBytes());
}
@Override
public BufferedReader getReader(Charset charset) {
return IoUtil.getReader(new StringReader(this.data.toString()));
}
@Override
public String readStr(Charset charset) throws IORuntimeException {
return this.data.toString();
}
@Override
public byte[] readBytes() throws IORuntimeException {
return this.data.toString().getBytes(this.charset);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy