Java ByteBuffer Get getBoolean(ByteBuffer bytes)

Here you can find the source of getBoolean(ByteBuffer bytes)

Description

get Boolean

License

Apache License

Declaration

public static boolean getBoolean(ByteBuffer bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Apigee Corporation//from w  w  w.j  a  va2 s .  com
 * 
 * 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.ByteBuffer;

public class Main {
    /**
     * @param bytes
     * @return
     */
    public static boolean getBoolean(byte[] bytes) {
        return bytes[0] != 0;
    }

    public static boolean getBoolean(ByteBuffer bytes) {
        return bytes.slice().get() != 0;
    }

    /**
     * @param bytes
     * @param offset
     * @return
     */
    public static boolean getBoolean(byte[] bytes, int offset) {
        return bytes[offset] != 0;
    }

    public static boolean getBoolean(Object obj) {
        if (obj instanceof Boolean) {
            return (Boolean) obj;
        } else if (obj instanceof String) {
            return Boolean.parseBoolean((String) obj);
        } else if (obj instanceof Number) {
            return ((Number) obj).longValue() > 0;
        }

        return false;
    }
}

Related

  1. getBigDecimalFromByteBuffer( ByteBuffer bytebuf, int start, int length, int scale)
  2. getBit(ByteBuffer in, int pos)
  3. getBitString(java.nio.ByteBuffer buffer, int lenBits)
  4. getBoolean(ByteBuffer b)
  5. getBoolean(ByteBuffer bb)
  6. getBoolean(java.nio.ByteBuffer buffer)
  7. getBuffer(ByteBuffer buffer, int len)
  8. getBufferBytes(ByteBuffer bb)
  9. getByte(ByteBuffer bb)