|
9 | 9 | import javax.annotation.Nullable; |
10 | 10 | import java.io.EOFException; |
11 | 11 | import java.io.IOException; |
| 12 | +import java.net.BindException; |
12 | 13 | import java.nio.channels.ClosedChannelException; |
13 | 14 | import java.util.List; |
| 15 | +import java.util.Optional; |
14 | 16 | import java.util.concurrent.Executor; |
15 | 17 | import java.util.concurrent.atomic.AtomicBoolean; |
16 | 18 |
|
@@ -134,9 +136,27 @@ static boolean connectionLost(@Nullable Throwable cause) { |
134 | 136 | String message = cause.getMessage(); |
135 | 137 | if (message != null) { |
136 | 138 | String msg = message.toLowerCase(); |
137 | | - return msg.contains("reset by peer") || msg.contains("broken pipe") || msg.contains("forcibly closed"); |
| 139 | + return msg.contains("reset by peer") || msg.contains("broken pipe") || msg |
| 140 | + .contains("forcibly closed"); |
138 | 141 | } |
139 | 142 | } |
140 | 143 | return (cause instanceof ClosedChannelException) || (cause instanceof EOFException); |
141 | 144 | } |
| 145 | + |
| 146 | + /** |
| 147 | + * Whenever the given exception is an address already in use. This probably won't work in none |
| 148 | + * English locale systems. |
| 149 | + * |
| 150 | + * @param cause Exception to check. |
| 151 | + * @return True address alaredy in use. |
| 152 | + */ |
| 153 | + static boolean isAddressInUse(@Nullable Throwable cause) { |
| 154 | + return (cause instanceof BindException) || |
| 155 | + (Optional.ofNullable(cause) |
| 156 | + .map(Throwable::getMessage) |
| 157 | + .map(String::toLowerCase) |
| 158 | + .filter(msg -> msg.contains("address already in use")) |
| 159 | + .isPresent() |
| 160 | + ); |
| 161 | + } |
142 | 162 | } |
0 commit comments