Skip to content

Commit

Permalink
Proper behavior with large files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Dec 11, 2024
1 parent ca04991 commit fd56cfe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/main/java/dev/lukebemish/immaculate/ForkFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -115,8 +116,13 @@ private SocketHandle(Socket socket) throws IOException {

synchronized void writeSubmission(int id, String fileName, String text) throws IOException {
output.writeInt(id);
output.writeUTF(fileName);
output.writeUTF(text);
// We do not use writeUTF here as it is limited to strings with length less than 65535 bytes
var fileNameBytes = fileName.getBytes(StandardCharsets.UTF_8);
output.writeInt(fileNameBytes.length);
output.write(fileNameBytes);
var textBytes = text.getBytes(StandardCharsets.UTF_8);
output.writeInt(textBytes.length);
output.write(textBytes);
output.flush();
}

Expand Down Expand Up @@ -153,7 +159,10 @@ boolean readSuccess() throws IOException {
}

String readResult() throws IOException {
return input.readUTF();
// We do not use readUTF here as it is limited to strings with length less than 65535 bytes
int length = input.readInt();
var bytes = input.readNBytes(length);
return new String(bytes, StandardCharsets.UTF_8);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Constructor;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -115,7 +116,10 @@ synchronized void writeFailure(int id) throws IOException {
synchronized void writeSuccess(int id, String result) throws IOException {
output.writeInt(id);
output.writeBoolean(true);
output.writeUTF(result);
// We do not use writeUTF here as it is limited to strings with length less than 65535 bytes
var resultBytes = result.getBytes(StandardCharsets.UTF_8);
output.writeInt(resultBytes.length);
output.write(resultBytes);
output.flush();
}

Expand All @@ -124,7 +128,10 @@ int readId() throws IOException {
}

String readUTF() throws IOException {
return input.readUTF();
// We do not use readUTF here as it is limited to strings with length less than 65535 bytes
int length = input.readInt();
var bytes = input.readNBytes(length);
return new String(bytes, StandardCharsets.UTF_8);
}
}
}

0 comments on commit fd56cfe

Please sign in to comment.