在Java中抑制运行时控制台警告的最好方法是什么? [英] What's the best way to suppress a runtime console warning in Java?

查看:202
本文介绍了在Java中抑制运行时控制台警告的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用org.apache.commons.httpclient.methods.PostMethod类的getResponseBody()方法。但是,我总是在运行时收到一条消息写入控制台:



警告:进入缓冲区响应主体大或未知的大小。建议使用getResponseBodyAsStream。



在代码中,我必须将响应写入字节数组,所以它是getResponseBody应该使用。但是有没有一个简单的方法,我可以抑制警告消息,所以我不必看看它在每次运行?



如果是一个编译器错误,我使用 @SuppressWarnings 注释,但这不是编译时问题;它发生在运行时。另外,我可以使用getResponseBodyAsStream写入一个ByteArrayOutputStream,但这似乎是一个黑客的方法来解决警告(额外的代码行做什么getResponseBody()已经为我做)。



我的猜测是,答案涉及System.out或System.err操作,但是有一个很好的方法来做这个?

解决方案

我建议你做警告建议和使用流而不是字节数组。如果你试图推送的响应特别大(假设它是一个大文件),你会将其加载到内存,这将是一个非常糟糕的事情。



你最好使用流。



也就是说,你可以通过替换System .err或System.out。它们只是 PrintStream 对象,它们可以使用setOut和setErr方法设置。

  PrintStream oldErr = System.err; 
PrintStream newErr = new PrintStream(new ByteArrayOutputStream());
System.setErr(newErr);

//做你的工作

System.setErr(oldErr);



编辑


我同意,最好是
使用流,但现在,
目标API,我需要把
响应是一个字节数组。如果
是必要的,我们可以做一个重构的
API,它将允许它采取
流;这将是更好。
警告是肯定有
的原因。


如果您可以修改API , 这样做。在这种情况下,流处理是最好的方式。如果你不能因为内部压力或其他原因,请去 @ John M 的路由,并提升 BUFFER_WARN_TRIGGER_LIMIT - 但确保您有一个已知的contentLength,该路由将失败。


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?

解决方案

I would recommend that you do as the warning suggests and use a stream rather than a byte array. If the response you're trying to push is particularly large (suppose it's a large file), you will load it all into memory, and that'd be a very bad thing.

You're really better off using streams.

That said, you might hack around it by replacing System.err or System.out temporarily. They're just PrintStream objects, and they're settable with the setOut and setErr methods.

PrintStream oldErr = System.err;
PrintStream newErr = new PrintStream(new ByteArrayOutputStream());
System.setErr(newErr);

// do your work

System.setErr(oldErr);

Edit:

I agree that it would be preferable to use streams, but as it is now, the target API where I need to put the response is a byte array. If necessary, we can do a refactor to the API that will allow it to take a stream; that would be better. The warning is definitely there for a reason.

If you can modify the API, do so. Stream processing is the best way to go in this case. If you can't due to internal pressures or whatever, go @John M's route and pump up the BUFFER_WARN_TRIGGER_LIMIT -- but make sure you have a known contentLength, or even that route will fail.

这篇关于在Java中抑制运行时控制台警告的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆