001/*******************************************************************************
002 * Copyright (c) 2017 Red Hat Inc and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Red Hat Inc - initial API and implementation
011 *******************************************************************************/
012package org.eclipse.kapua.gateway.client.utils;
013
014import java.nio.ByteBuffer;
015
016public final class Buffers {
017
018    private Buffers() {
019    }
020
021    public static ByteBuffer wrap(final byte[] data) {
022        if (data == null) {
023            return null;
024        }
025
026        final ByteBuffer buffer = ByteBuffer.wrap(data);
027        buffer.position(buffer.limit());
028
029        return buffer;
030    }
031
032    public static byte[] toByteArray(final ByteBuffer buffer) {
033        if (buffer == null) {
034            return null;
035        }
036
037        final byte[] byteArray = new byte[buffer.remaining()];
038        buffer.get(byteArray);
039        return byteArray;
040    }
041}