use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.
the class ObjectUtil method serialize.
/**
* 序列化<br>
* 对象必须实现Serializable接口
*
* @param <T> 对象类型
* @param obj 要被序列化的对象
* @return 序列化后的字节码
*/
public static <T> byte[] serialize(T obj) {
if (null == obj || false == (obj instanceof Serializable)) {
return null;
}
FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(byteOut);
oos.writeObject(obj);
oos.flush();
} catch (Exception e) {
throw new UtilException(e);
} finally {
IoUtil.close(oos);
}
return byteOut.toByteArray();
}
use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.
the class ObjectUtil method cloneByStream.
/**
* 序列化后拷贝流的方式克隆<br>
* 对象必须实现Serializable接口
*
* @param <T> 对象类型
* @param obj 被克隆对象
* @return 克隆后的对象
* @throws UtilException IO异常和ClassNotFoundException封装
*/
@SuppressWarnings("unchecked")
public static <T> T cloneByStream(T obj) {
if (null == obj || false == (obj instanceof Serializable)) {
return null;
}
final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(byteOut);
out.writeObject(obj);
out.flush();
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
return (T) in.readObject();
} catch (Exception e) {
throw new UtilException(e);
} finally {
IoUtil.close(out);
}
}
use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.
the class HttpUtil method downloadString.
/**
* 下载远程文本
*
* @param url 请求的url
* @param customCharset 自定义的字符集,可以使用{@link CharsetUtil#charset} 方法转换
* @param streamPress 进度条 {@link StreamProgress}
* @return 文本
*/
public static String downloadString(String url, Charset customCharset, StreamProgress streamPress) {
if (StrUtil.isBlank(url)) {
throw new NullPointerException("[url] is null!");
}
FastByteArrayOutputStream out = new FastByteArrayOutputStream();
download(url, out, true, streamPress);
return null == customCharset ? out.toString() : out.toString(customCharset);
}
use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.
the class HttpDownloader method downloadString.
/**
* 下载远程文本
*
* @param url 请求的url
* @param customCharset 自定义的字符集,可以使用{@code CharsetUtil#charset} 方法转换
* @param streamPress 进度条 {@link StreamProgress}
* @return 文本
*/
public static String downloadString(String url, Charset customCharset, StreamProgress streamPress) {
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
download(url, out, true, streamPress);
return null == customCharset ? out.toString() : out.toString(customCharset);
}
use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.
the class SerializeUtil method serialize.
/**
* 序列化<br>
* 对象必须实现Serializable接口
*
* @param <T> 对象类型
* @param obj 要被序列化的对象
* @return 序列化后的字节码
*/
public static <T> byte[] serialize(T obj) {
if (false == (obj instanceof Serializable)) {
return null;
}
final FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
IoUtil.writeObjects(byteOut, false, (Serializable) obj);
return byteOut.toByteArray();
}
Aggregations