Search in sources :

Example 6 with IORuntimeException

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

the class ImgUtil method write.

/**
 * 通过{@link ImageWriter}写出图片到输出流
 *
 * @param image   图片
 * @param writer  {@link ImageWriter}
 * @param output  输出的Image流{@link ImageOutputStream}
 * @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
 * @return 是否成功写出
 * @since 4.3.2
 */
public static boolean write(Image image, ImageWriter writer, ImageOutputStream output, float quality) {
    if (writer == null) {
        return false;
    }
    writer.setOutput(output);
    final RenderedImage renderedImage = toRenderedImage(image);
    // 设置质量
    ImageWriteParam imgWriteParams = null;
    if (quality > 0 && quality < 1) {
        imgWriteParams = writer.getDefaultWriteParam();
        if (imgWriteParams.canWriteCompressed()) {
            imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            imgWriteParams.setCompressionQuality(quality);
            // ColorModel.getRGBdefault();
            final ColorModel colorModel = renderedImage.getColorModel();
            imgWriteParams.setDestinationType(new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
        }
    }
    try {
        if (null != imgWriteParams) {
            writer.write(null, new IIOImage(renderedImage, null, null), imgWriteParams);
        } else {
            writer.write(renderedImage);
        }
        output.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        writer.dispose();
    }
    return true;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) ColorModel(java.awt.image.ColorModel) IOException(java.io.IOException) RenderedImage(java.awt.image.RenderedImage) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 7 with IORuntimeException

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

the class StrJoiner method append.

@Override
public StrJoiner append(CharSequence csq, int startInclude, int endExclude) {
    if (null == csq) {
        switch(this.nullMode) {
            case IGNORE:
                return this;
            case TO_EMPTY:
                csq = StrUtil.EMPTY;
                break;
            case NULL_STRING:
                csq = StrUtil.NULL;
                endExclude = StrUtil.NULL.length();
                break;
        }
    }
    try {
        final Appendable appendable = prepare();
        if (wrapElement && StrUtil.isNotEmpty(this.prefix)) {
            appendable.append(prefix);
        }
        appendable.append(csq, startInclude, endExclude);
        if (wrapElement && StrUtil.isNotEmpty(this.suffix)) {
            appendable.append(suffix);
        }
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 8 with IORuntimeException

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

the class SmUtil method rsPlainToAsn1.

/**
 * BC的SM3withSM2验签需要的rs是asn1格式的,这个方法将直接拼接r||s的字节数组转化成asn1格式
 *
 * @param sign in plain byte array
 * @return rs result in asn1 format
 * @since 4.5.0
 */
public static byte[] rsPlainToAsn1(byte[] sign) {
    if (sign.length != RS_LEN * 2) {
        throw new CryptoException("err rs. ");
    }
    BigInteger r = new BigInteger(1, Arrays.copyOfRange(sign, 0, RS_LEN));
    BigInteger s = new BigInteger(1, Arrays.copyOfRange(sign, RS_LEN, RS_LEN * 2));
    try {
        return StandardDSAEncoding.INSTANCE.encode(SM2_DOMAIN_PARAMS.getN(), r, s);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Example 9 with IORuntimeException

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

the class SmUtil method rsAsn1ToPlain.

/**
 * BC的SM3withSM2签名得到的结果的rs是asn1格式的,这个方法转化成直接拼接r||s
 *
 * @param rsDer rs in asn1 format
 * @return sign result in plain byte array
 * @since 4.5.0
 */
public static byte[] rsAsn1ToPlain(byte[] rsDer) {
    final BigInteger[] decode;
    try {
        decode = StandardDSAEncoding.INSTANCE.decode(SM2_DOMAIN_PARAMS.getN(), rsDer);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    final byte[] r = bigIntToFixedLengthBytes(decode[0]);
    final byte[] s = bigIntToFixedLengthBytes(decode[1]);
    return ArrayUtil.addAll(r, s);
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Example 10 with IORuntimeException

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

the class GlobalCookieManager method add.

/**
 * 将本地存储的Cookie信息附带到Http请求中,不覆盖用户定义好的Cookie
 *
 * @param conn {@link HttpConnection}
 */
public static void add(HttpConnection conn) {
    if (null == cookieManager) {
        // 全局Cookie管理器关闭
        return;
    }
    Map<String, List<String>> cookieHeader;
    try {
        cookieHeader = cookieManager.get(getURI(conn), new HashMap<>(0));
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    // 不覆盖模式回填Cookie头,这样用户定义的Cookie将优先
    conn.header(cookieHeader, false);
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) HashMap(java.util.HashMap) List(java.util.List) IOException(java.io.IOException)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)81 IOException (java.io.IOException)75 File (java.io.File)9 StreamProgress (cn.hutool.core.io.StreamProgress)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 UtilException (cn.hutool.core.exceptions.UtilException)3 POIException (cn.hutool.poi.exceptions.POIException)3 Rectangle (java.awt.Rectangle)3 BufferedImage (java.awt.image.BufferedImage)3 BufferedReader (java.io.BufferedReader)3 SocketChannel (java.nio.channels.SocketChannel)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 StreamGobbler (ch.ethz.ssh2.StreamGobbler)2 CryptoException (cn.hutool.crypto.CryptoException)2 RenderedImage (java.awt.image.RenderedImage)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2