How to get memcpy in Java?
<p>I am developing an app which communicate with a <strong>C Program</strong> running on windows server, This program is developed using Visual Studio (if this information can be of any help). </p>

<p>The server sends me an integer through socket communication, Before sending server does following things:-</p>

<ol>
<li>Declare an int </li>
<li>Assign it some value</li>
<li>copy 2 bytes to a char * (say buffer), using <strong>memcpy</strong></li>
<li>Add some more data to that buffer</li>
<li>Send that buffer</li>
</ol>

<p>Now at receiving end I have java implementation, so can't use <strong>memcpy</strong> directly, I have used </p>

<pre><code>short mId = java.nio.ByteBuffer.wrap(recvBuf, 0, 2).order(ByteOrder.LITTLE_ENDIAN).getShort();
</code></pre>

<p>which works, fine but this part of code is called in every few miliseconds, So I am trying to optimize it.. I have also used </p>

<pre><code>short mId =(short)(recvBuf[0] + recvBuf[1]*128);
</code></pre>

<p>Which also works fine but I doubt whether it will work if the number increases in future. What is the best way to do the repeat of <strong>memcpy</strong> in java ?</p>

<p>I have visited <a href="http://stackoverflow.com/questions/3329163/is-there-an-equivalent-to-memcpy-in-java">this</a> thread but that is not of much help,</p>

<p><strong>EDIT</strong>
I implemented following four methods which worked for me,</p>

<pre><code>public class CommonMethods {
    /*
     * Returns the byte[] representation of an int in Little Endian format
     * 
     * @param value that should be converted to byte[]
     */
    public static byte[] toByteArray(int value) {
        return new byte[] { (byte) value, (byte) (value &gt;&gt; 8), (byte) (value &gt;&gt; 16), (byte) (value &gt;&gt; 24) };
    }

    /*
     * Returns the int in LittleEndian value of the passed byte[]
     * 
     * @param bytes is the input byte[]
     * 
     * @param offset is the offset to start
     */
    public static int getInt(byte[] bytes, int offset, int length) {
        int retValue = (bytes[offset] &amp; 0xFF);
        byte bVal;
        for (int i = 1; i &lt; length; i++) {
            bVal = bytes[offset + i];
            retValue |= ((bVal &amp; 0xFF) &lt;&lt; (8 + (8 * (i - 1))));
        }
        return retValue;
    }

    /*
     * Returns the int in BigEndian from the passed byte[]
     * 
     * @param bytes is the byte[]
     */
    public static int getIntBigEndian(byte[] bytes, int offset, int length) {
        int retValue = (bytes[offset + length - 1] &amp; 0xFF);
        for (int i = 1; i &lt; length; i++) {
            retValue |= ((bytes[offset + length - 1 - i] &amp; 0xFF) &lt;&lt; (8 + (8 * (i - 1))));
        }
        return retValue;
    }

    /*
     * Returns the byte[] representation of an int in Big Endian format
     * 
     * @param value that should be converted to byte[]
     */
    public static byte[] toByteArrayBigEndian(int value) {
        return new byte[] { (byte) (value &gt;&gt; 24), (byte) (value &gt;&gt; 16), (byte) (value &gt;&gt; 8), (byte) value };
    }
}
</code></pre>
