Flowable源码注释(九)流来源类

本文介绍了Flowable流程引擎中的几种不同的流来源类,包括InputStreamSource、BytesStreamSource、StringStreamSource和UrlStreamSource等。这些类分别适用于从输入流、字节数组、字符串和URL获取数据的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flowable源码地址:https://github.com/flowable/flowable-engine

包路径 package org.flowable.common.engine.impl.util.io

InputStreamProvider 输入流提供类

package org.flowable.common.engine.api.io;

import java.io.InputStream;

/**
 * @author Joram Barrez
 */
public interface InputStreamProvider {

    /**
     * Creates a <b>NEW</b> {@link InputStream} to the provided resource.
     * 为提供的资源创建一个新的</b>{@link InputStream}。
     */
    InputStream getInputStream();

}

StreamSource 流来源类

package org.flowable.common.engine.impl.util.io;

import java.io.InputStream;

import org.flowable.common.engine.api.io.InputStreamProvider;

/**
 * @author Tom Baeyens
 * @author Joram Barrez
 */
public interface StreamSource extends InputStreamProvider {

    /**
     * Creates a <b>NEW</b> {@link InputStream} to the provided resource.
     * 为提供的资源创建一个新的</b>{@link InputStream}。
     */
    @Override
    InputStream getInputStream();

}

BytesStreamSource 字节流来源类

package org.flowable.common.engine.impl.util.io;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @author Joram Barrez
 */
public class BytesStreamSource implements StreamSource {

    protected byte[] bytes;

    public BytesStreamSource(byte[] bytes) {
        this.bytes = bytes;
    }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(bytes);
    }

}

InputStreamSource 输入流来源来

package org.flowable.common.engine.impl.util.io;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.flowable.common.engine.api.FlowableException;

/**
 * @author Tom Baeyens
 * @author Joram Barrez
 */
public class InputStreamSource implements StreamSource {

    // This class is used for bpmn parsing.
    // The bpmn parsers needs to go over the stream at least twice:
    // Once for the schema validation and once for the parsing itself.
    // So we keep the content of the inputstream in memory so we can
    // re-read it.
    
    // 此类用于bpmn解析。
    // bpmn解析器需要至少对流进行两次检查:
    // 一次用于模式验证,一次用于解析本身。
	// 因此,我们将输入流的内容保存在内存中,以便重读一遍
    
    protected BufferedInputStream inputStream;
    protected byte[] bytes;

    public InputStreamSource(InputStream inputStream) {
        this.inputStream = new BufferedInputStream(inputStream);
    }

    @Override
    public InputStream getInputStream() {
        if (bytes == null) {
            try {
                bytes = getBytesFromInputStream(inputStream);
            } catch (IOException e) {
                throw new FlowableException("Could not read from inputstream", e);
            }
        }
        return new BufferedInputStream(new ByteArrayInputStream(bytes));
    }

    @Override
    public String toString() {
        return "InputStream";
    }

    public byte[] getBytesFromInputStream(InputStream inStream) throws IOException {
        long length = inStream.available();
        byte[] bytes = new byte[(int) length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = inStream.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        if (offset < bytes.length) {
            throw new FlowableException("Could not completely read inputstream ");
        }

        // Close the input stream and return bytes
        // 关闭输入流并返回字节
        inStream.close();
        return bytes;
    }

}

StringStreamSource 字符串流来源类

package org.flowable.common.engine.impl.util.io;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.flowable.common.engine.api.FlowableException;

/**
 * @author Tom Baeyens
 */
public class StringStreamSource implements StreamSource {

    String string;
    String byteArrayEncoding = "utf-8";

    public StringStreamSource(String string) {
        this.string = string;
    }

    public StringStreamSource(String string, String byteArrayEncoding) {
        this.string = string;
        this.byteArrayEncoding = byteArrayEncoding;
    }

    @Override
    public InputStream getInputStream() {
        try {
            return new ByteArrayInputStream(byteArrayEncoding == null ? string.getBytes() : string.getBytes(byteArrayEncoding));
        } catch (UnsupportedEncodingException e) {
            throw new FlowableException("Unsupported encoding for string", e);
        }
    }

    @Override
    public String toString() {
        return "String";
    }
}

UrlStreamSource 网址流来源类

package org.flowable.common.engine.impl.util.io;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.flowable.common.engine.api.FlowableIllegalArgumentException;

/**
 * @author Tom Baeyens
 */
public class UrlStreamSource implements StreamSource {

    URL url;

    public UrlStreamSource(URL url) {
        this.url = url;
    }

    @Override
    public InputStream getInputStream() {
        try {
            return new BufferedInputStream(url.openStream());
        } catch (IOException e) {
            throw new FlowableIllegalArgumentException("couldn't open url '" + url + "'", e);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值