Skip to content

Commit 593c32a

Browse files
authored
Add cloned method to VBlob, add module info (#79)
1 parent b3e2763 commit 593c32a

File tree

10 files changed

+157
-29
lines changed

10 files changed

+157
-29
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.9")
23+
implementation("app.photofox.vips-ffm:vips-ffm-core:0.5.10")
2424
}
2525
```
2626
When running your project you must add: `--enable-native-access=ALL-UNNAMED` to your JVM runtime arguments. If you

core/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ tasks.withType<JavaExec>().configureEach {
6363
javaLauncher.set(project.javaToolchains.launcherFor(java.toolchain))
6464
}
6565

66-
tasks.withType<Jar>().configureEach {
67-
manifest {
68-
attributes("Automatic-Module-Name" to "app.photofox.vipsffm")
69-
}
70-
}
71-
7266
tasks.withType<Javadoc> {
7367
(options as StandardJavadocDocletOptions).tags("optionalArg")
7468
}

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

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,38 @@ public final class VBlob {
2929
}
3030

3131
/**
32-
* Gets the raw [MemorySegment] (C pointer) for this blob
33-
* The memory address' lifetime is bound to the scope of the [arena]
34-
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with [VipsHelper]
32+
* @deprecated Replaced by {@link #getUnsafeStructAddress()}
3533
*/
36-
public MemorySegment getUnsafeAddress() {
34+
@Deprecated(since = "0.5.10", forRemoval = true)
35+
public MemorySegment getUnsafeAddress() throws VipsError {
36+
return this.getUnsafeStructAddress();
37+
}
38+
39+
/**
40+
* Gets the raw {@link MemorySegment} (C pointer) for this VipsBlob struct
41+
* You might want the data address instead {@link #getUnsafeDataAddress()}
42+
* The memory address' lifetime is bound to the scope of the {@link #arena}
43+
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing and you
44+
* need to use it with {@link VipsHelper}
45+
*/
46+
public MemorySegment getUnsafeStructAddress() throws VipsError {
3747
return this.address;
3848
}
3949

4050
/**
4151
* Not recommended for use, use {@link #asByteBuffer()} instead
42-
* Gets the raw [MemorySegment] (C pointer) for the data in this blob
52+
* Gets the raw {@link MemorySegment} (C pointer) for the data in this blob
4353
* Sliced to the length of the data, which isn't always null terminated
4454
*/
45-
public MemorySegment getUnsafeDataAddress() {
55+
public MemorySegment getUnsafeDataAddress() throws VipsError {
4656
var lengthOutPointer = arena.allocate(C_LONG);
47-
var dataPointer = VipsRaw.vips_area_get_data(this.address, lengthOutPointer, MemorySegment.NULL, MemorySegment.NULL, MemorySegment.NULL);
57+
var dataPointer = VipsRaw.vips_area_get_data(
58+
this.address,
59+
lengthOutPointer,
60+
MemorySegment.NULL,
61+
MemorySegment.NULL,
62+
MemorySegment.NULL
63+
);
4864
var length = lengthOutPointer.get(C_LONG, 0);
4965
if (length < 0) {
5066
throw new VipsError("unexpected length of vblob data " + length);
@@ -60,10 +76,35 @@ public long byteSize() {
6076
}
6177

6278
/**
63-
* ByteBuffer representation of the data in this blob
64-
* Likely mapped to native memory, hence does not make a copy
79+
* @deprecated Replaced by {@link #asArenaScopedByteBuffer()}
6580
*/
81+
@Deprecated(since = "0.5.10", forRemoval = true)
6682
public ByteBuffer asByteBuffer() {
83+
return this.asArenaScopedByteBuffer();
84+
}
85+
86+
/**
87+
* ByteBuffer representation of the data in this blob
88+
* Mapped to native memory via {@link java.nio.DirectByteBuffer}, hence does not make a copy, so the data has the
89+
* same data lifetime as {@link #arena}
90+
*/
91+
public ByteBuffer asArenaScopedByteBuffer() {
6792
return this.getUnsafeDataAddress().asByteBuffer();
6893
}
94+
95+
/**
96+
* ByteBuffer representation of the data in this blob
97+
* A full copy of the data is taken, so that its contents are not coupled to the scope of {@link #arena}
98+
*/
99+
public ByteBuffer asClonedByteBuffer() {
100+
var buffer = this.asArenaScopedByteBuffer();
101+
if (buffer.capacity() == 0) {
102+
return ByteBuffer.allocate(0);
103+
}
104+
buffer.rewind();
105+
var newBuffer = ByteBuffer.allocate(buffer.capacity());
106+
buffer.put(newBuffer);
107+
newBuffer.rewind();
108+
return newBuffer;
109+
}
69110
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import app.photofox.vipsffm.enums.VipsOperationMorphology;
1616
import app.photofox.vipsffm.enums.VipsOperationRelational;
1717
import app.photofox.vipsffm.enums.VipsOperationRound;
18+
import java.lang.Deprecated;
1819
import java.lang.Double;
1920
import java.lang.Integer;
2021
import java.lang.Object;
@@ -57,11 +58,22 @@ public boolean equals(Object o) {
5758
}
5859

5960
/**
60-
* Gets the raw [MemorySegment] (C pointer) for this image
61-
* The memory address' lifetime is bound to the scope of the [arena]
62-
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with [VipsHelper]
61+
* @deprecated See {@link #getUnsafeStructAddress}
6362
*/
63+
@Deprecated(
64+
since = "0.5.10",
65+
forRemoval = true
66+
)
6467
public MemorySegment getUnsafeAddress() {
68+
return this.getUnsafeStructAddress();
69+
}
70+
71+
/**
72+
* Gets the raw {@link MemorySegment} (C pointer) for this VipsImage struct
73+
* The memory address' lifetime is bound to the scope of the {@link #arena}
74+
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with {@link VipsHelper}
75+
*/
76+
public MemorySegment getUnsafeStructAddress() {
6577
return this.address;
6678
}
6779

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public int hashCode() {
2828
return Objects.hashCode(address);
2929
}
3030

31+
/**
32+
* Gets the raw {@link MemorySegment} (C pointer) for this VipsInterpolate struct
33+
* The memory address' lifetime is bound to the scope of the arena that created it
34+
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing, and
35+
* you need to use it with {@link VipsHelper}
36+
*/
37+
public MemorySegment getUnsafeStructAddress() {
38+
return this.address;
39+
}
40+
3141
public static VInterpolate newFromName(Arena arena, String name) throws VipsError {
3242
var address = VipsHelper.interpolate_new(arena, name);
3343
return new VInterpolate(address);

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,37 @@ public final class VSource {
1717
this.address = address;
1818
}
1919

20+
@Override
21+
public boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (!(o instanceof VSource vSource)) return false;
24+
25+
return address.equals(vSource.address);
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return address.hashCode();
31+
}
32+
2033
/**
21-
* Gets the raw [MemorySegment] (C pointer) for this source
22-
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with [VipsHelper]
34+
* @deprecated See {@link #getUnsafeStructAddress}
2335
*/
36+
@Deprecated(
37+
since = "0.5.10",
38+
forRemoval = true
39+
)
2440
public MemorySegment getUnsafeAddress() {
41+
return this.getUnsafeStructAddress();
42+
}
43+
44+
/**
45+
* Gets the raw {@link MemorySegment} (C pointer) for this VipsSource struct
46+
* The memory address' lifetime is bound to the scope of the arena that created it
47+
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing, and
48+
* you need to use it with {@link VipsHelper}
49+
*/
50+
public MemorySegment getUnsafeStructAddress() {
2551
return this.address;
2652
}
2753

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,37 @@ public class VTarget {
1717
this.address = address;
1818
}
1919

20+
@Override
21+
public final boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (!(o instanceof VTarget vTarget)) return false;
24+
25+
return address.equals(vTarget.address);
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return address.hashCode();
31+
}
32+
2033
/**
21-
* Gets the raw [MemorySegment] (C pointer) for this target
22-
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with [VipsHelper]
34+
* @deprecated See {@link #getUnsafeStructAddress}
2335
*/
36+
@Deprecated(
37+
since = "0.5.10",
38+
forRemoval = true
39+
)
2440
public MemorySegment getUnsafeAddress() {
41+
return this.getUnsafeStructAddress();
42+
}
43+
44+
/**
45+
* Gets the raw {@link MemorySegment} (C pointer) for this VipsTarget struct
46+
* The memory address' lifetime is bound to the scope of the arena that created it
47+
* Usage of the memory address is strongly discouraged, but it is available if some functionality is missing, and
48+
* you need to use it with {@link VipsHelper}
49+
*/
50+
public MemorySegment getUnsafeStructAddress() {
2551
return this.address;
2652
}
2753

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module app.photofox.vipffm {
2+
exports app.photofox.vipsffm;
3+
exports app.photofox.vipsffm.enums;
4+
exports app.photofox.vipsffm.jextract;
5+
}

generator/src/main/kotlin/vipsffm/GenerateVClasses.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vipsffm
33
import app.photofox.vipsffm.jextract.GEnumClass
44
import app.photofox.vipsffm.jextract.GEnumValue
55
import app.photofox.vipsffm.jextract.VipsRaw
6+
import com.squareup.javapoet.AnnotationSpec
67
import com.squareup.javapoet.ArrayTypeName
78
import com.squareup.javapoet.ClassName
89
import com.squareup.javapoet.CodeBlock
@@ -135,10 +136,22 @@ object GenerateVClasses {
135136
.addStatement("if (!(o instanceof \$T vImage)) return false", vimageType)
136137
.addStatement("return Objects.equals(arena, vImage.arena) && Objects.equals(address, vImage.address)")
137138
.build()
138-
val unsafeAddress = MethodSpec.methodBuilder("getUnsafeAddress")
139-
.addJavadoc("Gets the raw [MemorySegment] (C pointer) for this image")
140-
.addJavadoc("\nThe memory address' lifetime is bound to the scope of the [arena]")
141-
.addJavadoc("\nUsage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with [VipsHelper]")
139+
val unsafeDeprecatedAddress = MethodSpec.methodBuilder("getUnsafeAddress")
140+
.addJavadoc("@deprecated See {@link #getUnsafeStructAddress}")
141+
.addStatement("return this.getUnsafeStructAddress()")
142+
.returns(memorySegmentType)
143+
.addModifiers(Modifier.PUBLIC)
144+
.addAnnotation(
145+
AnnotationSpec.builder(ClassName.get("java.lang", "Deprecated"))
146+
.addMember("since", "\"0.5.10\"")
147+
.addMember("forRemoval", "true")
148+
.build()
149+
)
150+
.build()
151+
val unsafeStructAddress = MethodSpec.methodBuilder("getUnsafeStructAddress")
152+
.addJavadoc("Gets the raw {@link MemorySegment} (C pointer) for this VipsImage struct")
153+
.addJavadoc("\nThe memory address' lifetime is bound to the scope of the {@link #arena}")
154+
.addJavadoc("\nUsage of the memory address is strongly discouraged, but it is available if some functionality is missing and you need to use it with {@link VipsHelper}")
142155
.addStatement("return this.address")
143156
.returns(memorySegmentType)
144157
.addModifiers(Modifier.PUBLIC)
@@ -150,7 +163,8 @@ object GenerateVClasses {
150163
.addMethod(ctor)
151164
.addMethod(hashCode)
152165
.addMethod(equals)
153-
.addMethod(unsafeAddress)
166+
.addMethod(unsafeDeprecatedAddress)
167+
.addMethod(unsafeStructAddress)
154168
.addMethods(operationMethods)
155169
.addMethods(imageMethods)
156170
.addField(arenaType, "arena", Modifier.PRIVATE, Modifier.FINAL)

sample/src/main/kotlin/vipsffm/VBlobByteBufferSample.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object VBlobByteBufferSample: RunnableSample {
1717
.thumbnailImage(400)
1818

1919
val blob = image.jpegsaveBuffer()
20-
val bytes = blob.asByteBuffer()
20+
val bytes = blob.asClonedByteBuffer()
2121
val rawDataSegment = blob.unsafeDataAddress
2222
val rawByteSize = blob.byteSize().toInt()
2323

0 commit comments

Comments
 (0)