davidmoten / subethasmtp

SubEtha SMTP is a Java library for receiving SMTP mail
Other
149 stars 40 forks source link

Encapsulate Socket.InputStream on Session #112

Open valenpo opened 1 year ago

valenpo commented 1 year ago

Incapsulate Session.getRawInput(), so returned stream couldn't be closed outside. This solve few problems:

  1. Possible to use InputStream in try block in DataInputStream etc...
  2. All Decorators (BdatInputStream, DotTerminatedInputStream etc...) of InputStream also could be closed, prevent stream leaks inside decorators

Example of code that will be fixed

private static byte[] readAndClose(InputStream is, int maxMessageSize)
                throws IOException, TooMuchDataException {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int n;
            try {
                while ((n = is.read(buffer)) != -1) {
                    bytes.write(buffer, 0, n);
                    if (maxMessageSize > 0 && bytes.size() > maxMessageSize) {
                        throw new TooMuchDataException("message size exceeded maximum of " + maxMessageSize + "bytes");
                    }
                }
            } finally {
                // TODO creator of stream should close it, not this method
                is.close();
            }
            return bytes.toByteArray();
        }
valenpo commented 11 months ago

@davidmoten Hello, any update?

davidmoten commented 11 months ago

I'll answer on ytour related PR #113.