-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly closing channel and socket when LocalPortForwarder fails to…
… open it. (Fix #175)
- Loading branch information
1 parent
1e061ae
commit e6c7c17
Showing
3 changed files
with
44 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.hierynomus.sshj.socket; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.net.Socket; | ||
|
||
public class Sockets { | ||
|
||
/** | ||
* Java 7 and up have Socket implemented as Closeable, whereas Java6 did not have this inheritance. | ||
* @param socket The socket to wrap as Closeable | ||
* @return | ||
*/ | ||
public static Closeable asCloseable(final Socket socket) { | ||
if (Closeable.class.isAssignableFrom(socket.getClass())) { | ||
return Closeable.class.cast(socket); | ||
} else { | ||
return new Closeable() { | ||
@Override | ||
public void close() throws IOException { | ||
socket.close(); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters