Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved code smell #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

EzxD
Copy link

@EzxD EzxD commented Nov 14, 2021

Reason for the change
Code smell is important for better readability

Description
I replaced stuff which can be expressed with isEmpty(). And a try with resource for closing the socket if exception gets thrown

Code examples
I replaced raw.size() == 0 with raw.isEmpty()
more important is the try with resource of the socket.
old code:

            SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(
                        socket,
                        socket.getInetAddress().getHostAddress(),
                        socket.getPort(),
                        true);

                    // replace input/output streams
                    inputStream = new DataInputStream(sslSocket.getInputStream());
                    outputStream = sslSocket.getOutputStream();

                    // execute SSL handshake
                    sslSocket.startHandshake();

new code:

   try (
                      SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory()
                        .createSocket(
                          socket,
                          socket.getInetAddress().getHostAddress(),
                          socket.getPort(),
                          true
                        )
                    ) {

                        // replace input/output streams
                        inputStream = new DataInputStream(sslSocket.getInputStream());
                        outputStream = sslSocket.getOutputStream();

                        // execute SSL handshake
                        sslSocket.startHandshake();
                    }

Checklist

References
Anything else related to the change e.g. documentations, RFCs, etc.

socket.getInetAddress().getHostAddress(),
socket.getPort(),
true);
try (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the existing code just creates an SSLSocket and then lets it get GC'd without closing it, assigning inputStream and outputStream instead. I guess the underlying socket does get closed later.

Right now I think we don't want this try block which closes the sslSocket at the end of the block. Why would we close() the sslSocket after constructing streams from it? Wouldn't those streams stop working?

I think we'd want sslSocket to be assigned to a member variable such that its lifetime is held alive as long as the streams exist.

I'll need to investigate what's going on a little bit further before merging this PR. So right now I'm leaving this open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants