开发者

Groovy, overloading << operator on ZipOutputStream

开发者 https://www.devze.com 2023-03-08 15:31 出处:网络
based on Google I managed to write a little Groovy script that packs a zip just as I needed. ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(\"${uid}.pufi\"));

based on Google I managed to write a little Groovy script that packs a zip just as I needed.

ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream("${uid}.pufi"));

ZipEntry mainentry = new ZipEntry('main.xml')
zipOutput.putNextEntry(mainentry)
zipOutput << "mainmainmain"
zipOutput.closeEntry()

ZipEntry manifentry = new ZipEntry('manifest.xml')
zipOutput.putNextEntry(manifentry)
zipOutput <<开发者_Go百科 new FileInputStream(options.manifest)
zipOutput.closeEntry()

It works, but I would like to know how does Groovy figure out what to call on lines entry << "foobar" or entry << new FileInputStream(..). As I see ZipOutputStream is a Java class, its javadoc doesn't contain any method, that could be called with even String or InputStream arguments. Could you explain me how does it work and where is it documented? I'd like to know more about Groovy.. :-)


Groovy adds additional methods to some basic Java classes in order to use them in a more groovy way. For a complete overview af the additional methods look at http://groovy.codehaus.org/groovy-jdk. In your case, the method leftShift was added the class OutputStream. Groovy also overloads the << operator, so it's the same as calling the method leftShift on the object.

0

精彩评论

暂无评论...
验证码 换一张
取 消