Utility增加byteArray方法

This commit is contained in:
Redkale
2019-11-18 13:25:51 +08:00
parent 34ae2d38c5
commit 2ea2667fa7

View File

@@ -37,13 +37,13 @@ public final class Utility {
/**
* <blockquote><pre>
* public final class AnonymousArrayFunction implements java.util.function.Function&lt;Object, char[]&gt; {
* public final class AnonymousCharArrayFunction implements java.util.function.Function&lt;Object, char[]&gt; {
*
* final sun.misc.Unsafe unsafe;
*
* final long fd;
*
* public AnonymousArrayFunction(Object obj, long fd) {
* public AnonymousCharArrayFunction(Object obj, long fd) {
* this.unsafe = (sun.misc.Unsafe) obj;
* this.fd = fd;
* }
@@ -56,37 +56,80 @@ public final class Utility {
* }
* </pre></blockquote>
*/
private static final String functionClassBinary = "cafebabe00000034002f0a00090022070023090008002409000800250a000200260700"
+ "270a0008002807002907002a07002b010006756e736166650100114c73756e2f6d6973632f556e736166653b01000266640100014a0100063c69"
+ "6e69743e010016284c6a6176612f6c616e672f4f626a6563743b4a2956010004436f646501000f4c696e654e756d6265725461626c650100124c"
+ "6f63616c5661726961626c655461626c65010004746869730100294c6f72672f7265646b616c652f7574696c2f416e6f6e796d6f757341727261"
+ "7946756e6374696f6e3b0100036f626a0100124c6a6176612f6c616e672f4f626a6563743b0100056170706c79010016284c6a6176612f6c616e"
+ "672f4f626a6563743b295b43010001740100236f72672e6e65746265616e732e536f757263654c6576656c416e6e6f746174696f6e730100144c"
+ "6a6176612f6c616e672f4f766572726964653b010026284c6a6176612f6c616e672f4f626a6563743b294c6a6176612f6c616e672f4f626a6563"
+ "743b0100095369676e61747572650100454c6a6176612f6c616e672f4f626a6563743b4c6a6176612f7574696c2f66756e6374696f6e2f46756e"
+ "6374696f6e3c4c6a6176612f6c616e672f4f626a6563743b5b433e3b01000a536f7572636546696c6501001b416e6f6e796d6f75734172726179"
+ "46756e6374696f6e2e6a6176610c000f002c01000f73756e2f6d6973632f556e736166650c000b000c0c000d000e0c002d002e0100025b430c00"
+ "1800190100276f72672f7265646b616c652f7574696c2f416e6f6e796d6f7573417272617946756e6374696f6e0100106a6176612f6c616e672f"
+ "4f626a65637401001b6a6176612f7574696c2f66756e6374696f6e2f46756e6374696f6e0100032829560100096765744f626a65637401002728"
+ "4c6a6176612f6c616e672f4f626a6563743b4a294c6a6176612f6c616e672f4f626a6563743b0031000800090001000a00020010000b000c0000"
+ "0010000d000e000000030001000f0010000100110000005c00030004000000122ab700012a2bc00002b500032a20b50004b10000000200120000"
+ "001200040000000e0004000f000c0010001100110013000000200003000000120014001500000000001200160017000100000012000d000e0002"
+ "000100180019000200110000004400040002000000102ab400032b2ab40004b60005c00006b00000000200120000000600010000001500130000"
+ "001600020000001000140015000000000010001a00170001001b000000060001001c000010410018001d00020011000000300002000200000006"
+ "2a2bb60007b00000000200120000000600010000000800130000000c000100000006001400150000001b000000060001001c00000002001e0000"
+ "0002001f0020000000020021";
private static final String functionCharClassBinary = "cafebabe00000034002d0a00090020070021090008002209000800230a00020024070025"
+ "0a00080026070027070028070029010006756e736166650100114c73756e2f6d6973632f556e736166653b01000266640100014a0100063c696e69743e0"
+ "10016284c6a6176612f6c616e672f4f626a6563743b4a2956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c56617269"
+ "61626c655461626c650100047468697301002d4c6f72672f7265646b616c652f7574696c2f416e6f6e796d6f757343686172417272617946756e6374696"
+ "f6e3b0100036f626a0100124c6a6176612f6c616e672f4f626a6563743b0100056170706c79010016284c6a6176612f6c616e672f4f626a6563743b295b"
+ "4301000174010026284c6a6176612f6c616e672f4f626a6563743b294c6a6176612f6c616e672f4f626a6563743b0100095369676e61747572650100454"
+ "c6a6176612f6c616e672f4f626a6563743b4c6a6176612f7574696c2f66756e6374696f6e2f46756e6374696f6e3c4c6a6176612f6c616e672f4f626a65"
+ "63743b5b433e3b01000a536f7572636546696c6501001f416e6f6e796d6f757343686172417272617946756e6374696f6e2e6a6176610c000f002a01000"
+ "f73756e2f6d6973632f556e736166650c000b000c0c000d000e0c002b002c0100025b430c0018001901002b6f72672f7265646b616c652f7574696c2f41"
+ "6e6f6e796d6f757343686172417272617946756e6374696f6e0100106a6176612f6c616e672f4f626a65637401001b6a6176612f7574696c2f66756e637"
+ "4696f6e2f46756e6374696f6e0100032829560100096765744f626a656374010027284c6a6176612f6c616e672f4f626a6563743b4a294c6a6176612f6c"
+ "616e672f4f626a6563743b0021000800090001000a00020010000b000c00000010000d000e000000030001000f0010000100110000005c0003000400000"
+ "0122ab700012a2bc00002b500032a20b50004b10000000200120000001200040000001200040013000c0014001100150013000000200003000000120014"
+ "001500000000001200160017000100000012000d000e0002000100180019000100110000004700040002000000132ab400032b2ab40004b60005c00006c"
+ "00006b00000000200120000000600010000001900130000001600020000001300140015000000000013001a0017000110410018001b0001001100000030"
+ "00020002000000062a2bb60007b00000000200120000000600010000000c00130000000c0001000000060014001500000002001c00000002001d001e000"
+ "00002001f";
private static final Function<Object, char[]> strFunction;
private static final Function<Object, char[]> strCharFunction;
private static final Function<Object, char[]> sbFunction;
private static final Function<Object, char[]> sbCharFunction;
/**
* <blockquote><pre>
* public final class AnonymousByteArrayFunction implements java.util.function.Function&lt;Object, byte[]&gt; {
*
* final sun.misc.Unsafe unsafe;
*
* final long fd;
*
* public AnonymousByteArrayFunction(Object obj, long fd) {
* this.unsafe = (sun.misc.Unsafe) obj;
* this.fd = fd;
* }
*
* &#64;Override
* public byte[] apply(Object t) {
* return (byte[]) unsafe.getObject(t, fd);
* }
*
* }
* </pre></blockquote>
*/
private static final String functionByteClassBinary = "cafebabe00000034002d0a00090020070021090008002209000800230a00020024070025"
+ "0a00080026070027070028070029010006756e736166650100114c73756e2f6d6973632f556e736166653b01000266640100014a0100063c696e69743e0"
+ "10016284c6a6176612f6c616e672f4f626a6563743b4a2956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c56617269"
+ "61626c655461626c650100047468697301002d4c6f72672f7265646b616c652f7574696c2f416e6f6e796d6f757342797465417272617946756e6374696"
+ "f6e3b0100036f626a0100124c6a6176612f6c616e672f4f626a6563743b0100056170706c79010016284c6a6176612f6c616e672f4f626a6563743b295b"
+ "4201000174010026284c6a6176612f6c616e672f4f626a6563743b294c6a6176612f6c616e672f4f626a6563743b0100095369676e61747572650100454"
+ "c6a6176612f6c616e672f4f626a6563743b4c6a6176612f7574696c2f66756e6374696f6e2f46756e6374696f6e3c4c6a6176612f6c616e672f4f626a65"
+ "63743b5b423e3b01000a536f7572636546696c6501001f416e6f6e796d6f757342797465417272617946756e6374696f6e2e6a6176610c000f002a01000"
+ "f73756e2f6d6973632f556e736166650c000b000c0c000d000e0c002b002c0100025b420c0018001901002b6f72672f7265646b616c652f7574696c2f41"
+ "6e6f6e796d6f757342797465417272617946756e6374696f6e0100106a6176612f6c616e672f4f626a65637401001b6a6176612f7574696c2f66756e637"
+ "4696f6e2f46756e6374696f6e0100032829560100096765744f626a656374010027284c6a6176612f6c616e672f4f626a6563743b4a294c6a6176612f6c"
+ "616e672f4f626a6563743b0021000800090001000a00020010000b000c00000010000d000e000000030001000f0010000100110000005c0003000400000"
+ "0122ab700012a2bc00002b500032a20b50004b10000000200120000001200040000001200040013000c0014001100150013000000200003000000120014"
+ "001500000000001200160017000100000012000d000e0002000100180019000100110000004700040002000000132ab400032b2ab40004b60005c00006c"
+ "00006b00000000200120000000600010000001900130000001600020000001300140015000000000013001a0017000110410018001b0001001100000030"
+ "00020002000000062a2bb60007b00000000200120000000600010000000c00130000000c0001000000060014001500000002001c00000002001d001e000"
+ "00002001f";
private static final Function<Object, byte[]> strByteFunction;
private static final Function<Object, byte[]> sbByteFunction;
private static final javax.net.ssl.SSLContext DEFAULTSSL_CONTEXT;
private static final javax.net.ssl.HostnameVerifier defaultVerifier = (s, ss) -> true;
static {
Function<Object, char[]> strFunction0 = null;
Function<Object, char[]> sbFunction0 = null;
Function<Object, char[]> strCharFunction0 = null;
Function<Object, char[]> sbCharFunction0 = null;
Function<Object, byte[]> strByteFunction0 = null;
Function<Object, byte[]> sbByteFunction0 = null;
try {
Field f = String.class.getDeclaredField("value");
if (f.getType() == char[].class) { //JDK9及以上不再是char[]
@@ -97,21 +140,40 @@ public final class Utility {
final Method fm = usafe.getClass().getMethod("objectFieldOffset", Field.class);
final long fd1 = (Long) fm.invoke(usafe, f);
final long fd2 = (Long) fm.invoke(usafe, StringBuilder.class.getSuperclass().getDeclaredField("value"));
byte[] bytes = hexToBin(functionClassBinary);
byte[] bytes = hexToBin(functionCharClassBinary);
Class<Attribute> creatorClazz = (Class<Attribute>) new ClassLoader() {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass("org.re" + "dkale.util.AnonymousArrayFunction", bytes);
}.loadClass("org.re" + "dkale.util.AnonymousCharArrayFunction", bytes);
strFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd1);
sbFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd2);
strCharFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd1);
sbCharFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd2);
} else {
Class unsafeClass = Class.forName("sun.misc.Unsafe");
Field safeField = unsafeClass.getDeclaredField("theUnsafe");
safeField.setAccessible(true);
final Object usafe = safeField.get(null);
final Method fm = usafe.getClass().getMethod("objectFieldOffset", Field.class);
final long fd1 = (Long) fm.invoke(usafe, f);
final long fd2 = (Long) fm.invoke(usafe, StringBuilder.class.getSuperclass().getDeclaredField("value"));
byte[] bytes = hexToBin(functionByteClassBinary);
Class<Attribute> creatorClazz = (Class<Attribute>) new ClassLoader() {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass("org.re" + "dkale.util.AnonymousByteArrayFunction", bytes);
strByteFunction0 = (Function<Object, byte[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd1);
sbByteFunction0 = (Function<Object, byte[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd2);
}
} catch (Throwable e) { //不会发生
//e.printStackTrace();
}
strFunction = strFunction0;
sbFunction = sbFunction0;
strCharFunction = strCharFunction0;
sbCharFunction = sbCharFunction0;
strByteFunction = strByteFunction0;
sbByteFunction = sbByteFunction0;
try {
DEFAULTSSL_CONTEXT = javax.net.ssl.SSLContext.getInstance("SSL");
@@ -1861,8 +1923,8 @@ public final class Utility {
public static byte[] encodeUTF8(final String value) {
if (value == null) return new byte[0];
if (strFunction == null) return encodeUTF8(value.toCharArray());
return encodeUTF8((char[]) strFunction.apply(value));
if (strCharFunction == null) return encodeUTF8(value.toCharArray());
return encodeUTF8((char[]) strCharFunction.apply(value));
}
public static byte[] encodeUTF8(final char[] array) {
@@ -1913,14 +1975,28 @@ public final class Utility {
public static char[] charArray(String value) {
if (value == null) return null;
if (strFunction == null) return value.toCharArray();
return strFunction.apply(value);
if (strCharFunction == null) return value.toCharArray();
return strCharFunction.apply(value);
}
public static char[] charArray(StringBuilder value) {
if (value == null) return null;
if (sbFunction == null) return value.toString().toCharArray();
return sbFunction.apply(value);
if (sbCharFunction == null) return value.toString().toCharArray();
return sbCharFunction.apply(value);
}
//只能是单字节字符串
public static byte[] byteArray(String latin1Value) {
if (latin1Value == null) return null;
if (strByteFunction == null) return latin1Value.getBytes();
return strByteFunction.apply(latin1Value);
}
//只能是单字节字符串
public static byte[] byteArray(StringBuilder latin1Value) {
if (latin1Value == null) return null;
if (sbByteFunction == null) return latin1Value.toString().getBytes();
return sbByteFunction.apply(latin1Value);
}
public static ByteBuffer encodeUTF8(final ByteBuffer buffer, final char[] array) {
@@ -1933,8 +2009,8 @@ public final class Utility {
public static int encodeUTF8Length(String value) {
if (value == null) return -1;
if (strFunction == null) return encodeUTF8Length(value.toCharArray());
return encodeUTF8Length(strFunction.apply(value));
if (strCharFunction == null) return encodeUTF8Length(value.toCharArray());
return encodeUTF8Length(strCharFunction.apply(value));
}
public static int encodeUTF8Length(final char[] text) {