Skip to content

Commit 7bd4b3b

Browse files
author
jordanqin
committed
update qcloud sdk to 5.9.41
1 parent 951ec13 commit 7bd4b3b

File tree

8 files changed

+520
-15
lines changed

8 files changed

+520
-15
lines changed

QCloudCosXml/cos-android-base/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
minSdkVersion 15
77
targetSdkVersion 28
88

9-
versionCode 50940
10-
versionName '5.9.38'
9+
versionCode 50941
10+
versionName '5.9.39'
1111

1212
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1313

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.tencent.cos.xml.utils;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
public class CRC64Calculator {
7+
// ECMA-182标准参数
8+
private static final long POLY = 0xC96C5795D7870F42L;
9+
private static final long INIT = 0xFFFFFFFFFFFFFFFFL;
10+
private static final int GF2_DIM = 64; // GF(2)矩阵维度
11+
12+
public static long getCRC64(InputStream inputStream, long skip, long size) throws IOException {
13+
// 确保精确跳过指定字节数
14+
long remaining = skip;
15+
while (remaining > 0) {
16+
long skipped = inputStream.skip(remaining);
17+
if (skipped <= 0) {
18+
throw new IOException("Failed to skip " + skip + " bytes");
19+
}
20+
remaining -= skipped;
21+
}
22+
23+
CRC64 crc64 = new CRC64();
24+
byte[] buff = new byte[8 * 1024];
25+
long totalRead = 0;
26+
27+
while (totalRead < size) {
28+
int needRead = (int) Math.min(size - totalRead, buff.length);
29+
int readLen = inputStream.read(buff, 0, needRead);
30+
if (readLen == -1) break;
31+
32+
crc64.update(buff, 0, readLen);
33+
totalRead += readLen;
34+
}
35+
36+
if (totalRead != size) {
37+
throw new IOException("Expected to read " + size + " bytes but got " + totalRead);
38+
}
39+
40+
return crc64.getValue();
41+
}
42+
43+
/**
44+
* 合并两个分段CRC64值
45+
* @param crc1 第一段CRC64值(对应0-start字节范围)
46+
* @param crc2 第二段CRC64值(对应start-end字节范围)
47+
* @param len2 第二段数据长度(end-start)
48+
* @return 合并后的CRC64值
49+
*/
50+
public static long combine(long crc1, long crc2, long len2) {
51+
// crc1 = new BigInteger(Long.toUnsignedString(crc1,10)).longValue();
52+
// crc2 = new BigInteger(Long.toUnsignedString(crc2,10)).longValue();
53+
54+
if (len2 == 0) return crc1;
55+
56+
// 转换为中间计算状态(反转并处理初始值)
57+
crc1 = ~crc1 ^ INIT;
58+
crc2 = ~crc2;
59+
60+
// 初始化GF(2)矩阵
61+
long[] mat = new long[GF2_DIM];
62+
long[] even = new long[GF2_DIM];
63+
long[] odd = new long[GF2_DIM];
64+
65+
// 构建多项式矩阵
66+
mat[0] = POLY;
67+
long val = 1L;
68+
for (int i = 1; i < GF2_DIM; i++) {
69+
mat[i] = val;
70+
val <<= 1;
71+
}
72+
73+
// 矩阵平方运算
74+
gf2MatrixSquare(even, mat);
75+
gf2MatrixSquare(odd, even);
76+
77+
// 合并运算
78+
long len = len2;
79+
do {
80+
gf2MatrixSquare(even, odd);
81+
if ((len & 1) != 0) {
82+
crc1 = gf2MatrixTimes(even, crc1);
83+
}
84+
len >>>= 1;
85+
if (len == 0) break;
86+
87+
gf2MatrixSquare(odd, even);
88+
if ((len & 1) != 0) {
89+
crc1 = gf2MatrixTimes(odd, crc1);
90+
}
91+
len >>>= 1;
92+
} while (len != 0);
93+
94+
// 转换为标准CRC64输出格式
95+
return ~(crc1 ^ crc2);
96+
}
97+
98+
// GF(2)矩阵乘法
99+
private static long gf2MatrixTimes(long[] mat, long vec) {
100+
long sum = 0;
101+
int index = 0;
102+
while (vec != 0) {
103+
if ((vec & 1) != 0) {
104+
sum ^= mat[index];
105+
}
106+
vec >>>= 1;
107+
index++;
108+
}
109+
return sum;
110+
}
111+
112+
// GF(2)矩阵平方
113+
private static void gf2MatrixSquare(long[] square, long[] mat) {
114+
for (int n = 0; n < GF2_DIM; n++) {
115+
square[n] = gf2MatrixTimes(mat, mat[n]);
116+
}
117+
}
118+
}

QCloudCosXml/cos-android-base/src/main/java/com/tencent/cos/xml/utils/FileUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import com.tencent.cos.xml.CosXmlBaseService;
2828
import com.tencent.cos.xml.common.ClientErrorCode;
2929
import com.tencent.cos.xml.exception.CosXmlClientException;
30+
import com.tencent.qcloud.core.logger.COSLogger;
3031
import com.tencent.qcloud.core.util.OkhttpInternalUtils;
3132

3233
import java.io.File;
3334
import java.io.FileInputStream;
3435
import java.io.FileOutputStream;
3536
import java.io.IOException;
3637
import java.io.InputStream;
38+
import java.io.RandomAccessFile;
3739

3840
/**
3941
* 文件工具类
@@ -151,5 +153,23 @@ public static File[] listFile(File file){
151153
}
152154
}
153155

156+
/**
157+
* 截断文件到指定长度
158+
* @param file 要截断的文件
159+
* @param newLength 新的文件长度
160+
* @return 是否成功
161+
*/
162+
public static boolean truncateFile(File file, long newLength) {
163+
if (file == null || !file.exists()) {
164+
return false;
165+
}
154166

167+
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
168+
raf.setLength(newLength);
169+
return true;
170+
} catch (IOException e) {
171+
COSLogger.wProcess("FileUtils", "Truncate file failed: " + e.getMessage());
172+
return false;
173+
}
174+
}
155175
}
Binary file not shown.

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/core/ServiceFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public CosXmlSimpleService newDualCheckService() {
247247
.setConnectionTimeout(10000)
248248
.setSocketTimeout(10000)
249249
.setRegion(TestConst.DUALCHECK_PERSIST_BUCKET_REGION)
250-
.setHost("dual-check.mynewcos.com")
250+
.setHost("dual-check.stor-helper.com")
251251
// 设置tls客户端证书
252252
.setClientCertificate(certificateBytes, password)
253253
.builder();

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/transfer/DownloadTest.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import com.tencent.cos.xml.model.CosXmlResult;
4444
import com.tencent.cos.xml.model.object.GetObjectBytesRequest;
4545
import com.tencent.cos.xml.model.object.GetObjectRequest;
46+
import com.tencent.cos.xml.utils.CRC64Calculator;
4647
import com.tencent.qcloud.core.http.HttpTaskMetrics;
48+
import com.tencent.qcloud.core.logger.COSLogger;
4749
import com.tencent.qcloud.core.logger.QCloudLogger;
4850

4951
import org.junit.After;
@@ -52,6 +54,9 @@
5254
import org.junit.runner.RunWith;
5355

5456
import java.io.File;
57+
import java.io.IOException;
58+
import java.io.InputStream;
59+
import java.math.BigInteger;
5560
import java.net.InetAddress;
5661

5762
@RunWith(AndroidJUnit4.class)
@@ -357,7 +362,7 @@ public void onFail(CosXmlRequest request, CosXmlClientException clientException,
357362
TransferManager transferManager = ServiceFactory.INSTANCE.newDefaultTransferManager();
358363

359364
GetObjectRequest getObjectRequest = new GetObjectRequest(TestConst.PERSIST_BUCKET,
360-
TestConst.PERSIST_BUCKET_SMALL_OBJECT_PATH,
365+
TestConst.PERSIST_BUCKET_BIG_OBJECT_PATH,
361366
TestUtils.localParentPath());
362367
getObjectRequest.setRange(0, 10);
363368
getObjectRequest.addNoSignHeader("Range");
@@ -741,4 +746,54 @@ private void getObjectBytesByPath(String cosPath, boolean isFail, boolean isObje
741746
}
742747
Assert.assertTrue(true);
743748
}
749+
750+
@Test public void testCrc64CheckSuccess() {
751+
// 测试CRC64校验成功场景
752+
TransferManager transferManager = ServiceFactory.INSTANCE.newDefaultTransferManager();
753+
COSXMLDownloadTask downloadTask = transferManager.download(TestUtils.getContext(),
754+
TestConst.ASR_BUCKET,
755+
// "ProxyDroid_3.2.0_APKPure.apk",
756+
// "pdd漏洞报告.pdf",
757+
"com.vmall.client.2406280940.apk",
758+
// TestConst.ASR_OBJECT_LONG,
759+
TestUtils.localParentPath());
760+
761+
// 模拟服务端返回CRC64值
762+
// downloadTask.setServerCrc64("1234567890"); // 假设这是正确的CRC值
763+
764+
final TestLocker testLocker = new TestLocker();
765+
downloadTask.setCosXmlResultListener(new CosXmlResultListener() {
766+
@Override
767+
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
768+
COSLogger.dProcess("COSXMLDownloadTask", "onSuccess");
769+
testLocker.release();
770+
}
771+
772+
@Override
773+
public void onFail(CosXmlRequest request, CosXmlClientException clientException, CosXmlServiceException serviceException) {
774+
TestUtils.printError(TestUtils.getCosExceptionMessage(clientException, serviceException));
775+
testLocker.release();
776+
}
777+
});
778+
testLocker.lock();
779+
TestUtils.assertCOSXMLTaskSuccess(downloadTask);
780+
}
781+
782+
@Test public void testCrc64CheckFailure1() throws IOException {
783+
int m = 10485760;
784+
InputStream fis1 = TestUtils.getContext().getResources().getAssets().open("ProxyDroid_3.2.0_APKPure.apk");
785+
int available = 15317944;
786+
long crc1 = CRC64Calculator.getCRC64(fis1, 0, m);
787+
System.out.println("COSXMLDownloadTask crc1: " + Long.toUnsignedString(crc1,10));
788+
789+
InputStream fis2 = TestUtils.getContext().getResources().getAssets().open("ProxyDroid_3.2.0_APKPure.apk");
790+
long crc2 = CRC64Calculator.getCRC64(fis2, m, available - m);
791+
System.out.println("COSXMLDownloadTask crc2: " + Long.toUnsignedString(crc2,10));
792+
793+
crc1 = new BigInteger(Long.toUnsignedString(crc1,10).trim()).longValue();
794+
crc2 = new BigInteger(Long.toUnsignedString(crc2,10).trim()).longValue();
795+
System.out.println("COSXMLDownloadTask: combine: " + "crc1=" + crc1 + ", crc2=" + crc2 + ", len2=" + (available-m));
796+
long combined = CRC64Calculator.combine(crc1, crc2, available-m);
797+
System.out.println("COSXMLDownloadTask: combined: " + Long.toUnsignedString(combined));
798+
}
744799
}

0 commit comments

Comments
 (0)