An equivalent of javax.nio.Buffer.flip() in c#

2019-07-15 13:59发布

I am porting some of the java code and need to be able to flip (javax.nio.Buffer.flip()).

I am using byte[] to store data, and I want to flip these byte array, much like Buffer does it, as I believe underneath Buffer class uses byte[] as well.

Thanks

2条回答
Root(大扎)
2楼-- · 2019-07-15 14:36

There's no direct equivalent of the java.nio package in .NET. Asynchronous IO is usually handled with Stream.BeginRead/EndRead, but it's not really the same model. Could you tell us what you're trying to do?

EDIT: You've now provided the low-level details of what you're trying to do, but as there's no real equivalent for the nio classes in .NET, you'll either have to write them yourself (not impossible, but a pain) or use a different higher level type. You might be able to use MemoryStream for example - write into it, then seek back to the start. That's a bit like flipping a byte buffer. However, we can't tell whether or not that's appropriate without more information as to the higher-level goal. There may be a much better way of doing it.

查看更多
再贱就再见
3楼-- · 2019-07-15 14:48

ByteBuffer may use a byte[] (create from, say, ByteBuffer.wrap) or non-Java heap memory (created with ByteBuffer.allocateDirect). You can get the underlying byte[] with ByteBuffer.array. But calling flip and similar methods on buffers does not chnage the actual data. Instead offsets associated with the data are changed. So the equivalent with byte would be to change the offsets that your code is associating with it.

查看更多
登录 后发表回答