Skip to content

Commit 37bb9f4

Browse files
committed
replace direct enum indexing with fromNative method for safer result handling
1 parent 7ce87e6 commit 37bb9f4

6 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/main/java/io/github/doblon8/openpnp/capture/CaptureContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public CaptureContext() {
1616
@Override
1717
public void close() throws CaptureException{
1818
int result = Cap_releaseContext(segment);
19-
CaptureResult captureResult = CaptureResult.values()[result];
19+
CaptureResult captureResult = CaptureResult.fromNative(result);
2020
if (captureResult != CaptureResult.OK) {
2121
throw new CaptureException("Error releasing capture context: " + result);
2222
}
@@ -25,4 +25,4 @@ public void close() throws CaptureException{
2525
MemorySegment getSegment() {
2626
return segment;
2727
}
28-
}
28+
}

src/main/java/io/github/doblon8/openpnp/capture/CaptureDevice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private CaptureFormatInfo getFormatInfo(int deviceId, int formatId) throws Captu
104104
try (Arena arena = Arena.ofConfined()) {
105105
MemorySegment formatInfoSegment = CapFormatInfo.allocate(arena);
106106
int result = Cap_getFormatInfo(context.getSegment(), deviceId, formatId, formatInfoSegment);
107-
CaptureResult captureResult = CaptureResult.values()[result];
107+
CaptureResult captureResult = CaptureResult.fromNative(result);
108108
if (captureResult == CaptureResult.OK) {
109109
return new CaptureFormatInfo(formatInfoSegment);
110110
}

src/main/java/io/github/doblon8/openpnp/capture/CaptureResult.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,14 @@ public enum CaptureResult {
55
ERROR,
66
DEVICE_NOT_FOUND,
77
FORMAT_NOT_SUPPORTED,
8-
PROPERTY_NOT_SUPPORTED,
8+
PROPERTY_NOT_SUPPORTED;
9+
10+
private static final CaptureResult[] VALUES = CaptureResult.values();
11+
12+
public static CaptureResult fromNative(int value) {
13+
if (value < 0 || value >= VALUES.length) {
14+
throw new CaptureException("Unknown capture result code: " + value);
15+
}
16+
return VALUES[value];
17+
}
918
}

src/main/java/io/github/doblon8/openpnp/capture/CaptureStream.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public BufferedImage capture() {
3333
MemorySegment bufferPointer = arena.allocate(ValueLayout.JAVA_BYTE, numBytes);
3434

3535
int result = Cap_captureFrame(context.getSegment(), id, bufferPointer, numBytes);
36-
CaptureResult captureResult = CaptureResult.values()[result];
36+
CaptureResult captureResult = CaptureResult.fromNative(result);
3737
if (captureResult != CaptureResult.OK) {
3838
throw new CaptureException("Error capturing frame: " + result);
3939
}
@@ -87,7 +87,7 @@ public PropertyLimits getPropertyLimits(CaptureProperty property) {
8787
MemorySegment defaultValuePointer = arena.allocate(ValueLayout.JAVA_INT);
8888

8989
int result = Cap_getPropertyLimits(context.getSegment(), id, property.value(), minPointer, maxPointer, defaultValuePointer);
90-
CaptureResult captureResult = CaptureResult.values()[result];
90+
CaptureResult captureResult = CaptureResult.fromNative(result);
9191
return switch (captureResult) {
9292
case OK -> new PropertyLimits(
9393
minPointer.get(ValueLayout.JAVA_INT, 0),
@@ -112,7 +112,7 @@ public int getProperty(CaptureProperty property) {
112112
try (Arena arena = Arena.ofConfined()) {
113113
MemorySegment valuePointer = arena.allocate(ValueLayout.JAVA_INT);
114114
int result = Cap_getProperty(context.getSegment(), id, property.value(), valuePointer);
115-
CaptureResult captureResult = CaptureResult.values()[result];
115+
CaptureResult captureResult = CaptureResult.fromNative(result);
116116
return switch (captureResult) {
117117
case OK -> valuePointer.get(ValueLayout.JAVA_INT, 0);
118118
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
@@ -130,7 +130,7 @@ public int getProperty(CaptureProperty property) {
130130
*/
131131
public void setProperty(CaptureProperty property, int value) {
132132
int result = Cap_setProperty(context.getSegment(), id, property.value(), value);
133-
CaptureResult captureResult = CaptureResult.values()[result];
133+
CaptureResult captureResult = CaptureResult.fromNative(result);
134134
switch (captureResult) {
135135
case OK -> {}
136136
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
@@ -149,7 +149,7 @@ public boolean getAutoProperty(CaptureProperty property) {
149149
try (Arena arena = Arena.ofConfined()) {
150150
MemorySegment valuePointer = arena.allocate(ValueLayout.JAVA_INT);
151151
int result = Cap_getAutoProperty(context.getSegment(), id, property.value(), valuePointer);
152-
CaptureResult captureResult = CaptureResult.values()[result];
152+
CaptureResult captureResult = CaptureResult.fromNative(result);
153153
return switch (captureResult) {
154154
case OK -> valuePointer.get(ValueLayout.JAVA_INT, 0) == 1;
155155
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
@@ -167,7 +167,7 @@ public boolean getAutoProperty(CaptureProperty property) {
167167
*/
168168
public void setAutoProperty(CaptureProperty property, boolean enable) {
169169
int result = Cap_setAutoProperty(context.getSegment(), id, property.value(), enable ? 1 : 0);
170-
CaptureResult captureResult = CaptureResult.values()[result];
170+
CaptureResult captureResult = CaptureResult.fromNative(result);
171171
switch (captureResult) {
172172
case OK -> {}
173173
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
@@ -193,7 +193,7 @@ public boolean isOpen() {
193193
@Override
194194
public void close() {
195195
int result = Cap_closeStream(context.getSegment(), id);
196-
CaptureResult captureResult = CaptureResult.values()[result];
196+
CaptureResult captureResult = CaptureResult.fromNative(result);
197197
if (captureResult != CaptureResult.OK) {
198198
throw new CaptureException("Error closing capture stream: " + result);
199199
}

src/main/java/io/github/doblon8/openpnp/capture/LogLevel.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,14 @@ public enum LogLevel {
99
NOTICE,
1010
INFO,
1111
DEBUG,
12-
VERBOSE,
12+
VERBOSE;
13+
14+
private static final LogLevel[] VALUES = values();
15+
16+
public static LogLevel fromNative(int value) {
17+
if (value < 0 || value >= VALUES.length) {
18+
throw new IllegalArgumentException("Unknown log level: " + value);
19+
}
20+
return VALUES[value];
21+
}
1322
}

src/main/java/io/github/doblon8/openpnp/capture/OpenPnpCapture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void setLogLevel(LogLevel level) {
5959
*/
6060
public static void installCustomLogFunction(BiConsumer<LogLevel, String> logFunction) {
6161
MemorySegment functionPointer = CapCustomLogFunc.allocate((level, stringPointer) ->
62-
logFunction.accept(LogLevel.values()[level], stringPointer.getString(0)), arena);
62+
logFunction.accept(LogLevel.fromNative(level), stringPointer.getString(0)), arena);
6363
Cap_installCustomLogFunction(functionPointer);
6464
}
6565

0 commit comments

Comments
 (0)