压缩java nio直接缓冲区 [英] compression on java nio direct buffers

查看:187
本文介绍了压缩java nio直接缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gzip输入/输出流不能在Java直接缓冲区上运行。



是否存在直接在直接缓冲区上运行的压缩算法实现?



这样就不会有直接缓冲区复制到java字节数组进行压缩的开销。

解决方案

我不是故意减损你的问题,但这个真的是你程序中的一个很好的优化点吗?您是否通过剖析器验证确实存在问题?你提出的问题意味着你没有做过任何研究,只是通过分配一个byte []来猜测你会遇到性能或内存问题。由于此主题中的所有答案可能都是某种类型的黑客攻击,因此在修复之前,您应该确实实际有问题。



<回到问题,如果你想在ByteBuffer上就地压缩数据,答案是否定的,没有能力做到内置于Java中。



如果您按以下方式分配了缓冲区:

  byte [] bytes = getMyData(); 
ByteBuffer buf = ByteBuffer.wrap(bytes);

您可以通过ByteBufferInputStream过滤您的byte [],如前面的答案所示。


The gzip input/output stream dont operate on Java direct buffers.

Is there any compression algorithm implementation out there that operates directly on direct buffers?

This way there would be no overhead of copying a direct buffer to a java byte array for compression.

解决方案

I don't mean to detract from your question, but is this really a good optimization point in your program? Have you verified with a profiler that you indeed have a problem? Your question as stated implies you have not done any research, but are merely guessing that you will have a performance or memory problem by allocating a byte[]. Since all the answers in this thread are likely to be hacks of some sort, you should really verify that you actually have a problem before fixing it.

Back to the question, if you're wanting to compress the data "in place" in on a ByteBuffer, the answer is no, there is no capability to do that built into Java.

If you allocated your buffer like the following:

byte[] bytes = getMyData();
ByteBuffer buf = ByteBuffer.wrap(bytes);

You can filter your byte[] through a ByteBufferInputStream as the previous answer suggested.

这篇关于压缩java nio直接缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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