This commit is contained in:
Looly 2023-03-30 02:35:12 +08:00
parent e7aa83220c
commit c9024ba8f9
2 changed files with 466 additions and 469 deletions

View File

@ -3155,7 +3155,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final boolean[] array, final boolean[] prefix) {
public static boolean startWith(final boolean[] array, final boolean... prefix) {
if (array == prefix) {
return true;
}
@ -3186,7 +3186,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final byte[] array, final byte[] prefix) {
public static boolean startWith(final byte[] array, final byte... prefix) {
if (array == prefix) {
return true;
}
@ -3212,7 +3212,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final char[] array, final char[] prefix) {
public static boolean startWith(final char[] array, final char... prefix) {
if (array == prefix) {
return true;
}
@ -3243,7 +3243,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final double[] array, final double[] prefix) {
public static boolean startWith(final double[] array, final double... prefix) {
if (array == prefix) {
return true;
}
@ -3274,7 +3274,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final float[] array, final float[] prefix) {
public static boolean startWith(final float[] array, final float... prefix) {
if (array == prefix) {
return true;
}
@ -3305,7 +3305,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final int[] array, final int[] prefix) {
public static boolean startWith(final int[] array, final int... prefix) {
if (array == prefix) {
return true;
}
@ -3336,7 +3336,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final long[] array, final long[] prefix) {
public static boolean startWith(final long[] array, final long... prefix) {
if (array == prefix) {
return true;
}
@ -3367,7 +3367,7 @@ public class PrimitiveArrayUtil {
* @param prefix 前缀
* @return 是否开头
*/
public static boolean startWith(final short[] array, final short[] prefix) {
public static boolean startWith(final short[] array, final short... prefix) {
if (array == prefix) {
return true;
}
@ -3402,7 +3402,7 @@ public class PrimitiveArrayUtil {
* @param subArray 子数组
* @return 是否局部匹配
*/
public static boolean isSubEquals(final byte[] array, final int offset, final byte[] subArray) {
public static boolean isSubEquals(final byte[] array, final int offset, final byte... subArray) {
if (array == subArray) {
return true;
}

View File

@ -38,6 +38,9 @@ public enum FileMagicNumber {
// region ----- image
/**
* jpeg
* <pre>
* prefixFFD8FF
* </pre>
*/
JPEG("image/jpeg", "jpg") {
@Override
@ -50,6 +53,9 @@ public enum FileMagicNumber {
},
/**
* jxr
* <pre>
* prefix4949BC
* </pre>
*/
JXR("image/vnd.ms-photo", "jxr") {
@Override
@ -63,19 +69,17 @@ public enum FileMagicNumber {
},
/**
* apng
* <pre>
* prefix 8位89504E47 0D0A1A0A
* </pre>
*/
APNG("image/apng", "apng") {
@Override
public boolean match(final byte[] bytes) {
final boolean b = bytes.length > 8
&& Objects.equals(bytes[0], (byte) 0x89)
&& Objects.equals(bytes[1], (byte) 0x50)
&& Objects.equals(bytes[2], (byte) 0x4e)
&& Objects.equals(bytes[3], (byte) 0x47)
&& Objects.equals(bytes[4], (byte) 0x0d)
&& Objects.equals(bytes[5], (byte) 0x0a)
&& Objects.equals(bytes[6], (byte) 0x1a)
&& Objects.equals(bytes[7], (byte) 0x0a);
final boolean b = ArrayUtil.startWith(bytes,
(byte) 0x89, (byte) 0x50, (byte) 0x4e, (byte) 0x47,
(byte) 0x0d, (byte) 0x0a, (byte) 0x1a, (byte) 0x0a
);
if (b) {
int i = 8;
@ -101,130 +105,132 @@ public enum FileMagicNumber {
},
/**
* png
* <pre>
* prefix: 89504E47
* </pre>
*/
PNG("image/png", "png") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 3
&& Objects.equals(bytes[0], (byte) 0x89)
&& Objects.equals(bytes[1], (byte) 0x50)
&& Objects.equals(bytes[2], (byte) 0x4e)
&& Objects.equals(bytes[3], (byte) 0x47);
return ArrayUtil.startWith(bytes,
(byte) 0x89, (byte) 0x50, (byte) 0x4e, (byte) 0x47
);
}
},
/**
* gif
* <pre>
* prefix: 474946
* </pre>
*/
GIF("image/gif", "gif") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 2
&& Objects.equals(bytes[0], (byte) 0x47)
&& Objects.equals(bytes[1], (byte) 0x49)
&& Objects.equals(bytes[2], (byte) 0x46);
return ArrayUtil.startWith(bytes,
(byte) 0x47, (byte) 0x49, (byte) 0x46
);
}
},
/**
* bmp
* <pre>
* prefix: 424D
* </pre>
*/
BMP("image/bmp", "bmp") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 1
&& Objects.equals(bytes[0], (byte) 0x42)
&& Objects.equals(bytes[1], (byte) 0x4d);
return ArrayUtil.startWith(bytes,
(byte) 0x42, (byte) 0x4d
);
}
},
/**
* tiff
* <pre>
* prefix: 49492A00 or 4D4D002A
* </pre>
*/
TIFF("image/tiff", "tiff") {
@Override
public boolean match(final byte[] bytes) {
if (bytes.length < 4) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x49)
&& Objects.equals(bytes[1], (byte) 0x49)
&& Objects.equals(bytes[2], (byte) 0x2a)
&& Objects.equals(bytes[3], (byte) 0x00);
final boolean flag2 = (Objects.equals(bytes[0], (byte) 0x4d)
&& Objects.equals(bytes[1], (byte) 0x4d)
&& Objects.equals(bytes[2], (byte) 0x00)
&& Objects.equals(bytes[3], (byte) 0x2a));
return flag1 || flag2;
return ArrayUtil.startWith(bytes,
(byte) 0x49, (byte) 0x49, (byte) 0x2a, (byte) 0x00
) || ArrayUtil.startWith(bytes,
(byte) 0x4d, (byte) 0x4d, (byte) 0x00, (byte) 0x2a
);
}
},
/**
* dwg
* <pre>
* prefix: 41433130
* </pre>
*/
DWG("image/vnd.dwg", "dwg") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 10
&& Objects.equals(bytes[0], (byte) 0x41)
&& Objects.equals(bytes[1], (byte) 0x43)
&& Objects.equals(bytes[2], (byte) 0x31)
&& Objects.equals(bytes[3], (byte) 0x30);
return ArrayUtil.startWith(bytes,
(byte) 0x41, (byte) 0x43, (byte) 0x31, (byte) 0x30
);
}
},
/**
* webp
* <pre>
* [8:11]: 57454250
* </pre>
*/
WEBP("image/webp", "webp") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 11
&& Objects.equals(bytes[8], (byte) 0x57)
&& Objects.equals(bytes[9], (byte) 0x45)
&& Objects.equals(bytes[10], (byte) 0x42)
&& Objects.equals(bytes[11], (byte) 0x50);
return ArrayUtil.isSubEquals(bytes, 8,
(byte) 0x57, (byte) 0x45, (byte) 0x42, (byte) 0x50
);
}
},
/**
* psd
* <pre>
* prefix: 38425053
* </pre>
*/
PSD("image/vnd.adobe.photoshop", "psd") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 3
&& Objects.equals(bytes[0], (byte) 0x38)
&& Objects.equals(bytes[1], (byte) 0x42)
&& Objects.equals(bytes[2], (byte) 0x50)
&& Objects.equals(bytes[3], (byte) 0x53);
return ArrayUtil.startWith(bytes,
(byte) 0x38, (byte) 0x42, (byte) 0x50, (byte) 0x53
);
}
},
/**
* icon
* <pre>
* prefix: 00000100
* </pre>
*/
ICO("image/x-icon", "ico") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 3
&& Objects.equals(bytes[0], (byte) 0x00)
&& Objects.equals(bytes[1], (byte) 0x00)
&& Objects.equals(bytes[2], (byte) 0x01)
&& Objects.equals(bytes[3], (byte) 0x00);
return ArrayUtil.startWith(bytes,
(byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00
);
}
},
/**
* xcf
* <pre>
* prefix: 67696D70 20786366 2076
* </pre>
*/
XCF("image/x-xcf", "xcf") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 9
&& Objects.equals(bytes[0], (byte) 0x67)
&& Objects.equals(bytes[1], (byte) 0x69)
&& Objects.equals(bytes[2], (byte) 0x6d)
&& Objects.equals(bytes[3], (byte) 0x70)
&& Objects.equals(bytes[4], (byte) 0x20)
&& Objects.equals(bytes[5], (byte) 0x78)
&& Objects.equals(bytes[6], (byte) 0x63)
&& Objects.equals(bytes[7], (byte) 0x66)
&& Objects.equals(bytes[8], (byte) 0x20)
&& Objects.equals(bytes[9], (byte) 0x76);
return ArrayUtil.startWith(bytes,
(byte) 0x67, (byte) 0x69, (byte) 0x6d, (byte) 0x70,
(byte) 0x20, (byte) 0x78, (byte) 0x63, (byte) 0x66,
(byte) 0x20, (byte) 0x76
);
}
},
// endregion
@ -232,61 +238,67 @@ public enum FileMagicNumber {
// region ----- audio
/**
* wav
* <pre>
* prefix: 52494646
* [8:11]: 57415645
* </pre>
*/
WAV("audio/x-wav", "wav") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 11
&& Objects.equals(bytes[0], (byte) 0x52)
&& Objects.equals(bytes[1], (byte) 0x49)
&& Objects.equals(bytes[2], (byte) 0x46)
&& Objects.equals(bytes[3], (byte) 0x46)
&& Objects.equals(bytes[8], (byte) 0x57)
&& Objects.equals(bytes[9], (byte) 0x41)
&& Objects.equals(bytes[10], (byte) 0x56)
&& Objects.equals(bytes[11], (byte) 0x45);
return ArrayUtil.startWith(bytes,
(byte) 0x52, (byte) 0x49, (byte) 0x46, (byte) 0x46
) && ArrayUtil.isSubEquals(bytes, 8,
(byte) 0x57, (byte) 0x41, (byte) 0x56, (byte) 0x45
);
}
},
/**
* midi
* <pre>
* prefix: 4D546864
* </pre>
*/
MIDI("audio/midi", "midi") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 3
&& Objects.equals(bytes[0], (byte) 0x4d)
&& Objects.equals(bytes[1], (byte) 0x54)
&& Objects.equals(bytes[2], (byte) 0x68)
&& Objects.equals(bytes[3], (byte) 0x64);
return ArrayUtil.startWith(bytes,
(byte) 0x4d, (byte) 0x54, (byte) 0x68, (byte) 0x64
);
}
},
/**
* mpeg-mp3
* <pre>
* prefix: 494433 or FFFB or FFF3 or FFF2
* </pre>
*/
MP3("audio/mpeg", "mp3") {
@Override
public boolean match(final byte[] bytes) {
if (bytes.length < 2) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x49) && Objects.equals(bytes[1], (byte) 0x44) && Objects.equals(bytes[2], (byte) 0x33);
final boolean flag2 = Objects.equals(bytes[0], (byte) 0xFF) && Objects.equals(bytes[1], (byte) 0xFB);
final boolean flag3 = Objects.equals(bytes[0], (byte) 0xFF) && Objects.equals(bytes[1], (byte) 0xF3);
final boolean flag4 = Objects.equals(bytes[0], (byte) 0xFF) && Objects.equals(bytes[1], (byte) 0xF2);
return flag1 || flag2 || flag3 || flag4;
return ArrayUtil.startWith(bytes,
(byte) 0x49, (byte) 0x44, (byte) 0x33
) || ArrayUtil.startWith(bytes,
(byte) 0xFF, (byte) 0xFB
) || ArrayUtil.startWith(bytes,
(byte) 0xFF, (byte) 0xF3
) || ArrayUtil.startWith(bytes,
(byte) 0xFF, (byte) 0xF2
);
}
},
/**
* ogg
* <pre>
* prefix: 4F676753
* </pre>
*/
OGG("audio/ogg", "ogg") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 3
&& Objects.equals(bytes[0], (byte) 0x4f)
&& Objects.equals(bytes[1], (byte) 0x67)
&& Objects.equals(bytes[2], (byte) 0x67)
&& Objects.equals(bytes[3], (byte) 0x53);
return ArrayUtil.startWith(bytes,
(byte) 0x4F, (byte) 0x67, (byte) 0x67, (byte) 0x53
);
}
},
/**
@ -295,11 +307,9 @@ public enum FileMagicNumber {
FLAC("audio/x-flac", "flac") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 3
&& Objects.equals(bytes[0], (byte) 0x66)
&& Objects.equals(bytes[1], (byte) 0x4c)
&& Objects.equals(bytes[2], (byte) 0x61)
&& Objects.equals(bytes[3], (byte) 0x43);
return ArrayUtil.startWith(bytes,
(byte) 0x66, (byte) 0x4c, (byte) 0x61, (byte) 0x43
);
}
},
/**
@ -342,30 +352,14 @@ public enum FileMagicNumber {
AMR("audio/amr", "amr") {
@Override
public boolean match(final byte[] bytes) {
//single-channel
if (bytes.length < 11) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x23)
&& Objects.equals(bytes[1], (byte) 0x21)
&& Objects.equals(bytes[2], (byte) 0x41)
&& Objects.equals(bytes[3], (byte) 0x4d)
&& Objects.equals(bytes[4], (byte) 0x52)
&& Objects.equals(bytes[5], (byte) 0x0A);
//multi-channel:
final boolean flag2 = Objects.equals(bytes[0], (byte) 0x23)
&& Objects.equals(bytes[1], (byte) 0x21)
&& Objects.equals(bytes[2], (byte) 0x41)
&& Objects.equals(bytes[3], (byte) 0x4d)
&& Objects.equals(bytes[4], (byte) 0x52)
&& Objects.equals(bytes[5], (byte) 0x5F)
&& Objects.equals(bytes[6], (byte) 0x4d)
&& Objects.equals(bytes[7], (byte) 0x43)
&& Objects.equals(bytes[8], (byte) 0x31)
&& Objects.equals(bytes[9], (byte) 0x2e)
&& Objects.equals(bytes[10], (byte) 0x30)
&& Objects.equals(bytes[11], (byte) 0x0a);
return flag1 || flag2;
return ArrayUtil.startWith(bytes,
(byte) 0x23, (byte) 0x21, (byte) 0x41, (byte) 0x4d,
(byte) 0x52, (byte) 0x0A
) || ArrayUtil.startWith(bytes,
(byte) 0x23, (byte) 0x21, (byte) 0x41, (byte) 0x4d,
(byte) 0x52, (byte) 0x5F, (byte) 0x4d, (byte) 0x43,
(byte) 0x31, (byte) 0x2e, (byte) 0x30, (byte) 0x0a
);
}
},
/**
@ -374,9 +368,9 @@ public enum FileMagicNumber {
AC3("audio/ac3", "ac3") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 2
&& Objects.equals(bytes[0], (byte) 0x0b)
&& Objects.equals(bytes[1], (byte) 0x77);
return ArrayUtil.startWith(bytes,
(byte) 0x0b, (byte) 0x77
);
}
},
/**
@ -390,6 +384,7 @@ public enum FileMagicNumber {
&& Objects.equals(bytes[1], (byte) 0x4f)
&& Objects.equals(bytes[2], (byte) 0x52)
&& Objects.equals(bytes[3], (byte) 0x4d)
&& Objects.equals(bytes[8], (byte) 0x41)
&& Objects.equals(bytes[9], (byte) 0x49)
&& Objects.equals(bytes[10], (byte) 0x46)
@ -409,19 +404,23 @@ public enum FileMagicNumber {
if (bytes.length < 8) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
final boolean flag1 =
Objects.equals(bytes[0], (byte) 0x77)
&& Objects.equals(bytes[1], (byte) 0x4f)
&& Objects.equals(bytes[2], (byte) 0x46)
&& Objects.equals(bytes[3], (byte) 0x46);
final boolean flag2 = Objects.equals(bytes[4], (byte) 0x00)
final boolean flag2 =
Objects.equals(bytes[4], (byte) 0x00)
&& Objects.equals(bytes[5], (byte) 0x01)
&& Objects.equals(bytes[6], (byte) 0x00)
&& Objects.equals(bytes[7], (byte) 0x00);
final boolean flag3 = Objects.equals(bytes[4], (byte) 0x4f)
final boolean flag3 =
Objects.equals(bytes[4], (byte) 0x4f)
&& Objects.equals(bytes[5], (byte) 0x54)
&& Objects.equals(bytes[6], (byte) 0x54)
&& Objects.equals(bytes[7], (byte) 0x4f);
final boolean flag4 = Objects.equals(bytes[4], (byte) 0x74)
final boolean flag4 =
Objects.equals(bytes[4], (byte) 0x74)
&& Objects.equals(bytes[5], (byte) 0x72)
&& Objects.equals(bytes[6], (byte) 0x75)
&& Objects.equals(bytes[7], (byte) 0x65);
@ -437,19 +436,23 @@ public enum FileMagicNumber {
if (bytes.length < 8) {
return false;
}
final boolean flag1 = Objects.equals(bytes[0], (byte) 0x77)
final boolean flag1 =
Objects.equals(bytes[0], (byte) 0x77)
&& Objects.equals(bytes[1], (byte) 0x4f)
&& Objects.equals(bytes[2], (byte) 0x46)
&& Objects.equals(bytes[3], (byte) 0x32);
final boolean flag2 = Objects.equals(bytes[4], (byte) 0x00)
final boolean flag2 =
Objects.equals(bytes[4], (byte) 0x00)
&& Objects.equals(bytes[5], (byte) 0x01)
&& Objects.equals(bytes[6], (byte) 0x00)
&& Objects.equals(bytes[7], (byte) 0x00);
final boolean flag3 = Objects.equals(bytes[4], (byte) 0x4f)
final boolean flag3 =
Objects.equals(bytes[4], (byte) 0x4f)
&& Objects.equals(bytes[5], (byte) 0x54)
&& Objects.equals(bytes[6], (byte) 0x54)
&& Objects.equals(bytes[7], (byte) 0x4f);
final boolean flag4 = Objects.equals(bytes[4], (byte) 0x74)
final boolean flag4 =
Objects.equals(bytes[4], (byte) 0x74)
&& Objects.equals(bytes[5], (byte) 0x72)
&& Objects.equals(bytes[6], (byte) 0x75)
&& Objects.equals(bytes[7], (byte) 0x65);
@ -1241,15 +1244,9 @@ public enum FileMagicNumber {
WASM("application/wasm", "wasm") {
@Override
public boolean match(final byte[] bytes) {
return bytes.length > 7
&& Objects.equals(bytes[0], (byte) 0x00)
&& Objects.equals(bytes[1], (byte) 0x61)
&& Objects.equals(bytes[2], (byte) 0x73)
&& Objects.equals(bytes[3], (byte) 0x6D)
&& Objects.equals(bytes[4], (byte) 0x01)
&& Objects.equals(bytes[5], (byte) 0x00)
&& Objects.equals(bytes[6], (byte) 0x00)
&& Objects.equals(bytes[7], (byte) 0x00);
return ArrayUtil.startWith(bytes,
(byte) 0x00, (byte) 0x61, (byte) 0x73, (byte) 0x6D, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00
);
}
},
/**