将Character添加到BufferedInputStream java的末尾

时间:2015-01-20 10:05:28

标签: java javamail bufferedinputstream

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

表示消息的结尾。

请建议。

2 个答案:

答案 0 :(得分: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);
   }
}

在你的情况下你可以做到

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

答案 1 :(得分:1)

只需将InputStraem值存储在String中,然后将其添加到该String:

    BufferedReader input;

    if(stream != null){
        input = new BufferedReader(new InputStreamReader(
                stream));
    String responseLine = "";
    String server_response = "";
    try {
        while (((responseLine = input.readLine()) != null) {
             server_response = server_response + responseLine  + "\r\n";
        }
    } catch (IOException e) {

    }
    server_response = server_response + "\r\n.\r\n";
  }
我错过了什么吗?或者你要的是什么?