StrUtil增加truncateByByteLength方法

This commit is contained in:
Looly 2023-06-30 10:34:16 +08:00
parent a51d0e6bd7
commit 7e04a657a1
3 changed files with 10 additions and 39 deletions

View File

@ -10,6 +10,7 @@
* 【core 】 Ipv4Util 新增方法:检测指定 IP 地址是否匹配通配符pr#3171@Github * 【core 】 Ipv4Util 新增方法:检测指定 IP 地址是否匹配通配符pr#3171@Github
* 【core 】 DateUtil.parse适配6位毫秒格式issue#I7H34N@Gitee * 【core 】 DateUtil.parse适配6位毫秒格式issue#I7H34N@Gitee
* 【core 】 RandomUtil增加可选是否包含边界的重载issue#3182@Github * 【core 】 RandomUtil增加可选是否包含边界的重载issue#3182@Github
* 【core 】 StrUtil增加truncateByByteLength方法pr#3176@Github
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题issue#3162@Github * 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题issue#3162@Github

View File

@ -487,20 +487,6 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
return truncateByByteLength(str, charset, maxBytes, 4, true); return truncateByByteLength(str, charset, maxBytes, 4, true);
} }
/**
* 截断字符串使用其按照GB18030编码为字节后不超过maxBytes长度截断后自动追加省略号(...)
* 用于存储数据库varchar且编码为GB2312GBKGB18030的字段
*
* @param str
* @param maxBytes
* @return
*/
public static String truncateGb18030(String str, int maxBytes) {
Charset charset = Charset.forName("GB18030");
//GB18030编码单个字符最大长度2
return truncateByByteLength(str, charset, maxBytes, 2, true);
}
/** /**
* 截断字符串使用其按照指定编码为字节后不超过maxBytes长度 * 截断字符串使用其按照指定编码为字节后不超过maxBytes长度
* *
@ -517,25 +503,25 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
if (str == null || str.length() * factor <= maxBytes) { if (str == null || str.length() * factor <= maxBytes) {
return str; return str;
} }
byte[] sba = str.getBytes(charset); final byte[] sba = str.getBytes(charset);
if (sba.length <= maxBytes) { if (sba.length <= maxBytes) {
return str; return str;
} }
//限制字节数 //限制字节数
int limitBytes; final int limitBytes;
if (appendDots) { if (appendDots) {
limitBytes = maxBytes - "...".getBytes(charset).length; limitBytes = maxBytes - "...".getBytes(charset).length;
} else { } else {
limitBytes = maxBytes; limitBytes = maxBytes;
} }
ByteBuffer bb = ByteBuffer.wrap(sba, 0, limitBytes); final ByteBuffer bb = ByteBuffer.wrap(sba, 0, limitBytes);
CharBuffer cb = CharBuffer.allocate(limitBytes); final CharBuffer cb = CharBuffer.allocate(limitBytes);
CharsetDecoder decoder = charset.newDecoder(); final CharsetDecoder decoder = charset.newDecoder();
//忽略被截断的字符 //忽略被截断的字符
decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.decode(bb, cb, true); decoder.decode(bb, cb, true);
decoder.flush(cb); decoder.flush(cb);
String result = new String(cb.array(), 0, cb.position()); final String result = new String(cb.array(), 0, cb.position());
if (appendDots) { if (appendDots) {
return result + "..."; return result + "...";
} }

View File

@ -652,7 +652,7 @@ public class StrUtilTest {
@Test @Test
public void truncateUtf8Test() { public void truncateUtf8Test() {
String str = "这是This一段中英文"; final String str = "这是This一段中英文";
String ret = StrUtil.truncateUtf8(str, 12); String ret = StrUtil.truncateUtf8(str, 12);
Assert.assertEquals("这是Thi...", ret); Assert.assertEquals("这是Thi...", ret);
@ -666,26 +666,10 @@ public class StrUtilTest {
Assert.assertEquals(str, ret); Assert.assertEquals(str, ret);
} }
@Test
public void truncateGb18030Test() {
String str = "这是This一段中英文";
String ret = StrUtil.truncateGb18030(str, 12);
Assert.assertEquals("这是This...", ret);
ret = StrUtil.truncateGb18030(str, 13);
Assert.assertEquals("这是This一...", ret);
ret = StrUtil.truncateGb18030(str, 14);
Assert.assertEquals("这是This一...", ret);
ret = StrUtil.truncateGb18030(str, 999);
Assert.assertEquals(str, ret);
}
@Test @Test
public void truncateByByteLengthTest() { public void truncateByByteLengthTest() {
String str = "This is English"; final String str = "This is English";
String ret = StrUtil.truncateByByteLength(str, StandardCharsets.ISO_8859_1,10, 1, false); final String ret = StrUtil.truncateByByteLength(str, StandardCharsets.ISO_8859_1,10, 1, false);
Assert.assertEquals("This is En", ret); Assert.assertEquals("This is En", ret);
} }
} }