lechniak
lechniak

Reputation: 87

Inputstream.available() always returns 0

I got some stupid problem, I don't know what am i doing wrong.

I wrote client and server, client is working properly. I checked that output stream works properly in client there are bytes, but in server when a client connected, method in.avaible() returns always zero? Why?

SOme code of my server:

            try{
            serverSocket = new ServerSocket(port);
        }
        catch (IOException e){
            System.err.println("Could not listen on port: " + port);
            return false;
        }
        System.out.println("Server Started");
        txtServer.setText("Server wystartował");
        return true;
        }
        else{
        txtPort.setText("Brak Portu!");
        txtPort.setBorder( BorderFactory.createLineBorder(Color.RED) );
        return false;}
    }

    @Override
    public void run() { 
        try{
            clientSocket = serverSocket.accept();
            data.clear();

            System.out.println("Client connected");
            cl_obs = new Client_obs(clientSocket, data);
            Thread t = new Thread(cl_obs);
            t.start();
        }
        catch (IOException e){
            System.err.println("Accept failed.");
            System.exit(1);
        }           


    }
          package Server;
          import java.io.IOException;
          import java.io.InputStream;
          import java.net.Socket;

          public class Client_obs implements Runnable {
      private InputStream in;
      private data data;
      private Socket clientSocket = null;
      public Client_obs(Socket cl, data data1){
        clientSocket =cl;
        data = data1;
    }
    @Override
    public void run() {
        try {
            in = clientSocket.getInputStream();
            byte[] data1 = new byte[in.available()];;           
            for (int i=0; i<data1.length; i++){
            data1[i] = (byte)in.read();
            }
            data.setData(data1);
            data.displayMSG(data.getdata());
            in.close();
            clientSocket.close();

        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

}

Upvotes: 3

Views: 5927

Answers (3)

Oleg Tretiakov
Oleg Tretiakov

Reputation: 159

Instead of available() you can use reflection as it was provided here: Determine the size of an InputStream .

Simple recipe for ByteArrayInputStream:

ByteArrayInputStream wrapper = (ByteArrayInputStream)inputStream;       
Field field = ByteArrayInputStream.class.getDeclaredField("buf");
field.setAccessible(true);
byte[] buffer = (byte[])field.get(wrapper);
return buffer.length;

Upvotes: 0

micha
micha

Reputation: 49572

You should look at the documentation of the available method. For some implementations its not possible to know the exact number of bytes available. Therefore 0 is a valid result for those implementations:

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

...

The available method for class InputStream always returns 0.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198103

That's a perfectly legal implementation.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass' implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

The available method for class InputStream always returns 0.

Upvotes: 9

Related Questions