Search in sources :

Example 6 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)

Example 7 with FastByteArrayOutputStream

use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.

the class ZipUtil method gzip.

/**
 * Gzip压缩处理
 *
 * @param val 被压缩的字节流
 * @return 压缩后的字节流
 * @throws UtilException IO异常
 */
public static byte[] gzip(byte[] val) throws UtilException {
    FastByteArrayOutputStream bos = new FastByteArrayOutputStream(val.length);
    GZIPOutputStream gos = null;
    try {
        gos = new GZIPOutputStream(bos);
        gos.write(val, 0, val.length);
        gos.finish();
        gos.flush();
        val = bos.toByteArray();
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(gos);
    }
    return val;
}
Also used : FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException)

Example 8 with FastByteArrayOutputStream

use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.

the class ZipUtil method unGzip.

/**
 * Gzip解压处理
 *
 * @param in     Gzip数据
 * @param length 估算长度,如果无法确定请传入{@link #DEFAULT_BYTE_ARRAY_LENGTH}
 * @return 解压后的数据
 * @throws UtilException IO异常
 * @since 4.1.18
 */
public static byte[] unGzip(InputStream in, int length) throws UtilException {
    FastByteArrayOutputStream bos = new FastByteArrayOutputStream(length);
    Gzip.of(in, bos).unGzip().close();
    return bos.toByteArray();
}
Also used : FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream)

Example 9 with FastByteArrayOutputStream

use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.

the class ExceptionUtil method stacktraceToString.

/**
 * 堆栈转为完整字符串
 *
 * @param throwable           异常对象
 * @param limit               限制最大长度,&gt;0表示不限制长度
 * @param replaceCharToStrMap 替换字符为指定字符串
 * @return 堆栈转为的字符串
 */
public static String stacktraceToString(Throwable throwable, int limit, Map<Character, String> replaceCharToStrMap) {
    final FastByteArrayOutputStream baos = new FastByteArrayOutputStream();
    throwable.printStackTrace(new PrintStream(baos));
    final String exceptionStr = baos.toString();
    final int length = exceptionStr.length();
    if (limit < 0 || limit > length) {
        limit = length;
    }
    if (MapUtil.isNotEmpty(replaceCharToStrMap)) {
        final StringBuilder sb = StrUtil.builder();
        char c;
        String value;
        for (int i = 0; i < limit; i++) {
            c = exceptionStr.charAt(i);
            value = replaceCharToStrMap.get(c);
            if (null != value) {
                sb.append(value);
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    } else {
        if (limit == length) {
            return exceptionStr;
        }
        return StrUtil.subPre(exceptionStr, limit);
    }
}
Also used : PrintStream(java.io.PrintStream) FastByteArrayOutputStream(cn.hutool.core.io.FastByteArrayOutputStream)

Example 10 with FastByteArrayOutputStream

use of cn.hutool.core.io.FastByteArrayOutputStream in project hutool by looly.

the class ASN1Util method encode.

/**
 * 编码为指定ASN1格式
 *
 * @param asn1Encoding 编码格式,见{@link ASN1Encoding},可选DER、BER或DL
 * @param elements     ASN.1元素
 * @return 编码后的bytes
 */
public static byte[] encode(String asn1Encoding, ASN1Encodable... elements) {
    final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
    encodeTo(asn1Encoding, out, elements);
    return out.toByteArray();
}
Also used : 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