Skip to content

Commit ae1ba5b

Browse files
author
markjrzhang
committed
optimize XStream parsing logic
1 parent 8413907 commit ae1ba5b

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.qcloud</groupId>
66
<artifactId>cos_api</artifactId>
7-
<version>5.6.225</version>
7+
<version>5.6.227</version>
88
<packaging>jar</packaging>
99
<name>cos-java-sdk</name>
1010
<description>java sdk for qcloud cos</description>

src/main/java/com/qcloud/cos/internal/cihandler/XStreamXmlResponsesSaxParser.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@
33
import com.qcloud.cos.exception.CosServiceException;
44
import com.qcloud.cos.internal.CosServiceRequest;
55
import com.thoughtworks.xstream.XStream;
6-
import com.thoughtworks.xstream.converters.Converter;
7-
import com.thoughtworks.xstream.converters.MarshallingContext;
8-
import com.thoughtworks.xstream.converters.SingleValueConverter;
9-
import com.thoughtworks.xstream.converters.UnmarshallingContext;
10-
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
11-
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
12-
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
13-
import com.thoughtworks.xstream.io.xml.StaxDriver;
14-
import com.thoughtworks.xstream.mapper.Mapper;
156
import com.thoughtworks.xstream.security.AnyTypePermission;
167

178
import java.io.InputStream;
18-
import java.io.StringWriter;
199
import java.lang.reflect.Field;
10+
import java.util.concurrent.ConcurrentHashMap;
2011

2112
public abstract class XStreamXmlResponsesSaxParser<T> {
22-
23-
private XStream xstream;
13+
private static final ConcurrentHashMap<String, XStream> xStreamMap = new ConcurrentHashMap<String, XStream>();
2414

2515
abstract T getResponse(InputStream in);
2616

@@ -29,17 +19,21 @@ static XStream initXStream(Object obj) {
2919
}
3020

3121
static <T> XStream initXStream(Class<T> cls) {
32-
XStream xstream = new XStream();
33-
//忽略不需要的节点
34-
xstream.ignoreUnknownElements();
35-
//对指定的类使用Annotations 进行序列化
36-
xstream.processAnnotations(cls);
37-
xstream.addPermission(AnyTypePermission.ANY);
38-
Field[] fields = CosServiceRequest.class.getDeclaredFields();
39-
for (Field field : fields) {
40-
xstream.omitField(CosServiceRequest.class, field.getName());
22+
if (!xStreamMap.containsKey(cls.getName())) {
23+
XStream xstream = new XStream();
24+
//忽略不需要的节点
25+
xstream.ignoreUnknownElements();
26+
//对指定的类使用Annotations 进行序列化
27+
xstream.processAnnotations(cls);
28+
xstream.addPermission(AnyTypePermission.ANY);
29+
Field[] fields = CosServiceRequest.class.getDeclaredFields();
30+
for (Field field : fields) {
31+
xstream.omitField(CosServiceRequest.class, field.getName());
32+
}
33+
xStreamMap.put(cls.getName(), xstream);
34+
return xstream;
4135
}
42-
return xstream;
36+
return xStreamMap.get(cls.getName());
4337
}
4438

4539
public static String toXML(Object obj) {
@@ -48,9 +42,9 @@ public static String toXML(Object obj) {
4842
}
4943

5044
public static <T> T toBean(InputStream inputStream, Class<T> cls) {
51-
try {
45+
try (InputStream in = inputStream) {
5246
XStream xstream = initXStream(cls);
53-
return (T) xstream.fromXML(inputStream);
47+
return (T) xstream.fromXML(in);
5448
} catch (Exception e) {
5549
e.printStackTrace();
5650
throw new CosServiceException("Response parse error");

src/main/java/com/qcloud/cos/model/ciModel/job/VideoTag.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package com.qcloud.cos.model.ciModel.job;
22

3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
35
public class VideoTag {
46
/**
57
* 场景类型,可选择视频标签的运用场景,不同的运用场景使用的算法、输入输出等都会有所差异
68
* 当前版本只适配 Stream 场景
79
*/
10+
@XStreamAlias("Scenario")
811
private String scenario;
12+
@XStreamAlias("Text")
13+
private String text;
14+
15+
public String getText() {
16+
return text;
17+
}
18+
19+
public void setText(String text) {
20+
this.text = text;
21+
}
922

1023
public String getScenario() {
1124
return scenario;

src/main/java/com/qcloud/cos/model/ciModel/xml/CIMediaXmlFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ private static void addVideoTag(XmlWriter xml, VideoTag videoTag) {
256256
if (objIsNotValid(videoTag)) {
257257
xml.start("VideoTag");
258258
addIfNotNull(xml, "Scenario", videoTag.getScenario());
259+
addIfNotNull(xml, "Text", videoTag.getText());
259260
xml.end();
260261
}
261262
}

0 commit comments

Comments
 (0)