Example usage for java.io PrintWriter checkError

List of usage examples for java.io PrintWriter checkError

Introduction

In this page you can find the example usage for java.io PrintWriter checkError.

Prototype

public boolean checkError() 

Source Link

Document

Flushes the stream if it's not closed and checks its error state.

Usage

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String RegisterTheDevice(String sSrvr, String sPort, String sData) {
    String sRet = "";
    String line = "";

    //        Debug.waitForDebugger();

    if (sSrvr != null && sPort != null && sData != null) {
        try {//from   w ww.  j  a  v  a  2 s .c o  m
            int nPort = Integer.parseInt(sPort);
            Socket socket = new Socket(sSrvr, nPort);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), false);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out.println(sData);
            if (out.checkError() == false) {
                socket.setSoTimeout(30000);
                while (socket.isInputShutdown() == false) {
                    line = in.readLine();

                    if (line != null) {
                        line = line.toLowerCase();
                        sRet += line;
                        // ok means we're done
                        if (line.contains("ok"))
                            break;
                    } else {
                        // end of stream reached
                        break;
                    }
                }
            }
            out.close();
            in.close();
            socket.close();
        } catch (NumberFormatException e) {
            sRet += "reg NumberFormatException thrown [" + e.getLocalizedMessage() + "]";
            e.printStackTrace();
        } catch (UnknownHostException e) {
            sRet += "reg UnknownHostException thrown [" + e.getLocalizedMessage() + "]";
            e.printStackTrace();
        } catch (IOException e) {
            sRet += "reg IOException thrown [" + e.getLocalizedMessage() + "]";
            e.printStackTrace();
        }
    }
    return (sRet);
}