Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
代码优化,避免内存泄漏问题
  • Loading branch information
kecikeci committed Jun 11, 2024
1 parent f1cbdc9 commit d909ce2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.0.0
version=1.0.2
73 changes: 55 additions & 18 deletions src/main/java/me/forxx/plugin/ResponseDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.netty.buffer.ByteBufUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.reactivestreams.Publisher;
Expand Down Expand Up @@ -101,31 +102,67 @@ public static String addParamToLinksByStyle(String html) {
}

@Override
@SuppressWarnings(value = "unchecked")
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {

if (body instanceof Mono) {
Mono<DataBuffer> monoBody = (Mono<DataBuffer>) body;
return super.writeWith(monoBody.map(dataBuffer -> {
DataBufferUtils.retain(dataBuffer); // 首先保留DataBuffer,避免被自动释放
byte[] originalContentByte = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(originalContentByte);
DataBufferUtils.release(dataBuffer);// 释放掉内存

String originalContent = new String(originalContentByte, StandardCharsets.UTF_8);
String modifiedContent;
if (isLikelyHtml(originalContent)) {
modifiedContent = addParamToLinksByStyle(addParamToLinksByImg(originalContent));
} else {
modifiedContent = originalContent;
}
// 将修改后的内容转换回DataBuffer
return bufferFactory().wrap(modifiedContent.getBytes(StandardCharsets.UTF_8));
}));
return super.writeWith(monoBody.map(dataBuffer -> modifyContent(dataBuffer)));
}
return super.writeWith(body);
}

private DataBuffer modifyContent(DataBuffer dataBuffer) {
try {
byte[] originalContentByte = readBytesFromDataBuffer(dataBuffer);
String originalContent = new String(originalContentByte, StandardCharsets.UTF_8);
String modifiedContent = processContent(originalContent);
return bufferFactory().wrap(modifiedContent.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
// 日志记录或其他异常处理逻辑
// Log the exception or handle it accordingly
return bufferFactory().wrap(new byte[0]);
}
}

private byte[] readBytesFromDataBuffer(DataBuffer dataBuffer) {
byte[] originalContentByte = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(originalContentByte);
// 由于read操作后不再需要dataBuffer,因此在这里释放
DataBufferUtils.release(dataBuffer);
return originalContentByte;
}

private String processContent(String content) {
if (isLikelyHtml(content)) {
return addParamToLinksByStyle(addParamToLinksByImg(content));
} else {
return content;
}
}
@SuppressWarnings(value = "unchecked")
// public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
//
// if (body instanceof Mono) {
// Mono<DataBuffer> monoBody = (Mono<DataBuffer>) body;
// return super.writeWith(monoBody.map(dataBuffer -> {
// DataBufferUtils.retain(dataBuffer); // 首先保留DataBuffer,避免被自动释放
// byte[] originalContentByte = new byte[dataBuffer.readableByteCount()];
// dataBuffer.read(originalContentByte);
// DataBufferUtils.release(dataBuffer);// 释放掉内存
//
// String originalContent = new String(originalContentByte, StandardCharsets.UTF_8);
// String modifiedContent;
// if (isLikelyHtml(originalContent)) {
// modifiedContent = addParamToLinksByStyle(addParamToLinksByImg(originalContent));
// } else {
// modifiedContent = originalContent;
// }
// // 将修改后的内容转换回DataBuffer
// return bufferFactory().wrap(modifiedContent.getBytes(StandardCharsets.UTF_8));
// }));
// }
// return super.writeWith(body);
// }

/**
* 判断是否是html
*
Expand Down

0 comments on commit d909ce2

Please sign in to comment.