Search code examples
javajakarta-mailbufferedinputstream

Adding Character to the end of the BufferedInputStream java


I'm getting inputstream from MimeMessage. In the InputStream, at the end I want to add \r\n.\r\n

To represent the end of the message.

Please suggest.


Solution

  • You can append it on the fly with

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

    In your case you can do

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