kanadianDri3
kanadianDri3

Reputation: 371

insert HttpInputStream into a OutputStream in groovy

Was if there's a difference in Groovy between

IOUtils.copyLarge(content, output)

And

output << content

Where output is an OutputStream object and content is a HttpInputStream.

Also, hope I'm doing it right to get back the information from the connection

def connection = (HttpURLConnection)(new URL(myURL).openConnection())
def content = connection.getContent()

Thanks in advance for the tips !

Upvotes: 1

Views: 860

Answers (1)

Rhysyngsun
Rhysyngsun

Reputation: 951

Assuming you're referring to IOUtils from Apache's commons-io library, a look into the respective sources reveals a key difference between Apache's IOUtils.copyLarge and Groovy's OutputStream.leftShift (a.k.a <<), namely that Groovy's calls Thread.yield();:

https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java#L207

In this case when nothing is read from the InputStream but it hasn't reached the end of it yet the thread will pause, allowing other threads to continue whereas IOUtils will simply block its thread until it's done processing the InputStream. This means that Groovy's << operator may behave differently then IOUtils.copyLarge depending on what other threads you have operating at that time and their relative priority.

HttpURLConnection.getContent() actually returns type of Object. This could be an HttpInputStream, but it might not be so you should be verifying the type of that object with:

if (content instanceof HttpInputStream) {
    output << content
} else {
    // Handle error condition
}

Upvotes: 2

Related Questions