Can't get InputStream read to block...

Posted by mark dufresne on Stack Overflow See other posts from Stack Overflow or by mark dufresne
Published on 2010-05-29T02:16:11Z Indexed on 2010/05/29 2:22 UTC
Read the original article Hit count: 302

Filed under:
|
|

I would like the input stream read to block instead of reading end of stream (-1). Is there a way to configure the stream to do this? Here's my Servlet code:

    PrintWriter out = response.getWriter();
    BufferedReader in = request.getReader();
    try {
        String line;
        int loop = 0;
        while (loop < 20) {
            line = in.readLine();
            lgr.log(Level.INFO, line);
            out.println("<" + loop + "html>");

            Thread.sleep(1000);
            loop++;
            //
        }
    } catch (InterruptedException ex) {
        lgr.log(Level.SEVERE, null, ex);
    } finally {
        out.close();
    }

Here's my Midlet code:

    private HttpConnection conn;
    InputStream is;
    OutputStream os;
    private boolean exit = false;

    public void run() {

        String url = "http://localhost:8080/WebApplication2/NewServlet";
        try {
            conn =
                    (HttpConnection) Connector.open(url);
            is = conn.openInputStream();
            os = conn.openOutputStream();
            StringBuffer sb = new StringBuffer();
            int c;

            while (!exit) {
                os.write("<html>\n".getBytes());
                while ((c = is.read()) != -1) {
                    sb.append((char) c);
                }
                System.out.println(sb.toString());
                sb.delete(0, sb.length() - 1);


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

            os.close();
            is.close();
            conn.close();

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

I've tried InputStream.read, but it doesn't block either, it returns -1 as well. I'm trying to keep the I/O streams on either side alive. I want the servlet to wait for input, process the input, then send back a response.
In the code above it should do this 20 times.

thanks for any help

© Stack Overflow or respective owner

Related posts about blocking

Related posts about inputstream