mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-11-25 14:57:00 +00:00
perf: vectorize bounded Base64 encoding and decoding with scalar fallback
This commit is contained in:
1 parent
5681d3b805
commit
44b023d919
18 files changed
+291
-32
No files matched your search
@@ -1,11 +1,11 @@
|
||||
package cn.qaiu.parser.clientlink.impl;
|
||||
|
||||
import cn.qaiu.util.Base64Encoder;
|
||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 迅雷协议链接生成器
|
||||
@@ -27,7 +27,7 @@ public class ThunderLinkGenerator implements ClientLinkGenerator {
|
||||
String thunderUrl = "AA" + originalUrl + "ZZ";
|
||||
|
||||
// Base64编码
|
||||
String encodedUrl = Base64.getEncoder().encodeToString(
|
||||
String encodedUrl = Base64Encoder.encode(
|
||||
thunderUrl.getBytes(StandardCharsets.UTF_8)
|
||||
);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.util.Base64Encoder;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.PanBase;
|
||||
import io.netty.handler.codec.http.cookie.Cookie;
|
||||
@@ -14,7 +15,6 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -48,7 +48,7 @@ public class GenShortUrl extends PanBase {
|
||||
public Future<String> parse() {
|
||||
String longUrl = shareLinkInfo.getShareUrl();
|
||||
|
||||
String url = WRAPPER_URL + Base64.getEncoder().encodeToString(longUrl.getBytes());
|
||||
String url = WRAPPER_URL + Base64Encoder.encode(longUrl.getBytes());
|
||||
|
||||
String payload = "mid=" + MID + "&content=" + URLEncoder.encode(url, StandardCharsets.UTF_8);
|
||||
clientSession.cookieStore().put(cookie);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.util.Base64Encoder;
|
||||
import cn.qaiu.entity.FileInfo;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.PanBase;
|
||||
@@ -13,7 +14,6 @@ import io.vertx.core.json.JsonObject;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
@@ -232,7 +232,7 @@ public class LeTool extends PanBase {
|
||||
"shareId", shareId,
|
||||
"fileId", fileId
|
||||
);
|
||||
String paramBase64 = Base64.getEncoder().encodeToString(paramJson.encode().getBytes());
|
||||
String paramBase64 = Base64Encoder.encode(paramJson.encode().getBytes());
|
||||
|
||||
// 设置解析URL和预览URL
|
||||
fileInfo.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.util.Base64Encoder;
|
||||
import cn.qaiu.entity.FileInfo;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.PanBase;
|
||||
@@ -22,7 +23,6 @@ import java.net.MalformedURLException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -262,7 +262,7 @@ public class YeTool extends PanBase {
|
||||
.put("Size", item.getLong("Size"))
|
||||
.put("Etag", item.getString("Etag"));
|
||||
|
||||
byte[] encode = Base64.getEncoder().encode(postData.encode().getBytes());
|
||||
byte[] encode = Base64Encoder.encodeBytes(postData.encode().getBytes());
|
||||
String param = new String(encode);
|
||||
|
||||
if (item.getInteger("Type") == 0) { // 文件
|
||||
|
||||
@@ -11,7 +11,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.HexFormat;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -131,13 +130,13 @@ public class AESUtils {
|
||||
public static String encryptBase64ByAES(String source, String keyString) throws NoSuchPaddingException,
|
||||
NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
|
||||
byte[] encrypted = encryptByAES(source, keyString);
|
||||
return Base64.getEncoder().encodeToString(encrypted);
|
||||
return Base64Encoder.encode(encrypted);
|
||||
}
|
||||
|
||||
public static String encryptBase64ByAES(String source, Key key) throws NoSuchPaddingException,
|
||||
NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
|
||||
byte[] encrypted = encryptByAES(source, key);
|
||||
return Base64.getEncoder().encodeToString(encrypted);
|
||||
return Base64Encoder.encode(encrypted);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,9 +41,11 @@ public final class Base64Decoder {
|
||||
|
||||
int groupEnd = dataLength - remainder;
|
||||
byte[] decoded = new byte[(groupEnd / 4) * 3 + (remainder == 0 ? 0 : remainder - 1)];
|
||||
int outputIndex = 0;
|
||||
int start = Base64Encoder.useVectors(groupEnd)
|
||||
? VectorBase64.decode(input, groupEnd, decoded, alphabet == URL_SAFE) : 0;
|
||||
int outputIndex = start / 4 * 3;
|
||||
// Each complete group consumes exactly four characters and writes exactly three bytes.
|
||||
for (int i = 0; i < groupEnd; i += 4) {
|
||||
for (int i = start; i < groupEnd; i += 4) {
|
||||
int a = alphabet[input[i] & 0xff];
|
||||
int b = alphabet[input[i + 1] & 0xff];
|
||||
int c = alphabet[input[i + 2] & 0xff];
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package cn.qaiu.util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class Base64Encoder {
|
||||
private static final boolean VECTORS = vectorsAvailable();
|
||||
static final String BASIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static final String URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
|
||||
private Base64Encoder() {}
|
||||
|
||||
private static boolean vectorsAvailable() {
|
||||
if (java.nio.ByteOrder.nativeOrder() != java.nio.ByteOrder.LITTLE_ENDIAN
|
||||
|| ModuleLayer.boot().findModule("jdk.incubator.vector").isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return VectorBase64.available();
|
||||
} catch (LinkageError unavailableOnThisJdk) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static boolean useVectors(int length) {
|
||||
return VECTORS && length >= 1024;
|
||||
}
|
||||
|
||||
public static String encode(byte[] data) {
|
||||
return useVectors(data.length) ? new String(vectorEncode(data, false), StandardCharsets.ISO_8859_1)
|
||||
: Base64.getEncoder().encodeToString(data);
|
||||
}
|
||||
|
||||
public static byte[] encodeBytes(byte[] data) {
|
||||
return useVectors(data.length) ? vectorEncode(data, false) : Base64.getEncoder().encode(data);
|
||||
}
|
||||
|
||||
public static String encodeUrlWithoutPadding(byte[] data) {
|
||||
return useVectors(data.length) ? new String(vectorEncode(data, true), StandardCharsets.ISO_8859_1)
|
||||
: Base64.getUrlEncoder().withoutPadding().encodeToString(data);
|
||||
}
|
||||
|
||||
private static byte[] vectorEncode(byte[] input, boolean url) {
|
||||
int remainder = input.length % 3;
|
||||
int tailLength = remainder + 1;
|
||||
if (remainder == 0) {
|
||||
tailLength = 0;
|
||||
} else if (!url) {
|
||||
tailLength = 4;
|
||||
}
|
||||
long length = (long) (input.length / 3) * 4 + tailLength;
|
||||
if (length > Integer.MAX_VALUE) {
|
||||
throw new OutOfMemoryError("Base64 output exceeds maximum array length");
|
||||
}
|
||||
byte[] output = new byte[(int) length];
|
||||
int src = VectorBase64.encode(input, output, url);
|
||||
int dst = src / 3 * 4;
|
||||
String alphabet = url ? URL : BASIC;
|
||||
while (src < input.length) {
|
||||
int bytes = Math.min(3, input.length - src);
|
||||
int bits = (input[src++] & 255) << 16;
|
||||
if (bytes >= 2) {
|
||||
bits |= (input[src++] & 255) << 8;
|
||||
}
|
||||
if (bytes == 3) {
|
||||
bits |= input[src++] & 255;
|
||||
}
|
||||
for (int shift = 18; shift >= 18 - bytes * 6; shift -= 6) {
|
||||
output[dst++] = (byte) alphabet.charAt((bits >>> shift) & 63);
|
||||
}
|
||||
}
|
||||
java.util.Arrays.fill(output, dst, output.length, (byte) '=');
|
||||
return output;
|
||||
}
|
||||
}
|
||||
@@ -118,9 +118,7 @@ public class CommonUtils {
|
||||
* <p>不再做 URLEncoder,避免前端/代理再 encode 时变成 %253D。</p>
|
||||
*/
|
||||
public static String urlBase64Encode(String str) {
|
||||
return java.util.Base64.getUrlEncoder()
|
||||
.withoutPadding()
|
||||
.encodeToString(str.getBytes(StandardCharsets.UTF_8));
|
||||
return Base64Encoder.encodeUrlWithoutPadding(str.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String padBase64(String s) {
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package cn.qaiu.util;
|
||||
|
||||
import jdk.incubator.vector.ByteVector;
|
||||
import jdk.incubator.vector.IntVector;
|
||||
import jdk.incubator.vector.VectorMask;
|
||||
import jdk.incubator.vector.VectorShuffle;
|
||||
import jdk.incubator.vector.VectorSpecies;
|
||||
|
||||
import static jdk.incubator.vector.VectorOperators.*;
|
||||
|
||||
final class VectorBase64 {
|
||||
private static final VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_PREFERRED;
|
||||
private static final int WIDTH = SPECIES.length();
|
||||
private static final int PACKED = WIDTH / 4 * 3;
|
||||
private static final VectorMask<Byte> PACKED_MASK = SPECIES.indexInRange(0, PACKED);
|
||||
// Each four-byte lane holds one quartet. Pack or expand its three binary bytes.
|
||||
private static final VectorShuffle<Byte> PACK = VectorShuffle.fromOp(SPECIES,
|
||||
i -> i < PACKED ? i / 3 * 4 + i % 3 : 0);
|
||||
private static final VectorShuffle<Byte> EXPAND = VectorShuffle.fromOp(SPECIES,
|
||||
i -> i / 4 * 3 + Math.min(i % 4, 2));
|
||||
|
||||
// Each low-nibble entry is a bit set of allowed high nibbles.
|
||||
private static final class Alphabet {
|
||||
final ByteVector lowMasks;
|
||||
final ByteVector highMasks;
|
||||
final ByteVector offsets;
|
||||
final byte last;
|
||||
final ByteVector encodeTable;
|
||||
|
||||
Alphabet(String letters) {
|
||||
byte[] low = new byte[Math.max(WIDTH, 16)];
|
||||
byte[] high = new byte[low.length];
|
||||
byte[] offset = new byte[low.length];
|
||||
byte[] chars = new byte[WIDTH];
|
||||
if (WIDTH >= 64) {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
chars[i] = (byte) letters.charAt(i);
|
||||
}
|
||||
}
|
||||
encodeTable = ByteVector.fromArray(SPECIES, chars, 0);
|
||||
last = (byte) letters.charAt(63);
|
||||
for (int i = 0; i < letters.length(); i++) {
|
||||
int ch = letters.charAt(i);
|
||||
int bucket = ch >>> 4;
|
||||
byte bit = (byte) (1 << bucket);
|
||||
high[bucket] = bit;
|
||||
low[ch & 15] |= bit;
|
||||
if (ch != last) {
|
||||
offset[bucket] = (byte) (i - ch);
|
||||
}
|
||||
}
|
||||
lowMasks = ByteVector.fromArray(SPECIES, low, 0);
|
||||
highMasks = ByteVector.fromArray(SPECIES, high, 0);
|
||||
offsets = ByteVector.fromArray(SPECIES, offset, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Alphabet BASIC = new Alphabet(Base64Encoder.BASIC);
|
||||
private static final Alphabet URL = new Alphabet(Base64Encoder.URL);
|
||||
|
||||
private VectorBase64() {}
|
||||
|
||||
static boolean available() {
|
||||
if (WIDTH < 16) {
|
||||
return false;
|
||||
}
|
||||
// Incubator APIs can change between JDK releases. Link both kernels before enabling them.
|
||||
byte[] plain = new byte[PACKED];
|
||||
byte[] encoded = new byte[WIDTH];
|
||||
byte[] decoded = new byte[PACKED];
|
||||
plain[0] = (byte) 0xfb;
|
||||
encode(plain, encoded, true);
|
||||
decode(encoded, WIDTH, decoded, true);
|
||||
return encoded[0] == '-' && java.util.Arrays.equals(plain, decoded);
|
||||
}
|
||||
|
||||
static int decode(byte[] input, int groupEnd, byte[] output, boolean url) {
|
||||
Alphabet alphabet = url ? URL : BASIC;
|
||||
int limit = SPECIES.loopBound(groupEnd);
|
||||
int dst = 0;
|
||||
for (int src = 0; src < limit; src += WIDTH) {
|
||||
ByteVector ascii = ByteVector.fromArray(SPECIES, input, src);
|
||||
ByteVector high = ascii.lanewise(LSHR, 4);
|
||||
ByteVector low = ascii.and((byte) 15);
|
||||
// The low-nibble table records which high nibbles form legal characters.
|
||||
if (high.selectFrom(alphabet.highMasks).and(low.selectFrom(alphabet.lowMasks)).eq((byte) 0).anyTrue()) {
|
||||
throw new IllegalArgumentException("Invalid Base64 character");
|
||||
}
|
||||
ByteVector values = ascii.add(high.selectFrom(alphabet.offsets)).blend((byte) 63, ascii.eq(alphabet.last));
|
||||
// [a,b,c,d] -> [(a<<2)|(b>>4), (b<<4)|(c>>2), (c<<6)|d, unused].
|
||||
IntVector w = values.reinterpretAsInts();
|
||||
IntVector bytes = w.lanewise(LSHL, 2).and(0xfc).or(w.lanewise(LSHR, 12).and(3))
|
||||
.or(w.lanewise(LSHL, 4).and(0xf000)).or(w.lanewise(LSHR, 10).and(0x0f00))
|
||||
.or(w.lanewise(LSHL, 6).and(0xc00000)).or(w.lanewise(LSHR, 8).and(0x3f0000));
|
||||
bytes.reinterpretAsBytes().rearrange(PACK).intoArray(output, dst, PACKED_MASK);
|
||||
dst += PACKED;
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
|
||||
static int encode(byte[] input, byte[] output, boolean url) {
|
||||
int limit = input.length / PACKED * PACKED;
|
||||
int dst = 0;
|
||||
for (int src = 0; src < limit; src += PACKED) {
|
||||
IntVector w = ByteVector.fromArray(SPECIES, input, src, PACKED_MASK)
|
||||
.rearrange(EXPAND).reinterpretAsInts();
|
||||
// Undo the quartet packing, extracting four six-bit indices per lane.
|
||||
IntVector indices = w.lanewise(LSHR, 2).and(63)
|
||||
.or(w.lanewise(LSHL, 12).and(0x3000)).or(w.lanewise(LSHR, 4).and(0x0f00))
|
||||
.or(w.lanewise(LSHL, 10).and(0x3c0000)).or(w.lanewise(LSHR, 6).and(0x030000))
|
||||
.or(w.lanewise(LSHL, 8).and(0x3f000000));
|
||||
ByteVector v = indices.reinterpretAsBytes();
|
||||
if (WIDTH >= 64) {
|
||||
v.selectFrom((url ? URL : BASIC).encodeTable).intoArray(output, dst);
|
||||
} else {
|
||||
v.add((byte) 'A').blend(v.add((byte) ('a' - 26)), v.compare(GE, (byte) 26))
|
||||
.blend(v.add((byte) ('0' - 52)), v.compare(GE, (byte) 52))
|
||||
.blend((byte) (url ? '-' : '+'), v.eq((byte) 62))
|
||||
.blend((byte) (url ? '_' : '/'), v.eq((byte) 63)).intoArray(output, dst);
|
||||
}
|
||||
dst += WIDTH;
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user