mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-30 04:45:55 +08:00
fix code
This commit is contained in:
parent
bb1d65ff97
commit
6c57694599
@ -1141,17 +1141,18 @@ public enum FileMagicNumber {
|
||||
@Override
|
||||
public boolean match(final byte[] bytes) {
|
||||
final byte[] byte1 = new byte[]{(byte) 0xd0, (byte) 0xcf, (byte) 0x11, (byte) 0xe0, (byte) 0xa1, (byte) 0xb1, (byte) 0x1a, (byte) 0xe1};
|
||||
final boolean flag1 = bytes.length > 515 && Arrays.equals(Arrays.copyOfRange(bytes, 0, 8), byte1);
|
||||
if (flag1) {
|
||||
if (bytes.length > 515 && ArrayUtil.isSubEquals(bytes, 0, byte1)) {
|
||||
final byte[] byte2 = new byte[]{(byte) 0xec, (byte) 0xa5, (byte) 0xc1, (byte) 0x00};
|
||||
final boolean flag2 = Arrays.equals(Arrays.copyOfRange(bytes, 512, 516), byte2);
|
||||
// check 512:516
|
||||
if(ArrayUtil.isSubEquals(bytes, 512, byte2)){
|
||||
return true;
|
||||
}
|
||||
final byte[] byte3 = new byte[]{(byte) 0x00, (byte) 0x0a, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x4d, (byte) 0x53, (byte) 0x57, (byte) 0x6f, (byte) 0x72, (byte) 0x64
|
||||
, (byte) 0x44, (byte) 0x6f, (byte) 0x63, (byte) 0x00, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x57, (byte) 0x6f, (byte) 0x72, (byte) 0x64,
|
||||
(byte) 0x2e, (byte) 0x44, (byte) 0x6f, (byte) 0x63, (byte) 0x75, (byte) 0x6d, (byte) 0x65, (byte) 0x6e, (byte) 0x74, (byte) 0x2e, (byte) 0x38, (byte) 0x00,
|
||||
(byte) 0xf4, (byte) 0x39, (byte) 0xb2, (byte) 0x71};
|
||||
final byte[] range = Arrays.copyOfRange(bytes, 2075, 2142);
|
||||
final boolean flag3 = bytes.length > 2142 && FileMagicNumber.indexOf(range, byte3) > 0;
|
||||
return flag2 || flag3;
|
||||
return bytes.length > 2142 && FileMagicNumber.indexOf(range, byte3) > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -1023,7 +1023,7 @@ public class CharSequenceUtil extends StrChecker {
|
||||
if (null == str || ArrayUtil.isEmpty(chars)) {
|
||||
return str(str);
|
||||
}
|
||||
return filter(str, (c)-> false == ArrayUtil.contains(chars, c));
|
||||
return filter(str, (c) -> false == ArrayUtil.contains(chars, c));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2052,16 +2052,16 @@ public class CharSequenceUtil extends StrChecker {
|
||||
* 任意一个字符串为null返回false
|
||||
*
|
||||
* @param str1 第一个字符串
|
||||
* @param start1 第一个字符串开始的位置
|
||||
* @param offset1 第一个字符串开始的位置
|
||||
* @param str2 第二个字符串
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
* @return 子串是否相同
|
||||
* @see String#regionMatches(boolean, int, String, int, int)
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static boolean isSubEquals(final CharSequence str1, final int start1,
|
||||
public static boolean isSubEquals(final CharSequence str1, final int offset1,
|
||||
final CharSequence str2, final boolean ignoreCase) {
|
||||
return isSubEquals(str1, start1, str2, 0, str2.length(), ignoreCase);
|
||||
return isSubEquals(str1, offset1, str2, 0, str2.length(), ignoreCase);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2069,23 +2069,23 @@ public class CharSequenceUtil extends StrChecker {
|
||||
* 任意一个字符串为null返回false
|
||||
*
|
||||
* @param str1 第一个字符串
|
||||
* @param start1 第一个字符串开始的位置
|
||||
* @param offset1 第一个字符串开始的位置
|
||||
* @param str2 第二个字符串
|
||||
* @param start2 第二个字符串开始的位置
|
||||
* @param offset2 第二个字符串开始的位置
|
||||
* @param length 截取长度
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
* @return 子串是否相同
|
||||
* @see String#regionMatches(boolean, int, String, int, int)
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static boolean isSubEquals(final CharSequence str1, final int start1,
|
||||
final CharSequence str2, final int start2, final int length,
|
||||
public static boolean isSubEquals(final CharSequence str1, final int offset1,
|
||||
final CharSequence str2, final int offset2, final int length,
|
||||
final boolean ignoreCase) {
|
||||
if (null == str1 || null == str2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str1.toString().regionMatches(ignoreCase, start1, str2.toString(), start2, length);
|
||||
return str1.toString().regionMatches(ignoreCase, offset1, str2.toString(), offset2, length);
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -31,6 +31,7 @@ public class PrimitiveArrayUtil {
|
||||
public static final int INDEX_NOT_FOUND = -1;
|
||||
|
||||
// region ----- isEmpty
|
||||
|
||||
/**
|
||||
* 数组是否为空
|
||||
*
|
||||
@ -3139,6 +3140,7 @@ public class PrimitiveArrayUtil {
|
||||
// endregion
|
||||
|
||||
// region ----- startWith
|
||||
|
||||
/**
|
||||
* array数组是否以prefix开头
|
||||
* <ul>
|
||||
@ -3155,7 +3157,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3186,7 +3188,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3217,7 +3219,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3248,7 +3250,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3279,7 +3281,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3310,7 +3312,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3341,7 +3343,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3372,7 +3374,7 @@ public class PrimitiveArrayUtil {
|
||||
if (array == prefix) {
|
||||
return true;
|
||||
}
|
||||
if(isEmpty(array)){
|
||||
if (isEmpty(array)) {
|
||||
return isEmpty(prefix);
|
||||
}
|
||||
if (prefix.length > array.length) {
|
||||
@ -3389,7 +3391,53 @@ public class PrimitiveArrayUtil {
|
||||
// endregion
|
||||
|
||||
// region rangeMatches
|
||||
public static boolean rangeMatches(final byte[] bytes1){
|
||||
return false;
|
||||
|
||||
/**
|
||||
* 是否局部匹配,相当于对比以下子串是否相等
|
||||
* <pre>
|
||||
* byte1[offset1, byte2.length]
|
||||
* ||
|
||||
* byte2
|
||||
* </pre>
|
||||
*
|
||||
* @param bytes1 第一个数组
|
||||
* @param offset 开始位置
|
||||
* @param bytes2 第二个数组
|
||||
* @return 是否局部匹配
|
||||
*/
|
||||
public static boolean isSubEquals(final byte[] bytes1, final int offset, final byte[] bytes2) {
|
||||
return regionMatches(bytes1, offset, bytes2, 0, bytes2.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否局部匹配,相当于对比以下子串是否相等
|
||||
* <pre>
|
||||
* byte1[offset1, offset1 + length]
|
||||
* ||
|
||||
* byte2[offset2, offset2 + length]
|
||||
* </pre>
|
||||
*
|
||||
* @param bytes1 第一个数组
|
||||
* @param offset1 第一个数组开始位置
|
||||
* @param bytes2 第二个数组
|
||||
* @param offset2 第二个数组开始位置
|
||||
* @param length 检查长度
|
||||
* @return 是否局部匹配
|
||||
*/
|
||||
public static boolean regionMatches(final byte[] bytes1, final int offset1,
|
||||
final byte[] bytes2, final int offset2, final int length) {
|
||||
if(bytes1.length < offset1 + length){
|
||||
throw new IndexOutOfBoundsException("[byte1] length must be >= [offset1 + length]");
|
||||
}
|
||||
if(bytes2.length < offset2 + length){
|
||||
throw new IndexOutOfBoundsException("[byte2] length must be >= [offset2 + length]");
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (bytes1[i + offset1] != bytes2[i + offset2]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import java.util.*;
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
@SuppressWarnings("ConstantValue")
|
||||
public class ArrayUtilTest {
|
||||
|
||||
@Test
|
||||
@ -728,10 +729,24 @@ public class ArrayUtilTest {
|
||||
|
||||
@Test
|
||||
public void copyOfRangeTest() {
|
||||
String a = "aIDAT";
|
||||
final String a = "aIDAT";
|
||||
final byte[] bytes1 = Arrays.copyOfRange(a.getBytes(CharsetUtil.UTF_8), 1, 1 + 4);
|
||||
|
||||
Assert.assertEquals(new String(bytes1),
|
||||
new String(a.getBytes(CharsetUtil.UTF_8), 1, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void regionMatchesTest() {
|
||||
final byte[] a = new byte[]{1, 2, 3, 4, 5};
|
||||
final byte[] b = new byte[]{2, 3, 4};
|
||||
|
||||
Assert.assertTrue(ArrayUtil.regionMatches(a, 1, b, 0, 1));
|
||||
Assert.assertTrue(ArrayUtil.regionMatches(a, 1, b, 0, 2));
|
||||
Assert.assertTrue(ArrayUtil.regionMatches(a, 1, b, 0, 3));
|
||||
Assert.assertTrue(ArrayUtil.isSubEquals(a, 1, b));
|
||||
|
||||
Assert.assertFalse(ArrayUtil.regionMatches(a, 2, b, 0, 2));
|
||||
Assert.assertFalse(ArrayUtil.regionMatches(a, 3, b, 0, 2));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user