Skip to content

Commit

Permalink
Use StandardCharsets.UTF_8 instead of "UTF-8" where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Nov 10, 2023
1 parent 699e665 commit dfdc142
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.newsclub.net.unix.domain;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -32,7 +33,6 @@
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;

Expand Down Expand Up @@ -76,7 +76,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
socket.setOutboundFileDescriptors(FileDescriptor.in, FileDescriptor.err);
assertTrue(socket.hasOutboundFileDescriptors());
try (OutputStream outputStream = socket.getOutputStream()) {
outputStream.write("HELLO".getBytes("UTF-8"));
outputStream.write("HELLO".getBytes(UTF_8));
}
assertFalse(socket.hasOutboundFileDescriptors());

Expand All @@ -95,7 +95,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {

numRead = in.read(buf);
assertEquals(5, numRead, "'HELLO' is five bytes long");
assertEquals("HELLO", new String(buf, 0, numRead, "UTF-8"));
assertEquals("HELLO", new String(buf, 0, numRead, UTF_8));

fds = socket.getReceivedFileDescriptors();
assertEquals(2, fds.length, "Now, we should have two file descriptors");
Expand Down Expand Up @@ -294,12 +294,12 @@ public void testFileInputStream() throws Exception {
@Override
protected void handleConnection(final AFUNIXSocket socket) throws IOException {
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
fos.write("WORLD!".getBytes("UTF-8"));
fos.write("WORLD!".getBytes(UTF_8));
}
try (FileInputStream fin = new FileInputStream(tmpFile)) {
socket.setOutboundFileDescriptors(fin.getFD());
try (OutputStream outputStream = socket.getOutputStream()) {
outputStream.write("HELLO".getBytes("UTF-8"));
outputStream.write("HELLO".getBytes(UTF_8));
}
}

Expand All @@ -316,7 +316,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {

numRead = in.read(buf);
assertEquals(5, numRead, "'HELLO' is five bytes long");
assertEquals("HELLO", new String(buf, 0, numRead, "UTF-8"));
assertEquals("HELLO", new String(buf, 0, numRead, UTF_8));

fds = socket.getReceivedFileDescriptors();
assertEquals(1, fds.length, "Now, we should have two file descriptors");
Expand All @@ -325,7 +325,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
try (FileInputStream fin = new FileInputStream(fdesc)) {
numRead = fin.read(buf);
assertEquals(6, numRead, "'WORLD!' is six bytes long");
assertEquals("WORLD!", new String(buf, 0, numRead, "UTF-8"));
assertEquals("WORLD!", new String(buf, 0, numRead, UTF_8));
}
} finally {
Files.deleteIfExists(tmpFile.toPath());
Expand All @@ -343,15 +343,15 @@ public void testFileInputStreamPartiallyConsumed() throws Exception {
@Override
protected void handleConnection(final AFUNIXSocket socket) throws IOException {
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
fos.write("WORLD!".getBytes("UTF-8"));
fos.write("WORLD!".getBytes(UTF_8));
}
try (FileInputStream fin = new FileInputStream(tmpFile)) {
assertEquals('W', fin.read());

// We send the file descriptor of fin, from which we already consumed one byte.
socket.setOutboundFileDescriptors(fin.getFD());
try (OutputStream outputStream = socket.getOutputStream()) {
outputStream.write("HELLO".getBytes("UTF-8"));
outputStream.write("HELLO".getBytes(UTF_8));
}
}

Expand All @@ -368,7 +368,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {

numRead = in.read(buf);
assertEquals(5, numRead, "'HELLO' is five bytes long");
assertEquals("HELLO", new String(buf, 0, numRead, "UTF-8"));
assertEquals("HELLO", new String(buf, 0, numRead, UTF_8));

fds = socket.getReceivedFileDescriptors();
assertEquals(1, fds.length, "Now, we should have two file descriptors");
Expand All @@ -377,7 +377,7 @@ protected void handleConnection(final AFUNIXSocket socket) throws IOException {
try (FileInputStream fin = new FileInputStream(fdesc)) {
numRead = fin.read(buf);
assertEquals(5, numRead, "'ORLD!' is five bytes long");
assertEquals("ORLD!", new String(buf, 0, numRead, "UTF-8"));
assertEquals("ORLD!", new String(buf, 0, numRead, UTF_8));
}
} finally {
Files.deleteIfExists(tmpFile.toPath());
Expand Down Expand Up @@ -414,16 +414,16 @@ public void testDatagramSocket() throws Exception {
assertEquals(1, fds.length);

try (FileOutputStream fos2 = new FileOutputStream(fds[0])) {
fos2.write("Hello".getBytes(StandardCharsets.UTF_8));
fos2.write("Hello".getBytes(UTF_8));
// closing the received file descriptor will not close the original one ...
}

// ... which is why we can append the data here
fos.write("World".getBytes(StandardCharsets.UTF_8));
fos.write("World".getBytes(UTF_8));
}

try (FileInputStream fin = new FileInputStream(tmpOut)) {
String text = new String(IOUtil.readAllBytes(fin), StandardCharsets.UTF_8);
String text = new String(IOUtil.readAllBytes(fin), UTF_8);
// ... and the final output will contain both parts
assertEquals("HelloWorld", text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
Expand Down Expand Up @@ -56,10 +57,10 @@ public static void main(String[] args) throws IOException {
byte[] buf = new byte[128];

int read = is.read(buf);
System.out.println("Server says: " + new String(buf, 0, read, "UTF-8"));
System.out.println("Server says: " + new String(buf, 0, read, StandardCharsets.UTF_8));

System.out.println("Replying to server...");
os.write("Hello Server".getBytes("UTF-8"));
os.write("Hello Server".getBytes(StandardCharsets.UTF_8));
os.flush();

System.out.println("Now reading numbers from the server...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import org.newsclub.net.unix.AFUNIXServerSocket;
import org.newsclub.net.unix.AFUNIXSocket;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static void main(String[] args) throws IOException {
}

System.out.println("Saying hello to client " + sock);
os.write("Hello, dear Client".getBytes("UTF-8"));
os.write("Hello, dear Client".getBytes(StandardCharsets.UTF_8));
os.flush();

byte[] buf = new byte[128];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.rmi.RemoteException;

import org.newsclub.net.unix.demo.rmi.services.StreamService;
import org.newsclub.net.unix.rmi.AFUNIXRMISocketFactory;
Expand All @@ -38,17 +37,16 @@
*
* @author Christian Kohlschütter
*/
public final class StreamServiceImpl implements StreamService, Closeable {
public class StreamServiceImpl implements StreamService, Closeable {
private final AFUNIXRMISocketFactory socketFactory;

/**
* Creates a new instance.
*
* @param socketFactory The socket factory to use.
* @throws RemoteException on error.
*/
@SuppressFBWarnings("EI_EXPOSE_REP")
public StreamServiceImpl(AFUNIXRMISocketFactory socketFactory) throws RemoteException {
public StreamServiceImpl(AFUNIXRMISocketFactory socketFactory) {
this.socketFactory = socketFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;

import org.newsclub.net.unix.AFUNIXSocket;

Expand Down Expand Up @@ -55,7 +56,7 @@ private void doServeSocket(AFUNIXSocket socket) throws IOException {
OutputStream os = socket.getOutputStream();
FileInputStream fin = new FileInputStream(file)) {
socket.setOutboundFileDescriptors(fin.getFD());
os.write("FD sent via ancillary message.".getBytes("UTF-8"));
os.write("FD sent via ancillary message.".getBytes(StandardCharsets.UTF_8));
}
}
}

0 comments on commit dfdc142

Please sign in to comment.