gpt4 book ai didi

java - 使用 Swizzle Stream 替换流中的字符串

转载 作者:搜寻专家 更新时间:2023-11-01 03:13:33 26 4
gpt4 key购买 nike

我已经尝试使用 Swizzle Stream 库来替换输入流中的标记。

  String RESOURCE_PATH = "FakePom.xml";
InputStream pomIS = JarFinderServlet.class.getClassLoader().getResourceAsStream( RESOURCE_PATH );

if( null == pomIS )
throw new MavenhoeException("Can't read fake pom template - getResourceAsStream( RESOURCE_PATH ) == null");

Map map = ArrayUtils.toMap( new String[][]{
{"@GRP@", artifactInfo.getGroup() },
{"@ART@", artifactInfo.getName() },
{"@VER@", artifactInfo.getVersion() },
{"@PACK@", artifactInfo.getPackaging() },
{"@NAME@", artifactInfo.getFileName() },
{"@DESC@", req.getQueryString() },
} );


// This does not replace anything, no idea why. //
ReplaceStringsInputStream replacingIS = new ReplaceStringsInputStream(pomIS, map);
ReplaceStringInputStream replacingIS2 = new ReplaceStringInputStream(pomIS, "@VER@", "0.0-AAAAA");
ReplaceStringInputStream replacingIS3 = new ReplaceStringInputStream(pomIS, "@", "#");

ServletOutputStream os = resp.getOutputStream();
IOUtils.copy( replacingIS, os );
replacingIS.close();

这没有用。它只是不更换。于是我使出了“PHP之道”……

  String pomTemplate = IOUtils.toString(pomIS)
.replace("@GRP@", artifactInfo.getGroup() )
.replace("@ART@", artifactInfo.getName() )
.replace("@VER@", artifactInfo.getVersion() )
.replace("@PACK@", artifactInfo.getPackaging() )
.replace("@NAME@", artifactInfo.getFileName() )
.replace("@DESC@", req.getQueryString() );

ServletOutputStream os = resp.getOutputStream();
IOUtils.copy( new StringInputStream(pomTemplate), os );
os.close();

有效。

怎么了?

最佳答案

IOUtils.copy 调用 read(byte[]) 方法而不是 read(),它被 ReplaceStringInputStream 的父类(super class) FixedTokenReplacementInputStream 覆盖。您应该自己实现复制,例如,如下所示:

try {
int b;
while ((b = pomIS.read()) != -1) {
os.write(b);
}} finally { os.flush();os.close(); }

关于java - 使用 Swizzle Stream 替换流中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4829773/

26 4 0