Java ByteOrder toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf)

Here you can find the source of toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf)

Description

to Int Byte Array

License

Apache License

Declaration

public static final int toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf)
            throws NumberFormatException 

Method Source Code

//package com.java2s;
/*//from  w  w w.j  av  a  2  s . c  om
 * Copyright 2013 Lyor Goldstein
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.ByteOrder;

public class Main {
    public static final int toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf,
            final int off) throws NumberFormatException {
        if (null == outOrder)
            throw new NumberFormatException("toInt64ByteArray(" + val + ") no order specified");

        if (ByteOrder.BIG_ENDIAN.equals(outOrder)) {
            for (int dIndex = 0, bIndex = off, shiftSize = Long.SIZE
                    - Byte.SIZE; dIndex < 8; dIndex++, shiftSize -= Byte.SIZE, bIndex++) {
                final long dv = (shiftSize > 0) ? (val >> shiftSize) : val;
                buf[bIndex] = (byte) (dv & 0x00FFL);
            }
        } else {
            for (int dIndex = off + 8, shiftSize = Long.SIZE
                    - Byte.SIZE; dIndex > off; dIndex--, shiftSize -= Byte.SIZE) {
                final long dv = (shiftSize > 0) ? (val >> shiftSize) : val;
                buf[dIndex - 1] = (byte) (dv & 0x00FFL);
            }
        }

        return 8;
    }

    public static final int toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf)
            throws NumberFormatException {
        return toInt64ByteArray(val, outOrder, buf, 0);
    }

    public static final byte[] toInt64ByteArray(long val, ByteOrder outOrder) throws NumberFormatException {
        final byte[] data = new byte[Long.SIZE / Byte.SIZE];
        final int eLen = toInt64ByteArray(val, outOrder, data);
        if (eLen != data.length)
            throw new NumberFormatException("toInt64ByteArray(" + val + ")[" + outOrder
                    + "] unexpected used length: expected=" + data.length + "/got=" + eLen);
        return data;
    }
}

Related

  1. toBytes(int value, ByteOrder order)
  2. toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
  3. toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)
  4. toInt(byte[] bytes, ByteOrder order)
  5. toInt16ByteArray(final int val, final ByteOrder outOrder, final byte[] buf, final int off)