All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vertexium.security.ByteBufferUtil Maven / Gradle / Ivy

There is a newer version: 4.10.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.vertexium.security;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

class ByteBufferUtil {
    public static byte[] toBytes(ByteBuffer buffer) {
        if (buffer == null)
            return null;
        return Arrays.copyOfRange(buffer.array(), buffer.position(), buffer.limit());
    }

    public static List toByteBuffers(Collection bytesList) {
        if (bytesList == null)
            return null;
        ArrayList result = new ArrayList();
        for (byte[] bytes : bytesList) {
            result.add(ByteBuffer.wrap(bytes));
        }
        return result;
    }

    public static List toBytesList(Collection bytesList) {
        if (bytesList == null)
            return null;
        ArrayList result = new ArrayList();
        for (ByteBuffer bytes : bytesList) {
            result.add(toBytes(bytes));
        }
        return result;
    }

    public static String toString(ByteBuffer bytes) {
        return new String(bytes.array(), bytes.position(), bytes.remaining(), Constants.UTF8);
    }

    public static ByteBuffer toByteBuffers(ByteSequence bs) {
        if (bs == null)
            return null;

        if (bs.isBackedByArray()) {
            return ByteBuffer.wrap(bs.getBackingArray(), bs.offset(), bs.length());
        } else {
            // TODO create more efficient impl
            return ByteBuffer.wrap(bs.toArray());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy