2011-01-28 2 views
4

입력 스트림에서 토큰을 바꾸기 위해 Swizzle Stream 라이브러리를 사용해 보았습니다.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[][]{ 
    {"@[email protected]", artifactInfo.getGroup() }, 
    {"@[email protected]", artifactInfo.getName() }, 
    {"@[email protected]", artifactInfo.getVersion() }, 
    {"@[email protected]", artifactInfo.getPackaging() }, 
    {"@[email protected]", artifactInfo.getFileName() }, 
    {"@[email protected]", req.getQueryString() }, 
    }); 


    // This does not replace anything, no idea why. // 
    ReplaceStringsInputStream replacingIS = new ReplaceStringsInputStream(pomIS, map); 
    ReplaceStringInputStream replacingIS2 = new ReplaceStringInputStream(pomIS, "@[email protected]", "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("@[email protected]", artifactInfo.getGroup()) 
    .replace("@[email protected]", artifactInfo.getName()) 
    .replace("@[email protected]", artifactInfo.getVersion()) 
    .replace("@[email protected]", artifactInfo.getPackaging()) 
    .replace("@[email protected]", artifactInfo.getFileName()) 
    .replace("@[email protected]", req.getQueryString()); 

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

작동.

무엇이 잘못 되었나요?

+0

코드는 괜찮습니다. 디버거를 사용하여 코드를 넣을 수 있습니다. – Ron

답변

3

IOUtils.copy는 replaceStringInputStream의 수퍼 클래스 인 FixedTokenReplacementInputStream에 의해 무시되는 read() 대신 read (byte []) 메서드를 호출합니다. 예를 들어, 다음과 같이 복사를 직접 구현해야합니다.

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

오 ... 그래서 스즐리는 일관성없는 API를 가지고 있습니다. :) 지적 해 주셔서 고마워요, 이것은 정말로 내 마음을 교차시키지 않았습니다. –

+1

오늘 내 자신에게 달려 들어 라. 버그 수정. 며칠 후 1.6.2에 나타납니다. –