Merge pull request #4143 from CherryRum/v5-dev

修改部分描述
This commit is contained in:
Golden Looly
2025-11-23 23:11:00 +08:00
committed by GitHub
2 changed files with 27 additions and 7 deletions

View File

@@ -4,15 +4,35 @@ 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;
/** 备选的本地端口 */ /** 备选的本地端口 */

View File

@@ -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;