Skip to content

Instantly share code, notes, and snippets.

@timyates
Created May 22, 2013 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timyates/eb5d394ab5e87a3496ce to your computer and use it in GitHub Desktop.
Save timyates/eb5d394ab5e87a3496ce to your computer and use it in GitHub Desktop.
Example of delegation
import org.apache.commons.fileupload.FileItem
import org.apache.commons.fileupload.FileItemHeaders
class StoredFile implements FileItem {
String contentType = 'text/plain'
File artifact
@Override void delete() { throw new UnsupportedOperationException( 'Cannot call delete on a wrapped File' ) }
@Override byte[] get() { artifact.bytes }
@Override String getContentType() { contentType }
@Override String getFieldName() { null }
@Override String getName() { artifact.name }
@Override long getSize() { artifact.length() }
@Override String getString() { artifact.text }
@Override boolean isFormField() { false }
@Override boolean isInMemory() { false }
@Override FileItemHeaders getHeaders() { null }
@Override void setHeaders( FileItemHeaders pHeaders ) { }
@Override void setFieldName( String s ) { }
@Override void setFormField( boolean b ) { }
@Override InputStream getInputStream() throws IOException {
artifact.newInputStream()
}
@Override
OutputStream getOutputStream() throws IOException {
artifact.newOutputStream()
}
@Override
String getString(String charset) throws UnsupportedEncodingException {
artifact.getText( charset )
}
@Override
void write(File file) throws Exception {
file.withOutputStream { ous ->
artifact.withInputStream { ins ->
ous << ins
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment