Skip to content

Commit

Permalink
FileDescriptors: Return empty array instead of null; code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Mar 11, 2022
1 parent b0060dc commit dda9ee4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public interface AFUNIXSocketExtensions {
* along with a call to {@link InputStream#read()}, etc.
*
* NOTE: Another call to this method will not return the same file descriptors again (most likely,
* {@code null} will be returned).
* an empty array will be returned).
*
* @return The file descriptors, or {@code null} if none were available.
* @return The file descriptors, or an empty array if none were available.
* @throws IOException if the operation fails.
*/
FileDescriptor[] getReceivedFileDescriptors() throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

class AncillaryDataSupport implements Closeable {
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
private static final FileDescriptor[] NO_FILE_DESCRIPTORS = new FileDescriptor[0];

protected final Map<FileDescriptor, Integer> openReceivedFileDescriptors = Collections
.synchronizedMap(new HashMap<FileDescriptor, Integer>());
Expand Down Expand Up @@ -106,19 +107,19 @@ final void clearReceivedFileDescriptors() {

FileDescriptor[] getReceivedFileDescriptors() {
if (receivedFileDescriptors.isEmpty()) {
return null;
return NO_FILE_DESCRIPTORS;
}
List<FileDescriptor[]> copy = new ArrayList<>(receivedFileDescriptors);
if (copy.isEmpty()) {
return null;
return NO_FILE_DESCRIPTORS;
}
receivedFileDescriptors.removeAll(copy);
int count = 0;
for (FileDescriptor[] fds : copy) {
count += fds.length;
}
if (count == 0) {
return null;
return NO_FILE_DESCRIPTORS;
}
FileDescriptor[] oneArray = new FileDescriptor[count];
int offset = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public ReadableByteChannel provideAs(FileDescriptorCast fdc,
});

addProvider(FileChannel.class, new CastingProvider<FileChannel>() {
@SuppressWarnings("resource")
@Override
public FileChannel provideAs(FileDescriptorCast fdc, Class<? super FileChannel> desiredType)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,23 @@ static void checkSupported() {

static native byte[] sockname(FileDescriptor fd, boolean peer);

static native long bind(final byte[] socketAddr, final FileDescriptor fd, final int options)
throws IOException;
static native long bind(byte[] socketAddr, FileDescriptor fd, int options) throws IOException;

static native void listen(final FileDescriptor fd, final int backlog) throws IOException;
static native void listen(FileDescriptor fd, int backlog) throws IOException;

static native boolean accept(final byte[] socketAddr, final FileDescriptor fdServer,
final FileDescriptor fd, long inode, int timeout) throws IOException;
static native boolean accept(byte[] socketAddr, FileDescriptor fdServer, FileDescriptor fd,
long inode, int timeout) throws IOException;

static native boolean connect(final byte[] socketAddr, final FileDescriptor fd, long inode)
static native boolean connect(byte[] socketAddr, FileDescriptor fd, long inode)
throws IOException;

static native boolean finishConnect(FileDescriptor fd) throws IOException;

static native void disconnect(final FileDescriptor fd) throws IOException;
static native void disconnect(FileDescriptor fd) throws IOException;

static native int socketStatus(final FileDescriptor fd) throws IOException;
static native int socketStatus(FileDescriptor fd) throws IOException;

static native Class<?> primaryType(final FileDescriptor fd) throws IOException;
static native Class<?> primaryType(FileDescriptor fd) throws IOException;

/**
* Reads data from an {@link AFUNIXSocketImpl}.
Expand All @@ -109,7 +108,7 @@ static native boolean connect(final byte[] socketAddr, final FileDescriptor fd,
* {@code buf} was {@code null}.
* @throws IOException upon error.
*/
static native int read(final FileDescriptor fd, byte[] buf, int off, int len,
static native int read(FileDescriptor fd, byte[] buf, int off, int len,
AncillaryDataSupport ancillaryDataSupport, int timeoutMillis) throws IOException;

/**
Expand All @@ -123,37 +122,37 @@ static native int read(final FileDescriptor fd, byte[] buf, int off, int len,
* @return The number of bytes written (which could be 0).
* @throws IOException upon error.
*/
static native int write(final FileDescriptor fd, byte[] buf, int off, int len,
static native int write(FileDescriptor fd, byte[] buf, int off, int len,
AncillaryDataSupport ancillaryDataSupport) throws IOException;

static native int receive(final FileDescriptor fd, ByteBuffer directBuffer, int offset,
int length, ByteBuffer directSocketAddressOut, int options,
AncillaryDataSupport ancillaryDataSupport, int timeoutMillis) throws IOException;
static native int receive(FileDescriptor fd, ByteBuffer directBuffer, int offset, int length,
ByteBuffer directSocketAddressOut, int options, AncillaryDataSupport ancillaryDataSupport,
int timeoutMillis) throws IOException;

static native int send(final FileDescriptor fd, ByteBuffer directBuffer, int offset, int length,
static native int send(FileDescriptor fd, ByteBuffer directBuffer, int offset, int length,
ByteBuffer directSocketAddress, int options, AncillaryDataSupport ancillaryDataSupport)
throws IOException;

static native void close(final FileDescriptor fd) throws IOException;
static native void close(FileDescriptor fd) throws IOException;

static native void shutdown(final FileDescriptor fd, int mode) throws IOException;
static native void shutdown(FileDescriptor fd, int mode) throws IOException;

static native int getSocketOptionInt(final FileDescriptor fd, int optionId) throws IOException;
static native int getSocketOptionInt(FileDescriptor fd, int optionId) throws IOException;

static native void setSocketOptionInt(final FileDescriptor fd, int optionId, int value)
static native void setSocketOptionInt(FileDescriptor fd, int optionId, int value)
throws IOException;

static native int available(final FileDescriptor fd) throws IOException;
static native int available(FileDescriptor fd) throws IOException;

static native AFUNIXSocketCredentials peerCredentials(FileDescriptor fd,
AFUNIXSocketCredentials creds) throws IOException;

static native void initServerImpl(final ServerSocket serverSocket, final AFUNIXSocketImpl impl)
static native void initServerImpl(ServerSocket serverSocket, AFUNIXSocketImpl impl)
throws IOException;

static native void createSocket(FileDescriptor fdesc, int type) throws IOException;

static native void setPort(final AFUNIXSocketAddress addr, int port);
static native void setPort(AFUNIXSocketAddress addr, int port);

static native void initFD(FileDescriptor fdesc, int fd) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
*/
package org.newsclub.net.unix;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void testBindBadArguments() throws Exception {
public void testReceivedFileDescriptorsUnconnected() throws Exception {
try (AFUNIXSocket sock = AFUNIXSocket.newInstance()) {
// We don't check socket status, so these calls are perfectly fine for unconnected sockets.
assertNull(sock.getReceivedFileDescriptors());
assertArrayEquals(new FileDescriptor[0], sock.getReceivedFileDescriptors());
sock.clearReceivedFileDescriptors();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
package org.newsclub.net.unix;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -73,7 +73,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
FileDescriptor[] fds;
int numRead;
fds = socket.getReceivedFileDescriptors();
assertNull(fds, "Initially, there are no file descriptors");
assertArrayEquals(new FileDescriptor[0], fds, "Initially, there are no file descriptors");

numRead = in.read(buf);
assertEquals(5, numRead, "'HELLO' is five bytes long");
Expand All @@ -83,13 +83,13 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
assertEquals(2, fds.length, "Now, we should have two file descriptors");

fds = socket.getReceivedFileDescriptors();
assertNull(fds, "If we ask again, these new file descriptors should be gone");
assertArrayEquals(new FileDescriptor[0], fds, "If we ask again, these new file descriptors should be gone");

try (InputStream is = socket.getInputStream()) {
numRead = is.read(buf);
assertEquals(-1, numRead, "There shouldn't be anything left to read");
fds = socket.getReceivedFileDescriptors();
assertNull(fds, "There shouldn't be any new file descriptors");
assertArrayEquals(new FileDescriptor[0], fds, "There shouldn't be any new file descriptors");
}
}
});
Expand Down Expand Up @@ -202,7 +202,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
} catch (SocketException e) {
// on Linux, a SocketException may be thrown (an ancillary message was sent, but not read)
}
assertNull(socket.getReceivedFileDescriptors());
assertArrayEquals(new FileDescriptor[0], socket.getReceivedFileDescriptors());
assertEquals(0, socket.getAncillaryReceiveBufferSize());
}
});
Expand Down Expand Up @@ -235,7 +235,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
try {
assertEquals(123, inputStream.read());
FileDescriptor[] fds = socket.getReceivedFileDescriptors();
if (fds != null) {
if (fds.length != 0) {
if (fds.length == 2) {
// space was sufficient
} else if (fds.length == 1) {
Expand All @@ -250,7 +250,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
} catch (SocketException e) {
// expected
}
assertNull(socket.getReceivedFileDescriptors());
assertArrayEquals(new FileDescriptor[0], socket.getReceivedFileDescriptors());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void shutdown() throws IOException {
* @param sock The socket to handle.
* @throws IOException upon error.
*/
protected abstract void handleConnection(final AFUNIXSocket sock) throws IOException;
protected abstract void handleConnection(AFUNIXSocket sock) throws IOException;

/**
* Called from within {@link #handleConnection(AFUNIXSocket)} to tell the server to no longer
Expand Down

0 comments on commit dda9ee4

Please sign in to comment.