将Character添加到BufferedInputStream java的末尾

Ros*_*han 0 java jakarta-mail bufferedinputstream

我从MimeMessage获得输入流.在InputStream中,最后我要添加\ r \n.\ r \n

表示消息的结尾.

请建议.

Pet*_*rey 5

你可以随意附加它

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

在你的情况下,你可以做到

InputStream in = 
InputStream in2 = ConcatInputStream.of(in, 
                              new ByteArrayInputStream("\r\n.\r\n".getBytes()));
Run Code Online (Sandbox Code Playgroud)