在ITextRenderer 将html文件生成pdf时候使用ByteArrayOutputStream对数据的不落地使用

本文介绍了如何利用ITextRenderer将HTML文件转换为PDF,同时通过ByteArrayOutputStream实现数据的不落地处理。在转换过程中,字节数组缓冲区会自动扩展,关闭流操作不会影响其功能,确保了高效且安全的转换方法。
摘要由CSDN通过智能技术生成

1. 定义:
此类实现一个字节输出流、其中数据被写入到字节数组中, 缓冲区在数据写入时会自动增长,关闭该流无效,关闭此流后调用方法不会有异常
2. 字段

	/**
     * The buffer where data is stored.
     * 存储数据的缓冲区
     */
    protected byte buf[];

    /**
     * The number of valid bytes in the buffer.
     * 缓存区的有效字节数
     */
    protected int count;

3. 构造函数:

	/**
     * Creates a new byte array output stream. The buffer capacity is
     * initially 32 bytes, though its size increases if necessary.
     * 创建缓冲区字节数组流,默认大小为32字节
     */
    public ByteArrayOutputStream() {
        this(32);
    }

    /**
     * Creates a new byte array output stream, with a buffer capacity of
     * the specified size, in bytes.
     * 创建一个自定义大小的字节缓冲区数据流
     *
     * @param   size   the initial size.
     * @exception  IllegalArgumentException if size is negative.
     */
    public ByteArrayOutputStream(int size) {
        if (size < 0) {
            throw new IllegalArgumentException("Negative initial size: "
                                               + size);
        }
        buf = new byte[size];
    }

4. 具体方法

	/**
     * Increases the capacity if necessary to ensure that it can hold
     * at least the number of elements specified by the minimum
     * capacity argument.
     * 必要时增加容量以确保它至少可以容纳由minimum capacity参数指定的元素数。
     */
    private void ensureCapacity(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - buf.length > 0)
            grow(minCapacity);
    }
	private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = buf.length;
        int newCapacity &#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值