Files
libsignal/rust/bridge/jni/bin/Native.java.in
2025-04-21 16:29:56 -04:00

140 lines
5.1 KiB
Java

//
// Copyright (C) 2020-2021 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
// WARNING: this file was automatically generated
package org.signal.libsignal.internal;
import org.signal.libsignal.protocol.message.CiphertextMessage;
import org.signal.libsignal.protocol.state.IdentityKeyStore;
import org.signal.libsignal.protocol.state.SessionStore;
import org.signal.libsignal.protocol.state.PreKeyStore;
import org.signal.libsignal.protocol.state.SignedPreKeyStore;
import org.signal.libsignal.protocol.state.KyberPreKeyStore;
import org.signal.libsignal.protocol.SignedPublicPreKey;
import org.signal.libsignal.protocol.groups.state.SenderKeyStore;
import org.signal.libsignal.protocol.logging.Log;
import org.signal.libsignal.protocol.logging.SignalProtocolLogger;
import org.signal.libsignal.net.internal.BridgeChatListener;
import org.signal.libsignal.net.internal.ConnectChatBridge;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Future;
import java.util.UUID;
import java.util.Map;
public final class Native {
private static Path tempDir;
private static void copyToTempDirAndLoad(InputStream in, String name) throws IOException {
// This isn't thread-safe but that's okay because it's only ever called from
// static initializers, which are themselves thread-safe.
if (tempDir == null) {
tempDir = Files.createTempDirectory("libsignal");
tempDir.toFile().deleteOnExit();
}
File tempFile = Files.createFile(tempDir.resolve(name)).toFile();
tempFile.deleteOnExit();
try (OutputStream out = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[4096];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
System.load(tempFile.getAbsolutePath());
}
/**
* If the library is embedded within this jar as a resource file, attempt to
* copy it to a temporary file and then load it. This allows the jar to be
* used even without a shared library existing on the filesystem.
*
* If a version of the library that includes this system's hardware architecture in its name is
* present, prefer that to the supplied name (e.g. "libsignal_amd64.so" will be preferred to
* "libsignal.so"). This applies only to libraries embedded as a resource, not libraries
* installed on the local machine.
*
* Package-private to allow the NativeTest class to load its shared library.
* This method should only be called from a static initializer.
*/
private static void loadLibrary(String name) throws IOException {
String arch = System.getProperty("os.arch");
// Special-case: some Java implementations use "x86_64", but OpenJDK uses "amd64".
if ("x86_64".equals(arch)) {
arch = "amd64";
}
for (String suffix : new String[]{ "_" + arch, "" }) {
final String libraryName = System.mapLibraryName(name + suffix);
try (InputStream in = Native.class.getResourceAsStream("/" + libraryName)) {
if (in != null) {
copyToTempDirAndLoad(in, libraryName);
return;
}
}
}
System.loadLibrary(name);
}
private static void loadNativeCode() {
try {
// First try to load the testing library. This will only succeed when
// libsignal is being used in a test context. The testing library
// contains a superset of the functionality of the non-test library, so if
// it gets loaded successfully, we're done.
loadLibrary("signal_jni_testing");
return;
} catch (Throwable e) {
// The testing library wasn't available. This is expected for production
// builds, so no error handling is needed. We'll try to load the non-test
// library next.
}
try {
loadLibrary("signal_jni");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static {
loadNativeCode();
initializeLibrary();
}
/**
* Ensures that the static initializer for this class gets run.
*/
static void ensureLoaded() {}
private Native() {}
/**
* Keeps an object from being garbage-collected until this call completes.
*
* This can be used to keep a Java wrapper around a Rust object handle alive while
* earlier calls use that Rust object handle. That is, you should call {@code keepAlive}
* <em>after</em> the code where an object must not be garbage-collected.
* However, most of the time {@link NativeHandleGuard} is a better choice,
* since the lifetime of the guard is clear.
*
* Effectively equivalent to Java 9's <a href="https://docs.oracle.com/javase/9/docs/api/java/lang/ref/Reference.html#reachabilityFence-java.lang.Object-"><code>reachabilityFence()</code></a>.
* Uses {@code native} because the JVM can't look into the implementation of the method
* and optimize away the use of {@code obj}. (The actual implementation does nothing.)
*/
public static native void keepAlive(Object obj);
// INSERT DECLS HERE
}