问题描述
ByteBuffer
提供 asFloatBuffer()
功能。但是,没有相应的 FloatBuffer.asByteBuffer()
ByteBuffer
offers asFloatBuffer()
function. However, there is no equivalent FloatBuffer.asByteBuffer()
我正在尝试:
float[] array = ...
try( ByteChannel channel = Files.newByteChannel( path, WRITE, CREATE, TRUNCATE_EXISTING ) ) {
channel.write (FloatBuffer.wrap (array) .asByteBuffer());
}
有没有办法有效地做到这一点,或者我必须求助于分配 ByteBuffer
,如下所示:
Is there a way of efficiently doing this, or do I have to resort to allocating a ByteBuffer
as in:
ByteBuffer buffer = ByteBuffer.allocate( array.length * 4 );
buffer .asFloatBuffer() .put (array);
channel.write (buffer);
推荐答案
对于 HeapFloatBuffer
,即由 FloatBuffer.allocate
或 FloatBuffer.wrap
创建,没有简单的解决方案。需要编写一个扩展ByteBuffer的自定义类。
For a HeapFloatBuffer
, ie created by FloatBuffer.allocate
or FloatBuffer.wrap
, there is no easy solution. A custom class extending ByteBuffer needs to be written.
对于HotSpot 8中的直接缓冲区,这将适用于简单的情况:
For direct buffers in HotSpot 8, this will work in the trivial case:
FloatBuffer floatBuffer = ByteBuffer.allocateDirect (...).asFloatBuffer();
ByteBuffer byteBuffer = (ByteBuffer) ((sun.nio.ch.DirectBuffer)floatBuffer).attachment();
对于其他情况,请使用以下方法。请注意,此类在包 java.nio
中声明。这也可能仅适用于HotSpot 8.
For other situations, use the following. Note that this class is declared in package java.nio
. This also probably will only work in HotSpot 8.
package java.nio;
/**
*
* @author Aleksandr Dubinsky
*/
public class BufferUtils {
public static ByteBuffer
asByteBuffer (FloatBuffer floatBuffer) {
if (floatBuffer instanceof DirectFloatBufferU)
{
DirectFloatBufferU buffer = (DirectFloatBufferU) floatBuffer;
return (ByteBuffer) new DirectByteBuffer (buffer.address(), floatBuffer.capacity() * Float.BYTES, buffer)
.position (floatBuffer.position() * Float.BYTES)
.limit (floatBuffer.limit() * Float.BYTES);
}
else if (floatBuffer instanceof DirectFloatBufferS)
{
DirectFloatBufferS buffer = (DirectFloatBufferS) floatBuffer;
return (ByteBuffer) new DirectByteBuffer (buffer.address(), floatBuffer.capacity() * Float.BYTES, buffer)
.position (floatBuffer.position() * Float.BYTES)
.limit (floatBuffer.limit() * Float.BYTES);
}
else if (floatBuffer instanceof ByteBufferAsFloatBufferB)
{
ByteBufferAsFloatBufferB buffer = (ByteBufferAsFloatBufferB)floatBuffer;
return (ByteBuffer) ((ByteBuffer) buffer.bb
.duplicate()
.position (buffer.offset)
.limit (buffer.offset + buffer.capacity() * Float.BYTES))
.slice()
.position (buffer.position() * Float.SIZE)
.limit (buffer.limit() * Float.BYTES);
}
else if (floatBuffer instanceof ByteBufferAsFloatBufferL)
{
ByteBufferAsFloatBufferL buffer = (ByteBufferAsFloatBufferL)floatBuffer;
return (ByteBuffer) ((ByteBuffer) buffer.bb
.duplicate()
.position (buffer.offset)
.limit (buffer.offset + buffer.capacity() * Float.BYTES))
.slice()
.position (buffer.position() * Float.SIZE)
.limit (buffer.limit() * Float.BYTES);
}
else
throw new IllegalArgumentException ("Unsupported implementing class " + floatBuffer.getClass().getName());
}
}
这篇关于将FloatBuffer转换为ByteBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!