I'm writing a little utility that downloads a file from a server and the re-uploads it to a different location. I've got it working just fine by using a temporary file, along the following lines:
> val zip = File.createTempFile("promote", null)
> zip.deleteOnExit
> Http(source >>> new FileOutputStream(zip))
> Http(destination <<< (zip, "application/zip") >|)
But the temporary file bugs me - the amount of data is small and will easily fit in RAM, so I'd like to do something like:
> val data = new ByteArrayOutputStream
> Http(source >>> data)
> Http(destination <<< new ByteArrayInputStream(data.toByteArray) >|)
But I can't find anything that will post from an InputStream :-(
Am I missing something? Is there a better way to achieve the same effect?
--
paul.butcher->msgCount++
Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?
http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
Hi Paul, glad you like it!
> [...]
>
>> val zip = File.createTempFile("promote", null)
>> zip.deleteOnExit
>> Http(source>>> new FileOutputStream(zip))
>> Http(destination<<< (zip, "application/zip")>|)
> But the temporary file bugs me - the amount of data is small and will easily fit in RAM, so I'd like to do something like:
>
>> val data = new ByteArrayOutputStream
>> Http(source>>> data)
>> Http(destination<<< new ByteArrayInputStream(data.toByteArray)>|)
> But I can't find anything that will post from an InputStream :-(
You mean to PUT from an InputStream, I think, since the <<< method is a
PUT. There isn't support for this built into Dispatch, though there
probably should be. Look at the implementation for the method you are using:
/** PUT the given file. */
def <<< (file: java.io.File, content_type: String) = PUT.copy(
body=Some(new org.apache.http.entity.FileEntity(file, content_type))
)
https://github.com/dispatch/dispatch/blob/master/core/src/main/scala/requests.scala#L153
Instead of FileEntity there are a couple of AbstractHttpEntity
subclasses that would do what you want:
So you could do this with your own utility method, or you could add it
to Dispatch and send a pull request.
Nathan
Of course I do - apologies for any confusion.
> So you could do this with your own utility method, or you could add it to Dispatch and send a pull request.
Many thanks (and to Daniel). If I get a chance to turn this into something I'd be happy to issue a pull request for, I will do - but don't hold your breath (I have my hands full with ScalaMock at the moment :-)
Thanks again!