Skip to content

Commit

Permalink
Implement the AutoCloseable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jefdriesen committed Jun 3, 2024
1 parent 9850c04 commit 6692732
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 1 deletion.
7 changes: 7 additions & 0 deletions App.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,16 @@ public void Gasmix(int value) {
}
});

parser.close();

// Continue downloading dives.
return 1;
}
});

device.close();
iostream.close();
descriptor.close();
context.close();
}
}
8 changes: 8 additions & 0 deletions org/libdivecomputer/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Context extends Handle
{
private native long New();
private native long Free(long handle);
private native void SetLogLevel(long handle, int loglevel);
private native void SetLogFunc(long handle, LogFunc logfunc);

Expand Down Expand Up @@ -34,6 +35,13 @@ public void SetLogFunc(LogFunc logfunc)
SetLogFunc(handle, logfunc);
}

@Override
public void close()
{
Free(handle);
handle = 0;
}

static {
System.loadLibrary("divecomputer-java");
}
Expand Down
8 changes: 8 additions & 0 deletions org/libdivecomputer/Descriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public class Descriptor extends Handle
{
private native long Free(long handle);
private native String Vendor(long handle);
private native String Product(long handle);
private native int Type(long handle);
Expand Down Expand Up @@ -42,6 +43,13 @@ public int Transports()
return Transports(handle);
}

@Override
public void close()
{
Free(handle);
handle = 0;
}

static {
System.loadLibrary("divecomputer-java");
}
Expand Down
8 changes: 8 additions & 0 deletions org/libdivecomputer/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Device extends Handle
{
private native long Open(long context, long descriptor, long iostream);
private native long Close(long handle);
private native void Foreach(long handle, Callback callback);
private native void SetFingerprint(long handle, byte[] fingerprint);
private native void SetEvents(long handle, Events events);
Expand Down Expand Up @@ -49,6 +50,13 @@ public void SetCancel(Cancel cancel)
SetCancel(handle, cancel);
}

@Override
public void close()
{
Close(handle);
handle = 0;
}

static {
System.loadLibrary("divecomputer-java");
}
Expand Down
2 changes: 1 addition & 1 deletion org/libdivecomputer/Handle.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.libdivecomputer;

abstract class Handle
abstract class Handle implements AutoCloseable
{
public long handle = 0;
}
12 changes: 12 additions & 0 deletions org/libdivecomputer/IOStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@

public abstract class IOStream extends Handle
{
private native long Close(long handle);

@Override
public void close()
{
Close(handle);
handle = 0;
}

static {
System.loadLibrary("divecomputer-java");
}
}

8 changes: 8 additions & 0 deletions org/libdivecomputer/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Parser extends Handle
{
private native long New(long device, byte[] data);
private native long New2(long context, long descriptor, byte[] data);
private native long Free(long handle);
private native void Foreach(long handle, Callback callback);
private native void GetDatetime(long handle, Datetime datetime);
private native void GetSalinity(long handle, Salinity salinity);
Expand Down Expand Up @@ -185,6 +186,13 @@ public void Foreach(Callback callback)
Foreach(handle, callback);
}

@Override
public void close()
{
Free(handle);
handle = 0;
}

static {
System.loadLibrary("divecomputer-java");
}
Expand Down
6 changes: 6 additions & 0 deletions org_libdivecomputer_Context.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Context_New
return (jlong) context;
}

JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Context_Free
(JNIEnv *env, jobject obj, jlong handle)
{
dc_context_free ((dc_context_t *) handle);
}

JNIEXPORT void JNICALL Java_org_libdivecomputer_Context_SetLogLevel
(JNIEnv *env, jobject obj, jlong handle, jint loglevel)
{
Expand Down
8 changes: 8 additions & 0 deletions org_libdivecomputer_Context.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions org_libdivecomputer_Descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include <libdivecomputer/descriptor.h>

JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Descriptor_Free
(JNIEnv *env, jobject obj, jlong handle)
{
dc_descriptor_free ((dc_descriptor_t *) handle);
}

JNIEXPORT jstring JNICALL Java_org_libdivecomputer_Descriptor_Vendor
(JNIEnv *env, jobject obj, jlong handle)
{
Expand Down
8 changes: 8 additions & 0 deletions org_libdivecomputer_Descriptor.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions org_libdivecomputer_Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Device_Open
return (jlong) device;
}

JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Device_Close
(JNIEnv *env, jobject obj, jlong handle)
{
dc_device_close ((dc_device_t *) handle);
}

JNIEXPORT void JNICALL Java_org_libdivecomputer_Device_Foreach
(JNIEnv *env, jobject obj, jlong handle, jobject callback)
{
Expand Down
8 changes: 8 additions & 0 deletions org_libdivecomputer_Device.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions org_libdivecomputer_IOStream.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "org_libdivecomputer_IOStream.h"

#include <libdivecomputer/iostream.h>

JNIEXPORT jlong JNICALL Java_org_libdivecomputer_IOStream_Close
(JNIEnv *env, jobject obj, jlong handle)
{
dc_iostream_close ((dc_iostream_t *) handle);
}
21 changes: 21 additions & 0 deletions org_libdivecomputer_IOStream.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions org_libdivecomputer_Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Parser_New2
return (jlong) parser;
}

JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Parser_Free
(JNIEnv *env, jobject obj, jlong handle)
{
dc_parser_destroy ((dc_parser_t *) handle);
}

JNIEXPORT void JNICALL Java_org_libdivecomputer_Parser_Foreach
(JNIEnv *env, jobject obj, jlong handle, jobject callback)
{
Expand Down
8 changes: 8 additions & 0 deletions org_libdivecomputer_Parser.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6692732

Please sign in to comment.