Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to suppress a runtime console warning in Java?

I am using the getResponseBody() method of the org.apache.commons.httpclient.methods.PostMethod class. However, I am always getting a message written to the console at runtime:

WARNING: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

In the code I have to write the response to a byte array anyway, so it is the getResponseBody() method that I ought to use. But is there an easy way that I can suppress the warning message so I don't have to look at it at every run?

If it was a compiler error, I'd use the @SuppressWarnings annotation, but this isn't a compile-time issue; it happens at runtime. Also, I could use getResponseBodyAsStream to write to a ByteArrayOutputStream, but this seems like a hacky way to get around the warning (extra lines of code to do what getResponseBody() is already doing for me).

My guess is that the answer involves System.out or System.err manipulation, but is there a good way to do this?

like image 305
seansand Avatar asked Apr 13 '09 19:04

seansand


1 Answers

If you want to cleanly stop this log entry, there's a tunable max that triggers the warning. I saw this looking at the code.

        int limit = getParams().getIntParameter(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1024*1024);
        if ((contentLength == -1) || (contentLength > limit)) {
            LOG.warn("Going to buffer response body of large or unknown size. "
                    +"Using getResponseBodyAsStream instead is recommended.");
        }

HttpMethodBase.setParams() looks like the place to set HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT to the desired value.

like image 147
John M Avatar answered Sep 21 '22 21:09

John M