Java ByteOrder toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)

Here you can find the source of toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)

Description

to Double Byte Array

License

Apache License

Declaration

public static final int toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
            throws NumberFormatException 

Method Source Code

//package com.java2s;
/*/*from  w  w w.  j a v a 2  s . co m*/
 * 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 toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
            throws NumberFormatException {
        return toInt64ByteArray(Double.doubleToLongBits(val), outOrder, buf, off);
    }

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

    public static final byte[] toDoubleByteArray(double val, ByteOrder outOrder) throws NumberFormatException {
        return toInt64ByteArray(Double.doubleToLongBits(val), outOrder);
    }

    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. longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
  2. opposite(ByteOrder order)
  3. reduceToSmallestByteArray(byte[] originalBytes, ByteOrder byteOrder, boolean isSigned)
  4. setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder)
  5. toBytes(int value, ByteOrder order)
  6. toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)
  7. toInt(byte[] bytes, ByteOrder order)
  8. toInt16ByteArray(final int val, final ByteOrder outOrder, final byte[] buf, final int off)
  9. toInt64ByteArray(final long val, final ByteOrder outOrder, final byte[] buf)