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);
}
}
}