Code example for OutputStream

Methods: close, write

0
                mIn.close();
        } catch (IOException ex) {
        } 
        try { 
            if (mOut != null)
                mOut.close();
        } catch (IOException ex) {
        } 
        mSocket = null;
        mIn = null;
        mOut = null;
    } 
 
    private boolean readBytes(byte buffer[], int len) {
        int off = 0, count;
        if (len < 0)
            return false; 
        while (off != len) {
            try { 
                count = mIn.read(buffer, off, len - off);
                if (count <= 0) {
                    Slog.e(TAG, "read error " + count);
                    break; 
                } 
                off += count;
            } catch (IOException ex) {
                Slog.e(TAG, "read exception");
                break; 
            } 
        } 
        if (LOCAL_DEBUG) {
            Slog.i(TAG, "read " + len + " bytes");
        } 
        if (off == len)
            return true; 
        disconnect(); 
        return false; 
    } 
 
    private boolean readReply() { 
        int len;
        buflen = 0;
        if (!readBytes(buf, 2))
            return false; 
        len = (((int) buf[0]) & 0xff) | ((((int) buf[1]) & 0xff) << 8);
        if ((len < 1) || (len > 1024)) {
            Slog.e(TAG, "invalid reply length (" + len + ")");
            disconnect(); 
            return false; 
        } 
        if (!readBytes(buf, len))
            return false; 
        buflen = len;
        return true; 
    } 
 
    private boolean writeCommand(String _cmd) {
        byte[] cmd = _cmd.getBytes();
        int len = cmd.length;
        if ((len < 1) || (len > 1024))
            return false; 
        buf[0] = (byte) (len & 0xff);
        buf[1] = (byte) ((len >> 8) & 0xff);
        try { 
            mOut.write(buf, 0, 2);
            mOut.write(cmd, 0, len);
        } catch (IOException ex) {
            Slog.e(TAG, "write error");
            disconnect(); 
            return false; 
        }