Skip to content

Commit 69a7305

Browse files
authored
Add a way to get a safe ByteBuffer representation of a VBlob (#74)
1 parent c1ff1d2 commit 69a7305

File tree

10 files changed

+5895
-21096
lines changed

10 files changed

+5895
-21096
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repositories {
2020
}
2121

2222
dependencies {
23-
implementation("app.photofox.vips-ffm:vips-ffm-core:0.5.6")
23+
implementation("app.photofox.vips-ffm:vips-ffm-core:0.5.7")
2424
}
2525
```
2626

core/src/main/java/app/photofox/vipsffm/VBlob.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
package app.photofox.vipsffm;
22

3+
import app.photofox.vipsffm.jextract.VipsArea;
4+
import app.photofox.vipsffm.jextract.VipsRaw;
5+
6+
import java.lang.foreign.Arena;
37
import java.lang.foreign.MemorySegment;
8+
import java.nio.ByteBuffer;
9+
10+
import static app.photofox.vipsffm.jextract.VipsRaw.C_LONG;
411

512
/**
613
* Represents a VipsBlob, boxed to avoid exposing its raw MemorySegment
714
*/
815
public final class VBlob {
916

17+
private final Arena arena;
1018
final MemorySegment address;
1119

12-
VBlob(MemorySegment address) throws VipsError {
20+
VBlob(
21+
Arena arena,
22+
MemorySegment address
23+
) throws VipsError {
1324
if (!VipsValidation.isValidPointer(address)) {
1425
throw new VipsError("invalid pointer used for creation");
1526
}
27+
this.arena = arena;
1628
this.address = address;
1729
}
1830

@@ -24,4 +36,18 @@ public final class VBlob {
2436
public MemorySegment getUnsafeAddress() {
2537
return this.address;
2638
}
39+
40+
public long byteSize() {
41+
return VipsArea.length(this.address);
42+
}
43+
44+
public ByteBuffer asByteBuffer() {
45+
var lengthOutPointer = arena.allocate(C_LONG);
46+
var dataPointer = VipsRaw.vips_area_get_data(this.address, lengthOutPointer, MemorySegment.NULL, MemorySegment.NULL, MemorySegment.NULL);
47+
var length = lengthOutPointer.get(C_LONG, 0);
48+
if (length < 0) {
49+
throw new VipsError("unexpected length of vblob data " + length);
50+
}
51+
return dataPointer.asSlice(0, length).asByteBuffer();
52+
}
2753
}

0 commit comments

Comments
 (0)