org.graylog2.shared.utilities.ByteBufferUtils Maven / Gradle / Ivy
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see .
*/
package org.graylog2.shared.utilities;
import java.nio.ByteBuffer;
public abstract class ByteBufferUtils {
/**
* Read the given byte buffer into a byte array
*/
public static byte[] readBytes(ByteBuffer buffer) {
return readBytes(buffer, 0, buffer.limit());
}
/**
* Read a byte array from the given offset and size in the buffer
*/
public static byte[] readBytes(ByteBuffer buffer, int offset, int size) {
final byte[] dest = new byte[size];
if (buffer.hasArray()) {
System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, size);
} else {
buffer.mark();
buffer.get(dest);
buffer.reset();
}
return dest;
}
}