0

I'm asking out of a "this can't be right" kind of mindset. This is the Code I'm trying to write:

final Socket client;

    // Constructor
    public ClientHandler(Socket client)
    {
        this.client = client;
    }

    @Override
    public void run()
    {
        // Instantiating Readers and Writers
        InputStreamReader binaryReader;     //for primitive Datatypes
        DataOutputStream binaryWriter;      //for primitive Datatypes
        BufferedReader asciiReader;         //for ASCII Strings
        PrintWriter asciiWriter;            //for ASCII Strings

        // Assigning Readers and Writers
        boolean IOInitialized = false;
        while (!IOInitialized) {
            try {
                binaryReader = new InputStreamReader(client.getInputStream());
                binaryWriter = new DataOutputStream(client.getOutputStream());
                asciiReader = new BufferedReader(binaryReader);
                asciiWriter = new PrintWriter(client.getOutputStream());
                IOInitialized = true;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        //Testing
        asciiWriter.println("Testing Writer");
    }

I'm trying to initialize IOStreams from a Socket. Their initialisation requires a Try-Catch block, and using the new IOStreams requires a guarantee that they're initialized, which I solved with the while loop but the compiler doesn't accept this. When trying to use the asciiWriter to print something to the Client, the compiler tells me that "asciiWriter might not have been initialized". Encompassing the entire run()-method would solve the problem but seems like a very unclean solution. Is there any way I can get this to work in a better way?

Nils
  • 1
  • 1
  • Use a try-with-resources. – Boris the Spider Jun 05 '21 at 13:41
  • "*... the IDE doesn't accept this.*" - This is pretty irrelevant. What does the compiler say? – Turing85 Jun 05 '21 at 13:44
  • Sorry, I clarified the sentence in the question, I meant that the compiler doesn't accept it. – Nils Jun 05 '21 at 13:50
  • When I copied your code to an IDE - the one error displayed is the line `asciiWriter.println...` which "might not have been initialized"- is this your error? –  Jun 05 '21 at 13:53
  • Yes exactly, that's the error I'm trying to solve. I'll put that in the question aswell for clarification. – Nils Jun 05 '21 at 13:58
  • Assign `null` to it where you declare it. change `PrintWriter asciiWriter; ` to `PrintWriter asciiWriter = null;`. This at the least assigns *something* to the variable. – Hovercraft Full Of Eels Jun 05 '21 at 14:02
  • Thank you really much!! That solved the problem! – Nils Jun 05 '21 at 14:21
  • 1
    This is not a solution but a horribly kludge. When the attempt to create a stream already produces an exception, the chances that you can use them without problems are low. In the worst case, this code will loop infinitely. It’s also unclear why you create all these objects when you don’t use them. On the other hand, using multiple streams to write into the same channel will quickly lead to corrupted data. – Holger Jun 07 '21 at 11:43
  • Thank you really much @Holger ! I appreciate the indepth answer, since I'm a beginner it's hard to know these things. The code I've asked about is just the first step, I'll need the objects later on. The while loop is to prevent null pointers down the road. If you wouldn't mind, what would you recommend to do when using streams? Would it be better to have the entire code in a try block or even an aforementioned try-with-resources? Or is there a way of better exception handling? Thank you so much in advance! – Nils Jun 08 '21 at 12:16
  • 1
    You should not have multiple streams. In your example, they are exhibiting a completely wrong understanding, both, `binaryReader` and `asciiReader`, have a `Reader` type for Unicode character data and nothing else, not “for primitive Datatypes” and not limited to ASCII in any way. The only difference is that the latter is a *buffered* version of the former, so when you use the former, you’ll bypass the buffer and distort the data. And one side using a `DataOutputStream` to write data would be entirely incompatible to the other side using a `Reader` to read the data. You should not mix them up. – Holger Jun 08 '21 at 12:57
  • Thanks so much @Holger ! That makes a lot of sense, I'll definitely change that and try to reread about the different input/output streams. As for the while loop, do you have a suggestion on better handling the exceptions that could be caused from creating the streams? I'm really thankful for your help! – Nils Jun 08 '21 at 13:21
  • 1
    Don’t try to continue when `getInputStream()` or `getOutputStream()` throw an exception. Consider the entire `Socket` as being in an erroneous state and bail out. In case of a client, decide to try to connect again or to quit, in case of the server, wait for such a new attempt or proceed without that particular client. – Holger Jun 08 '21 at 13:31
  • Thank you @Holger ! I implemented that like that, the Client now quits if there's an exception. Thank you again really much for the help, I've been able to learn a lot! – Nils Jun 09 '21 at 12:39

0 Answers0