multipart stream generator

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

multipart stream generator

getagrip
Hi,

is there a special reason for the following method taking a function which returns an InputStream?

<<(name: String, file_name: String, stream: () => InputStream)

Why not pass a Stream directly?
Reply | Threaded
Open this post in threaded view
|

Re: multipart stream generator

n8han
Administrator
Special reason? It takes a stream generator is so that the stream is not created until (and unless) the request is actually executed. Also, when I was testing that method in the console without the generator, it was too easy to try to reuse a stream reference which will cause a runtime exception. The generator function makes the safer behavior explicit.

By the way if you can use File you should. The underlying apache mime library will produce a content length header for files, but it can't do that with a stream.

And finally, if you want an example of the callback listner (which is kind of neat!) it's used here to drive an upload progress meter:

https://github.com/meetup/snapup-android/blob/master/src/main/scala/Members.scala#L113

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: multipart stream generator

getagrip
Thanks for the response,

I have the stream containing all data already at hand coming from a Subversion repository, so unfortunately I cannot use files as this would kill performance.
Reply | Threaded
Open this post in threaded view
|

Re: multipart stream generator

n8han
Administrator
On 11/8/10 11:05 AM, getagrip [via Databinder] wrote:
> I have the stream containing all data already at hand coming from a
> Subversion repository, so unfortunately I cannot use files as this
> would kill performance.

Cool. You should be able to a Content-Length header manually with <:<,
assuming you have the content length available.

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: multipart stream generator

getagrip
Thanks for the hint.

The compiler will not accept a function thinking it is the actual stream:

"Type mismatch expected () => InputStream, actual InputStream"

I guess it's a little messy to create a var which is set just to be returned by the streamFunc-function but that's the only way I can think of:

object SVNIndexer {

  var svnStream:InputStream
 
  def streamFunc:InputStream = svnStream

  def index(url) {
    svnStream = SVNRepository.get(url)
    extract(url, streamFunc _)
  }
 
  def extract(url:String, streamFunc: () => InputStream) {
    val request = :/("localhost", 8983) / "solr"
    val http = new dispatch.Http
    var post = request << Map("id" -> url) << ("file", "filename", streamFunc _)
    http(post / command >>> System.out)
  }
}
Reply | Threaded
Open this post in threaded view
|

Re: multipart stream generator

n8han
Administrator
This should work:

val post = request << Map("id" -> url) << ("file", "filename", {
     () => SVNRepository.get(url) } )

Reply | Threaded
Open this post in threaded view
|

Re: multipart stream generator

getagrip
Thanks! I love Scala more and more every day.