Java UUID to Byte Array uuidToBytesNullOk(UUID uuid)

Here you can find the source of uuidToBytesNullOk(UUID uuid)

Description

uuid To Bytes Null Ok

License

Apache License

Parameter

Parameter Description
uuid a parameter

Declaration

public static byte[] uuidToBytesNullOk(UUID uuid) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Apigee Corporation//from  w w w  .j  a va 2  s. c  o  m
 * 
 * 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.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.util.Date;

import java.util.UUID;

public class Main {
    /**
     * 
     */
    public static final String UTF8_ENCODING = "UTF-8";

    /**
     * @param uuid
     * @return
     */
    public static byte[] uuidToBytesNullOk(UUID uuid) {
        if (uuid != null) {
            return bytes(uuid);
        }
        return new byte[16];
    }

    /**
     * @param uuid
     * @return
     */
    public static byte[] bytes(UUID uuid) {
        if (uuid == null) {
            return null;
        }
        long msb = uuid.getMostSignificantBits();
        long lsb = uuid.getLeastSignificantBits();
        byte[] buffer = new byte[16];

        for (int i = 0; i < 8; i++) {
            buffer[i] = (byte) (msb >>> (8 * (7 - i)));
        }
        for (int i = 8; i < 16; i++) {
            buffer[i] = (byte) (lsb >>> (8 * (7 - i)));
        }

        return buffer;
    }

    /**
     * @param s
     * @return
     */
    public static byte[] bytes(String s) {
        return bytes(s, UTF8_ENCODING);
    }

    /**
     * @param s
     * @param encoding
     * @return
     */
    public static byte[] bytes(String s, String encoding) {
        try {
            return s.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            // logger.log(Level.SEVERE, "UnsupportedEncodingException ", e);
            throw new RuntimeException(e);
        }
    }

    public static byte[] bytes(ByteBuffer bb) {
        byte[] b = new byte[bb.remaining()];
        bb.duplicate().get(b);
        return b;
    }

    /**
     * @param b
     * @return
     */
    public static byte[] bytes(Boolean b) {
        byte[] bytes = new byte[1];
        bytes[0] = b ? (byte) 1 : 0;
        return bytes;
    }

    /**
     * @param val
     * @return
     */
    public static byte[] bytes(Long val) {
        ByteBuffer buf = ByteBuffer.allocate(8);
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.putLong(val);
        return buf.array();
    }

    /**
     * @param obj
     * @return
     */
    public static byte[] bytes(Object obj) {
        if (obj == null) {
            return new byte[0];
        } else if (obj instanceof byte[]) {
            return (byte[]) obj;
        } else if (obj instanceof Long) {
            return bytes((Long) obj);
        } else if (obj instanceof String) {
            return bytes((String) obj);
        } else if (obj instanceof UUID) {
            return bytes((UUID) obj);
        } else if (obj instanceof Boolean) {
            return bytes((Boolean) obj);
        } else if (obj instanceof Date) {
            return bytes(((Date) obj).getTime());
        } else {
            return bytes(obj.toString());
        }
    }
}

Related

  1. uuidToByteArray(UUID uuid)
  2. uuidToBytes(UUID id)
  3. uuidToBytes(UUID uuid)
  4. uuidToBytes(UUID uuid)
  5. uuidToBytes(UUID uuid)