Search in sources :

Example 1 with FastByteArrayOutputStream

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();
}
Also used : Serializable(java.io.Serializable) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) ObjectOutputStream(java.io.ObjectOutputStream) UtilException(cn.hutool.core.exceptions.UtilException)

Example 2 with FastByteArrayOutputStream

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);
    }
}
Also used : Serializable(java.io.Serializable) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UtilException(cn.hutool.core.exceptions.UtilException) ObjectOutputStream(java.io.ObjectOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with FastByteArrayOutputStream

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);
}
Also used : FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream)

Example 4 with FastByteArrayOutputStream

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);
}
Also used : FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream)

Example 5 with FastByteArrayOutputStream

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();
}
Also used : Serializable(java.io.Serializable) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream)

Aggregations

FastByteArrayOutputStream (cn.hutool.core.io.FastByteArrayOutputStream)12 UtilException (cn.hutool.core.exceptions.UtilException)3 Serializable (java.io.Serializable)3 ObjectOutputStream (java.io.ObjectOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 PrintStream (java.io.PrintStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1