-
-
Save timyates/eb5d394ab5e87a3496ce to your computer and use it in GitHub Desktop.
Example of delegation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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