(#QV6) Update on no http response sent

This is sort of an update on my first post, but I basically rewrote the basis of how the concurrency was done and modeled it after a code example. Basically when I try to create the file it appears to work as expected, and my logs show that the path is correct, the create file command runs, and the proper response is recorded, but the response itself doesn’t send back. I tried running a code example by itself but changed the http response a little and it showed that all the print statements showed before the http response and not after like it does in mine.
Any advice or help is greatly appreciated, thanks.

Here is my code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;;

public class Main {
public static void main(String args) {
// You can use print statements as follows for debugging, they’ll be visible when running tests.
ServerSocket serverSocket = null;
Socket clientSocket = null;
try{
String directory = null;
if(args.length==2 && args[0].equalsIgnoreCase(“–directory”))
directory = args[1];

serverSocket = new ServerSocket(4221);
  serverSocket.setReuseAddress(true);
  while (true) {
  clientSocket = serverSocket.accept();
    System.out.println("accepted new connection");
    Handler handler = new Handler(clientSocket.getInputStream(), clientSocket.getOutputStream(), directory);
    handler.start();
    System.out.println("done");
  }

}catch (IOException e)
{
System.out.println("IOE: " + e);
}
}
}

class Handler extends Thread{

private InputStream input;
private OutputStream outputStream;
private String directory;

public Handler(InputStream input, OutputStream outputStream, String directory)
{
this.input = input;
this.outputStream = outputStream;
this.directory = directory;

}

//request handler
public void run()
{
try{

boolean gotFile = false;
byte[] fileBytes = null;
  final BufferedReader reader = new BufferedReader(new InputStreamReader(input));

//read input and parse as string
//System.out.println(reader.readLine());
String s = reader.readLine();
while(reader.ready())
{
s+=reader.readLine();
}
System.out.println(“String:” + s);
String outString = “HTTP/1.1 404 Not Found\r\n\r\n”;

  //System.out.println(s.substring(0,4));
 
 //read and write data sent to server
  if(s.substring(0, 4).equalsIgnoreCase("POST"))
  {
  
   String d = directory;
   String name = s.substring(11, s.substring(12).indexOf(" ")+12);
  String body = s.substring(s.indexOf("application/octet-stream")+24);
  System.out.println("body: " + body);
  System.out.println(d+name);
  String fileName = d+name;
  //System.out.println(fileName);
    File file = new File(fileName);
    File dir = new File(directory);
    if (!dir.exists())
        dir.mkdirs();


    if(!file.exists()){
    file.createNewFile();
    try(FileWriter fileWriter = new FileWriter(file)){
      fileWriter.write(body);}
      System.out.println("finished file creation");
    outString = "HTTP/1.1 201 Created\r\n\r\n";
    }
   else
    outString = "HTTP/1.1 404 Not Found\r\n\r\n";
   
    System.out.println("Sending:" + outString);
   outputStream.write(outString.getBytes());
   outputStream.flush();
   outputStream.close();
   return;
  }
 
 
else if(s.charAt(5)==' ') // / url path
    outString = "HTTP/1.1 200 OK\r\n\r\n";




else if(s.substring(5, 9).equals("echo")) //echo string
    {
      String echoString ="";
      for(int i = 10; i<s.length(); i++)
      {
        if(s.charAt(i)==' ')
        break;
        echoString+=s.charAt(i);
      }
      System.out.println(echoString);
      outString = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: "+echoString.length()+"\r\n\r\n" + echoString;
    }




else if(s.substring(5, 15).equals("user-agent")) //user-agent header
    {
      String headerString = "";
      int startIndex = s.indexOf("User-Agent:");
      for(int i = startIndex+12; i<s.length(); i++)
      {
        if(s.charAt(i)=='\r')
        break;
        headerString+=s.charAt(i);
        //System.out.println(s.charAt(i)); //for debugging
      }
      System.out.println(headerString);
      System.out.println(headerString.length());
      outString = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: "+headerString.length()+"\r\n\r\n"+headerString ;
    }




else if(s.substring(5, 10).equals("files")) //files header
    {
      //get file name
      String fileString = "";
      int startIndex = s.indexOf("/files/");
      for(int i = startIndex+7; i<s.length(); i++)
      {
        if(s.charAt(i)==' ')
        break;
        fileString+=s.charAt(i);
        //System.out.println(s.charAt(i));
      }


      //get file path
      Path filePath = Paths.get(directory, fileString);
     
      System.out.println(directory+fileString);
        if(Files.exists(filePath ))
         {
           gotFile = true;
            fileBytes = Files.readAllBytes(filePath);
            outString = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: "+fileBytes.length+"\r\n\r\n" ;

        }
        else
        {
        outString = "HTTP/1.1 404 Not Found\r\n\r\n";
        }
      System.out.println(fileString);
      System.out.println(fileString.length());

    }

    System.out.println("outstrinf: " + outString);
    outputStream.write(outString.getBytes());




    if(gotFile)
    outputStream.write(fileBytes);
    outputStream.flush();
    outputStream.close();
  }
  catch(IOException e)
  {
    throw new RuntimeException(e);
  }

}

}

Here are my logs (I had to remove a link to let me post it):
remote: [compile] Compilation successful.
remote:
remote: [tester::#QV8] Running tests for Stage #QV8 (Read request body)
remote: [tester::#QV8] $ ./your_program.sh --directory /tmp/data/codecrafters.io/http-server-tester/
remote: [tester::#QV8] $ curl -v -X POST http://localhost:4221/files/mango_pear_banana_orange -H “Content-Length: 60” -H “Content-Type: application/octet-stream” -d ‘banana pear strawberry grape grape grape raspberry raspberry’
remote: [your_program] accepted new connection
remote: [your_program] done
remote: [tester::#QV8] Failed to read response:
remote: [tester::#QV8] Received: “” (no content received)
remote: [tester::#QV8] ^ error
remote: [tester::#QV8] Error: Expected: HTTP-version, Received: “”
remote: [tester::#QV8] Test failed (try setting ‘debug: true’ in your codecrafters.yml to see more details)
remote: [your_program] String:POST /files/mango_pear_banana_orange HTTP/1.1Host: localhost:4221Content-Length: 60Content-Type: application/octet-streambanana pear strawberry grape grape grape raspberry raspberry
remote: [your_program] body: banana pear strawberry grape grape grape raspberry raspberry
remote: [your_program] /tmp/data/codecrafters.io/http-server-tester//mango_pear_banana_orange
remote: [your_program] finished file creation
remote: [your_program] Sending Response: HTTP/1.1 201 Created
remote: [your_program]
remote: [your_program]
remote:
remote: Try our CLI to run tests faster without Git:
remote: