gpt4 book ai didi

java - 将字符添加到 BufferedInputStream java 的末尾

转载 作者:行者123 更新时间:2023-12-01 07:57:38 24 4
gpt4 key购买 nike

我正在从 MimeMessage 获取输入流。在InputStream中,最后我想添加\r\n。\r\n

代表消息的结束。

请提出建议。

最佳答案

您可以使用以下命令动态附加它

public class ConcatInputStream extends InputStream {
private final InputStream[] is;
private int last = 0;

ConcatInputStream(InputStream[] is) {
this.is = is;
}

public static InputStream of(InputStream... is) {
return new ConcatInputStream(is);
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
for (; last < is.length; last++) {
int read = is[last].read(b, off, len);
if (read >= 0)
return read;
}
throw new EOFException();
}

@Override
public int read() throws IOException {
for (; last < is.length; last++) {
int read = is[last].read();
if (read >= 0)
return read;
}
throw new EOFException();
}

@Override
public int available() throws IOException {
long available = 0;
for(int i = last; i < is.length; i++)
available += is[i].available();
return (int) Math.min(Integer.MAX_VALUE, available);
}
}

根据你的情况,你可以这样做

InputStream in = 
InputStream in2 = ConcatInputStream.of(in,
new ByteArrayInputStream("\r\n.\r\n".getBytes()));

关于java - 将字符添加到 BufferedInputStream java 的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com