mirror of
https://gitee.com/dromara/hutool.git
synced 2025-11-24 08:33:22 +08:00
@@ -4,14 +4,34 @@ import java.io.Serializable;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地端口生成器<br>
|
* 本地端口生成器(LocalPortGenerator)。
|
||||||
* 用于生成本地可用(未被占用)的端口号<br>
|
* <p>
|
||||||
* 注意:多线程甚至单线程访问时可能会返回同一端口(例如获取了端口但是没有使用)
|
* 当前类名中“Generater”为拼写错误(正确应为 Generator),为保持兼容性暂未更改。
|
||||||
|
* 该问题将在后续大版本中以重命名方式修复,并保留旧类名的弃用(@Deprecated)兼容层。
|
||||||
|
* <p>
|
||||||
*
|
*
|
||||||
|
* 用于从指定起点开始递增探测一个当前“可用”的本地端口。探测通过短暂绑定
|
||||||
|
* {@link java.net.ServerSocket}(以及可选 UDP DatagramSocket)完成,但不会真正占用端口。
|
||||||
|
* <p>注意:</p>
|
||||||
|
* <ul>
|
||||||
|
* <li>该方法执行的是端口“探测”,非“分配”,返回端口不保证实际使用时仍然可用。</li>
|
||||||
|
* <li>存在 TOCTOU(检测到使用之间)竞态,多线程下可能返回同一端口。</li>
|
||||||
|
* <li>UDP 探测可能导致误判(TCP 可用但 UDP 被占用)。</li>
|
||||||
|
* <li>不适合作为生产级端口分配策略,推荐使用 {@code new ServerSocket(0)}。</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>未来版本计划:</p>
|
||||||
|
* <ul>
|
||||||
|
* <li>修复类名拼写问题:将“Generater”更名为 “Generator”。</li>
|
||||||
|
* <li>提供真正可靠的端口获取实现(绑定即占用,避免竞态)。</li>
|
||||||
|
* <li>优化探测策略,减少不必要的 UDP 检测。</li>
|
||||||
|
* <li>提供更安全的随机端口生成 API。</li>
|
||||||
|
* </ul>
|
||||||
* @author looly
|
* @author looly
|
||||||
* @since 4.0.3
|
* @since 4.0.3
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public class LocalPortGenerater implements Serializable{
|
public class LocalPortGenerater implements Serializable{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ public class URLUtil extends URLEncodeUtil {
|
|||||||
try {
|
try {
|
||||||
return file.toURI().toURL();
|
return file.toURI().toURL();
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
throw new UtilException(e, "Error occured when get URL!");
|
throw new UtilException(e, "Error occurred when get URL!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +288,7 @@ public class URLUtil extends URLEncodeUtil {
|
|||||||
urls[i] = files[i].toURI().toURL();
|
urls[i] = files[i].toURI().toURL();
|
||||||
}
|
}
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
throw new UtilException(e, "Error occured when get URL!");
|
throw new UtilException(e, "Error occurred when get URL!");
|
||||||
}
|
}
|
||||||
|
|
||||||
return urls;
|
return urls;
|
||||||
|
|||||||
Reference in New Issue
Block a user