diff --git a/CHANGELOG.md b/CHANGELOG.md index 61e0d2f20..0d75ffb76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,14 +3,16 @@ ------------------------------------------------------------------------------------------------------------- -## 5.3.10 (2020-07-16) +## 5.3.10 (2020-07-17) ### 新特性 * 【db 】 增加DbUtil.setReturnGeneratedKeyGlobal(issue#I1NM0K@Gitee) * 【core 】 增加DataSize和DataSizeUtil(issue#967@Github) +* 【core 】 ImgUtil增加异常,避免空指针(issue#I1NKXG@Gitee) ### Bug修复 * 【core 】 修复ZipUtil中finish位于循环内的问题(issue#961@Github) +* 【core 】 修复CollUtil.page未越界检查的问题(issue#I1O2LR@Gitee) ------------------------------------------------------------------------------------------------------------- ## 5.3.9 (2020-07-12) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index babe5f555..a02638d7c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -235,10 +235,17 @@ public class ListUtil { return new ArrayList<>(0); } } + + if((pageNo * pageSize) > resultSize){ + // 越界直接返回空 + return new ArrayList<>(0); + } + final int[] startEnd = PageUtil.transToStartEnd(pageNo, pageSize); if (startEnd[1] > resultSize) { startEnd[1] = resultSize; } + return list.subList(startEnd[0], startEnd[1]); } diff --git a/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java b/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java index 8caa47738..165f5dea7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java @@ -1322,7 +1322,7 @@ public class ImgUtil { * @param font 字体{@link Font} * @param backgroundColor 背景颜色,默认透明 * @param fontColor 字体颜色,默认黑色 - * @param imageType 图片类型,见:{@link BufferedImage} + * @param imageType 图片类型,见:{@link BufferedImage} * @return 图片 * @throws IORuntimeException IO异常 */ @@ -1355,7 +1355,7 @@ public class ImgUtil { /** * 获取font的样式应用在str上的整个矩形 * - * @param str 字符串,必须非空 + * @param str 字符串,必须非空 * @param font 字体,必须非空 * @return {@link Rectangle2D} * @since 5.3.3 @@ -1607,11 +1607,18 @@ public class ImgUtil { * @since 3.2.2 */ public static BufferedImage read(File imageFile) { + BufferedImage result; try { - return ImageIO.read(imageFile); + result = ImageIO.read(imageFile); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type of file [" + imageFile.getName() + "] is not supported!"); + } + + return result; } /** @@ -1633,11 +1640,18 @@ public class ImgUtil { * @since 3.2.2 */ public static BufferedImage read(InputStream imageStream) { + BufferedImage result; try { - return ImageIO.read(imageStream); + result = ImageIO.read(imageStream); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type is not supported!"); + } + + return result; } /** @@ -1648,11 +1662,18 @@ public class ImgUtil { * @since 3.2.2 */ public static BufferedImage read(ImageInputStream imageStream) { + BufferedImage result; try { - return ImageIO.read(imageStream); + result = ImageIO.read(imageStream); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type is not supported!"); + } + + return result; } /** @@ -1663,11 +1684,18 @@ public class ImgUtil { * @since 3.2.2 */ public static BufferedImage read(URL imageUrl) { + BufferedImage result; try { - return ImageIO.read(imageUrl); + result = ImageIO.read(imageUrl); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type of [" + imageUrl.toString() + "] is not supported!"); + } + + return result; } /** @@ -1679,11 +1707,18 @@ public class ImgUtil { * @since 3.1.2 */ public static ImageOutputStream getImageOutputStream(OutputStream out) throws IORuntimeException { + ImageOutputStream result; try { - return ImageIO.createImageOutputStream(out); + result = ImageIO.createImageOutputStream(out); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type is not supported!"); + } + + return result; } /** @@ -1695,11 +1730,18 @@ public class ImgUtil { * @since 3.2.2 */ public static ImageOutputStream getImageOutputStream(File outFile) throws IORuntimeException { + ImageOutputStream result; try { - return ImageIO.createImageOutputStream(outFile); + result = ImageIO.createImageOutputStream(outFile); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type of file [" + outFile.getName() + "] is not supported!"); + } + + return result; } /** @@ -1711,11 +1753,18 @@ public class ImgUtil { * @since 3.1.2 */ public static ImageInputStream getImageInputStream(InputStream in) throws IORuntimeException { + ImageOutputStream result; try { - return ImageIO.createImageInputStream(in); + result = ImageIO.createImageOutputStream(in); } catch (IOException e) { throw new IORuntimeException(e); } + + if (null == result) { + throw new IllegalArgumentException("Image type is not supported!"); + } + + return result; } /** @@ -1778,7 +1827,7 @@ public class ImgUtil { */ public static String toHex(int r, int g, int b) { // rgb 小于 255 - if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255){ + if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) { throw new IllegalArgumentException("RGB must be 0~255!"); } return String.format("#%02X%02X%02X", r, g, b); @@ -1908,9 +1957,9 @@ public class ImgUtil { /** * 获得修正后的矩形坐标位置,变为以背景中心为基准坐标(即x,y == 0,0时,处于背景正中) * - * @param rectangle 矩形 + * @param rectangle 矩形 * @param backgroundWidth 参考宽(背景宽) - * @param backgroundHeight 参考高(背景高) + * @param backgroundHeight 参考高(背景高) * @return 修正后的{@link Point} * @since 5.3.6 */ diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index b14391d5c..e4f060628 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -650,4 +650,14 @@ public class CollUtilTest { Assert.assertEquals(Integer.valueOf(2), countMap.get("c")); Assert.assertEquals(Integer.valueOf(1), countMap.get("d")); } + + @Test + public void pageTest(){ + List objects = CollUtil.newArrayList(); + for (int i = 0; i < 10; i++) { + objects.add(Dict.create().set("name", "姓名:" + i)); + } + + Assert.assertEquals(0, CollUtil.page(3, 5, objects).size()); + } } diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml index 0e58c2692..0808ef333 100644 --- a/hutool-script/pom.xml +++ b/hutool-script/pom.xml @@ -18,7 +18,7 @@ 2.7.2 3.0.1 - 3.0.4 + 3.0.2