Skip to content

Commit b52ab01

Browse files
committed
core: Opaque: provide helper method to instantiate from ByteBuffer
We can construct an Opaque instance from the bytes provided in a ByteBuffer. This saves us one clone() in Inode. Related: #149 Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent e74c087 commit b52ab01

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

core/src/main/java/org/dcache/nfs/util/Opaque.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package org.dcache.nfs.util;
2121

22+
import java.nio.ByteBuffer;
2223
import java.util.Arrays;
2324
import java.util.Base64;
2425

@@ -31,6 +32,22 @@ public static Opaque forBytes(byte[] bytes) {
3132
return new OpaqueImpl(bytes.clone());
3233
}
3334

35+
/**
36+
* Returns an {@link Opaque} instance based on a copy of the {@code length} bytes from the given {@link ByteBuffer}
37+
* starting from the current {@link ByteBuffer#position()} plus the given {@code offset}.
38+
*
39+
* @param buf The buffer.
40+
* @param offset The offset (relative to the current position).
41+
* @param length The number of bytes.
42+
* @return The {@link Opaque} instance.
43+
*/
44+
public static Opaque forBytes(ByteBuffer buf, int offset, int length) {
45+
byte[] bytes = new byte[length];
46+
buf.get(bytes, offset, length);
47+
48+
return new OpaqueImpl(bytes);
49+
}
50+
3451
byte[] toBytes();
3552

3653
String toBase64();

core/src/main/java/org/dcache/nfs/vfs/Inode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ public Inode(byte[] bytes) {
109109
exportIdx = b.getInt();
110110
type = (int) b.get();
111111
int olen = (int) b.get();
112-
byte[] fs_opaque = new byte[olen];
113-
b.get(fs_opaque);
114-
this.opaqueKey = Opaque.forBytes(fs_opaque);
112+
this.opaqueKey = Opaque.forBytes(b, 0, olen);
115113

116114
this.nfsHandle = bytes.clone();
117115
}

0 commit comments

Comments
 (0)