Skip to content

Encapsulate Socket.InputStream on Session #112

@valenpo

Description

@valenpo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions