Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the equivalent of "ByteBuffer.flip" & "ByteBuffer.slice" in .NET?
    text
    copied!<p>I need to port code from Java to C#. In the Java code, the methods "ByteBuffer.flip()" and "ByteBuffer.slice" is used, and I don't know how to translate this.</p> <p>I've read this question (<a href="https://stackoverflow.com/questions/607587/an-equivalent-of-javax-nio-buffer-flip-in-c">An equivalent of javax.nio.Buffer.flip() in c#</a>), but although an answer is given, I cannot figure how to apply it. According to Tom Hawtin, I should "Set the limit to the current position and then set the position to zero" in the underlying array. I am unsure as of how to change these values. (If you could explain the underlying logic, it would help me a lot :)</p> <p>As for the ByteBuffer.slice, I have no clue on how to translate it.</p> <p>EDIT: If it can be clearer with the actual code, I'll post it:</p> <p>Java:</p> <pre><code>ByteBuffer buff; buff.putShort((short) 0); buff.put(customArray); buff.flip(); buff.putShort((short) 0); ByteBuffer b = buff.slice(); short size = (short) (customFunction(b) + 2); buff.putShort(0, size); buff.position(0).limit(size); </code></pre> <p>So far, my translation in C#.NET:</p> <pre><code>BinaryWriter b = new BinaryWriter(); //ByteBuffer buff; b.Write((short)0); // buff.putShort((short) 0); b.Write(paramStream.ToArray()); // buff.put(customArray); b.BaseStream.SetLength(b.BaseStream.Position); // buff.flip; (not sure) b.BaseStream.Position = 0; // buff.flip; too (not sure) b.Write((short)0); // buff.putShort((short) 0) ??? // ByteBuffer b = buff.slice(); // Not done but I can do it, short size = (short) (customFunction(b) + 2); ??? // How do I write at a particular position? ??? // buff.position(0).limit(size); I don't know how to do this </code></pre> <p>Thank you!</p> <p>EDIT: Changed <code>b.BaseStream.SetLength(b.BaseStream.Length);</code> to <code>b.BaseStream.SetLength(b.BaseStream.Position);</code>, based on the Java docs.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload