Skip to content

Commit e01d9f8

Browse files
committed
core: Opaque: add byteAt/longAt support
We sometimes want to check a certain byte or long as part of an Opaque. Let's add helper methods so we don't have to convert to byte[]. Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent ad8f569 commit e01d9f8

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
package org.dcache.nfs.util;
2121

2222
import java.nio.ByteBuffer;
23+
import java.nio.ByteOrder;
2324
import java.util.Arrays;
2425
import java.util.Base64;
2526
import java.util.Objects;
2627

28+
import org.dcache.oncrpc4j.util.Bytes;
29+
2730
/**
2831
* Describes something that can be used as a key for {@link java.util.HashMap} and that can be converted to a
2932
* {@code byte[]} and a Base64 string representation.
@@ -85,6 +88,9 @@ static Opaque forBytes(ByteBuffer buf, int length) {
8588
* @see #toImmutableOpaque()
8689
*/
8790
static Opaque forMutableByteBuffer(ByteBuffer buf, int index, int length) {
91+
if (buf.order() != ByteOrder.BIG_ENDIAN) {
92+
buf = buf.duplicate();
93+
}
8894
return new OpaqueBufferImpl(buf, index, length);
8995
}
9096

@@ -174,10 +180,26 @@ default void putBytes(ByteBuffer buf) {
174180
@Override
175181
boolean equals(Object o);
176182

177-
class OpaqueImpl implements Opaque {
183+
/**
184+
* Returns the byte stored at the given position.
185+
*
186+
* @param byteOffset The byte offset
187+
* @return The byte.
188+
*/
189+
byte byteAt(int byteOffset);
190+
191+
/**
192+
* Returns the {@code long} stored at the given position, using big-endian byte order.
193+
*
194+
* @param byteOffset The byte offset
195+
* @return The long.
196+
*/
197+
long longAt(int byteOffset);
198+
199+
public class OpaqueImpl implements Opaque {
178200
final byte[] _opaque;
179201

180-
OpaqueImpl(byte[] opaque) {
202+
protected OpaqueImpl(byte[] opaque) {
181203
_opaque = opaque;
182204
}
183205

@@ -253,6 +275,16 @@ public int numBytes() {
253275
public Opaque toImmutableOpaque() {
254276
return Opaque.forBytes(_opaque);
255277
}
278+
279+
@Override
280+
public byte byteAt(int position) {
281+
return _opaque[position];
282+
}
283+
284+
@Override
285+
public long longAt(int byteOffset) {
286+
return Bytes.getLong(_opaque, byteOffset);
287+
}
256288
}
257289

258290
final class OpaqueImmutableImpl extends OpaqueImpl {
@@ -360,13 +392,29 @@ public boolean equals(Object o) {
360392
}
361393
return true;
362394
} else {
363-
return toImmutableOpaque().equals(o);
395+
Opaque other = (Opaque) o;
396+
for (int i = index, n = index + length, oi = 0; i < n; i++, oi++) {
397+
if (buf.get(i) != other.byteAt(oi)) {
398+
return false;
399+
}
400+
}
401+
return true;
364402
}
365403
}
366404

367405
@Override
368406
public String toString() {
369407
return super.toString() + "[" + toBase64() + "]";
370408
}
409+
410+
@Override
411+
public byte byteAt(int position) {
412+
return buf.get(position);
413+
}
414+
415+
@Override
416+
public long longAt(int byteOffset) {
417+
return buf.getLong(byteOffset);
418+
}
371419
}
372420
}

0 commit comments

Comments
 (0)