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

コードはOKに見えますが、デバッガでそれにステップする時間であるかもしれません。 – 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に表示されるはずです。 –

関連する問題