Skip to content

Package: Buffers

Buffers

nameinstructionbranchcomplexitylinemethod
toByteArray(ByteBuffer)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
wrap(byte[])
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others.
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.client.gateway.spi.util;
14:
15: import java.nio.ByteBuffer;
16:
17: public final class Buffers {
18:
19: private Buffers() {
20: }
21:
22: /**
23: * Wrap bytes into a {@link ByteBuffer}, as if they would just haven been put
24: *
25: * @param data
26: * the data to use, may be {@code null} or empty
27: * @return the new {@link ByteBuffer}, may be {@code null} or empty, if the input is {@code null} or empty
28: */
29: public static ByteBuffer wrap(final byte[] data) {
30:• if (data == null) {
31: return null;
32: }
33:
34: final ByteBuffer buffer = ByteBuffer.wrap(data);
35: buffer.position(buffer.limit());
36:
37: return buffer;
38: }
39:
40: /**
41: * Extract the remaining data as a byte array
42: *
43: * @param buffer
44: * the input buffer, may be {@code null}
45: * @return the output array, may be empty or {@code null} if the input is empty or {@code null}
46: */
47: public static byte[] toByteArray(final ByteBuffer buffer) {
48:• if (buffer == null) {
49: return null;
50: }
51:
52: final byte[] byteArray = new byte[buffer.remaining()];
53: buffer.get(byteArray);
54: return byteArray;
55: }
56: }