Skip to content

Commit

Permalink
Fix Java API calls to allow execution on Java 1.7
Browse files Browse the repository at this point in the history
Thank you, animal-sniffer.
  • Loading branch information
kohlschuetter committed Sep 26, 2023
1 parent 59df98b commit 2ebe971
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 19 deletions.
6 changes: 6 additions & 0 deletions junixsocket-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-annotations</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2 changes: 2 additions & 0 deletions junixsocket-common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* The common junixsocket classes.
*/
// NOPMD -- https://github.com/pmd/pmd/issues/4620
@SuppressWarnings("module")
module org.newsclub.net.unix {
exports org.newsclub.net.unix;

requires java.base;
requires static java.rmi;
requires static transitive com.kohlschutter.annotations.compiletime;
requires static org.eclipse.jdt.annotation;
requires static animal.sniffer.annotations;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.io.InputStream;
import java.io.OutputStream;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/**
* An {@link OutputStream} for {@link AFSocket}, etc.
*
* @author Christian Kohlschütter
*/
@IgnoreJRERequirement // see src/main/java8
public abstract class AFOutputStream extends OutputStream implements FileDescriptorAccess {
AFOutputStream() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public static AFSYSTEMSocketAddress of(URI uri, int overridePort) throws SocketE
for (String p : host.split("\\.")) {
int v;
try {
v = Integer.parseUnsignedInt(p);
v = parseUnsignedInt(p, 10);
} catch (NumberFormatException e) {
throw (SocketException) new SocketException("Unsupported URI: " + uri).initCause(e);
}
Expand Down Expand Up @@ -396,7 +396,7 @@ public URI toURI(String scheme, URI template) throws IOException {
ByteBuffer bb = ByteBuffer.wrap(getBytes());
StringBuilder sb = new StringBuilder();
while (bb.remaining() > 0) {
sb.append(Integer.toUnsignedString(bb.getInt()));
sb.append(toUnsignedString(bb.getInt()));
if (bb.remaining() > 0) {
sb.append('.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -41,9 +42,10 @@ final class AFSelector extends AbstractSelector {
private final ByteBuffer pipeMsgWakeUp = ByteBuffer.allocate(1);
private final ByteBuffer pipeMsgReceiveBuffer = ByteBuffer.allocateDirect(256);

private final Set<AFSelectionKey> keysRegistered = ConcurrentHashMap.newKeySet();
private final Map<AFSelectionKey, Boolean> keysRegistered = new ConcurrentHashMap<>();
private final Set<AFSelectionKey> keysRegisteredKeySet = keysRegistered.keySet();
private final Set<SelectionKey> keysRegisteredPublic = Collections.unmodifiableSet(
keysRegistered);
keysRegisteredKeySet);

private final Set<SelectionKey> selectedKeysSet = new HashSet<>();
private final Set<SelectionKey> selectedKeysPublic = new UngrowableSet<>(selectedKeysSet);
Expand All @@ -62,7 +64,7 @@ protected SelectionKey register(AbstractSelectableChannel ch, int ops, Object at
AFSelectionKey key = new AFSelectionKey(this, ch, ops, att);
synchronized (this) {
pollFd = null;
keysRegistered.add(key);
keysRegistered.put(key, Boolean.TRUE);
}
return key;
}
Expand Down Expand Up @@ -193,7 +195,7 @@ private synchronized void setOpsReady(PollFd pfd) {
@SuppressWarnings({"resource", "PMD.CognitiveComplexity"})
private PollFd initPollFd(PollFd existingPollFd) throws IOException {
synchronized (this) {
for (Iterator<AFSelectionKey> it = keysRegistered.iterator(); it.hasNext();) {
for (Iterator<AFSelectionKey> it = keysRegisteredKeySet.iterator(); it.hasNext();) {
AFSelectionKey key = it.next();
if (!key.getAFCore().fd.valid() || key.hasOpInvalid()) {
key.cancelNoRemove();
Expand All @@ -209,7 +211,7 @@ private PollFd initPollFd(PollFd existingPollFd) throws IOException {
(existingPollFd.keys.length - 1) == keysRegistered.size()) {
boolean needsUpdate = false;
int i = 1;
for (AFSelectionKey key : keysRegistered) {
for (AFSelectionKey key : keysRegisteredKeySet) {
if (existingPollFd.keys[i] != key || !key.isValid()) { // NOPMD
needsUpdate = true;
break;
Expand All @@ -225,7 +227,7 @@ private PollFd initPollFd(PollFd existingPollFd) throws IOException {
}

int keysToPoll = keysRegistered.size();
for (AFSelectionKey key : keysRegistered) {
for (AFSelectionKey key : keysRegisteredKeySet) {
if (!key.isValid()) {
keysToPoll--;
}
Expand All @@ -240,7 +242,7 @@ private PollFd initPollFd(PollFd existingPollFd) throws IOException {
ops[0] = SelectionKey.OP_READ;

int i = 1;
for (AFSelectionKey key : keysRegistered) {
for (AFSelectionKey key : keysRegisteredKeySet) {
if (!key.isValid()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,64 @@ private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeUTF(addressFamily == null ? "undefined" : addressFamily.getJuxString());
}

/**
* Returns a string representation of the argument as an unsigned decimal value.
* <p>
* Works like {@link Integer#toUnsignedString(int)}; added to allow execution on Java 1.7.
*
* @param i The value.
* @return The string.
*/
protected static String toUnsignedString(int i) {
return Long.toString(toUnsignedLong(i));
}

/**
* Returns a string representation of the first argument as an unsigned integer value in the radix
* specified by the second argument; added to allow execution on Java 1.7.
*
* @param i The value.
* @param radix The radix.
* @return The string.
*/
protected static String toUnsignedString(int i, int radix) {
return Long.toUnsignedString(toUnsignedLong(i), radix);
}

private static long toUnsignedLong(long x) {
return x & 0xffffffffL;
}

/**
* Parses the string argument as an unsigned integer in the radix specified by the second
* argument. Works like {@link Integer#parseUnsignedInt(String, int)}; added to allow execution on
* Java 1.7.
*
* @param s The string.
* @param radix The radix.
* @return The integer.
* @throws NumberFormatException on parse error.
*/
protected static int parseUnsignedInt(String s, int radix) throws NumberFormatException {
if (s == null || s.isEmpty()) {
throw new NumberFormatException("Cannot parse null or empty string");
}

int len = s.length();
if (s.startsWith("-")) {
throw new NumberFormatException("Illegal leading minus sign on unsigned string " + s);
}

if (len <= 5 || (radix == 10 && len <= 9)) {
return Integer.parseInt(s, radix);
} else {
long ell = Long.parseLong(s, radix);
if ((ell & 0xffff_ffff_0000_0000L) == 0) {
return (int) ell;
} else {
throw new NumberFormatException("String value exceeds " + "range of unsigned int: " + s);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public static final class AddressType extends NamedInteger {

private AddressType(int id) {
super(id);
this.ds = (a, b, c) -> ":" + Integer.toUnsignedString(a) + ":" + Integer.toUnsignedString(b)
+ ":" + Integer.toUnsignedString(c);
this.ds = (a, b, c) -> ":" + toUnsignedString(a) + ":" + toUnsignedString(b) + ":"
+ toUnsignedString(c);
}

private AddressType(String name, int id, DebugStringProvider ds) {
Expand Down Expand Up @@ -431,9 +431,9 @@ public static AFTIPCSocketAddress ofTopologyService() throws SocketException {

private static int parseUnsignedInt(String v) {
if (v.startsWith("0x")) {
return Integer.parseUnsignedInt(v.substring(2), 16);
return parseUnsignedInt(v.substring(2), 16);
} else {
return Integer.parseUnsignedInt(v);
return parseUnsignedInt(v, 10);
}
}

Expand Down Expand Up @@ -686,9 +686,9 @@ protected Set<String> uriSchemes() {

private String toTipcInt(int v) {
if (v < 0) {
return "0x" + Integer.toUnsignedString(v, 16);
return "0x" + toUnsignedString(v, 16);
} else {
return Integer.toUnsignedString(v);
return toUnsignedString(v);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public URI toURI(String scheme, URI template) throws IOException {
portStr = "any";
break;
default:
portStr = Integer.toUnsignedString(port);
portStr = toUnsignedString(port);
break;
}

Expand All @@ -540,7 +540,7 @@ public URI toURI(String scheme, URI template) throws IOException {
cidStr = "host";
break;
default:
cidStr = Integer.toUnsignedString(cid);
cidStr = toUnsignedString(cid);
break;
}

Expand All @@ -551,11 +551,11 @@ public URI toURI(String scheme, URI template) throws IOException {

private static int parseInt(String v) {
if (v.startsWith("0x")) {
return Integer.parseUnsignedInt(v.substring(2), 16);
return parseUnsignedInt(v.substring(2), 16);
} else if (v.startsWith("-")) {
return Integer.parseInt(v);
} else {
return Integer.parseUnsignedInt(v);
return parseUnsignedInt(v, 10);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.IOException;
import java.lang.ref.Cleaner;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/**
* This wrapper (along with the Java 8-specific counterpart in src/main/java8) allows us to
* implement cleanup logic for objects that are garbage-collectable/no longer reachable.
Expand Down Expand Up @@ -49,6 +51,7 @@
*
* @author Christian Kohlschütter
*/
@IgnoreJRERequirement // see src/main/java8
abstract class CleanableState implements Closeable {
private static final Cleaner CLEANER = Cleaner.create();
private final Cleaner.Cleanable cleanable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.net.DatagramSocketImpl;
import java.net.SocketOption;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

@IgnoreJRERequirement // see src/main/java8
abstract class DatagramSocketShim extends DatagramSocket {

protected DatagramSocketShim(DatagramSocketImpl impl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.nio.file.FileSystems;
import java.nio.file.Path;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/**
* Some {@link Path}-related helper methods.
*
* @author Christian Kohlschütter
*/
@IgnoreJRERequirement // see src/main/java8
final class PathUtil {
private PathUtil() {
throw new IllegalStateException("No instances");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.net.SocketException;
import java.net.UnixDomainSocketAddress;

import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/**
* {@link SocketAddress}-related helper methods.
*
* @author Christian Kohlschütter
*/
@IgnoreJRERequirement // see src/main/java15
final class SocketAddressUtil {
private SocketAddressUtil() {
throw new IllegalStateException("No instances");
Expand Down

0 comments on commit 2ebe971

Please sign in to comment.