public final void writeASCII(String s) throws IOException { if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException(); OutputStream os = getOutputStream(); synchronized (os) { if (Defines.DEBUG && CAT.isDebugEnabled()) CAT.debug("C: " + s); os.write(s.getBytes("ASCII")); os.write((byte) 13); os.write((byte) 10); os.flush(); } }
public final String readCommand(byte[] b) throws IOException, InterruptedException, MessagingNetworkException { InputStream is = getInputStream(); synchronized (is) { long abortTime = System.currentTimeMillis() + 1000 * MSNMessagingNetwork.REQPARAM_SOCKET_TIMEOUT_SECONDS; int ofs = 0; boolean d = false; for (; ; ) { if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException(); int by = is.read(); if (by == -1) throw new IOException("unexpected EOF"); if (by == 10 && d) break; d = (by == 13); if (ofs < b.length) { b[ofs++] = (byte) by; } if (System.currentTimeMillis() > abortTime) throw new IOException("connection timed out"); /* if (len >= buffer.length) { ... return ...; } int pos = findCRLF(); if (pos != -1) break; fill(is, abortTime); */ } if (b[ofs - 1] == 13) --ofs; String line = new String(b, 0, ofs, "ASCII"); if (StringUtil.startsWith(line, "MSG")) { StringTokenizer st = new StringTokenizer(line); String len_s = null; while (st.hasMoreTokens()) { len_s = st.nextToken(); } if (len_s == null) throw new AssertException("len_s is null"); int len; try { len = Integer.parseInt(len_s); } catch (NumberFormatException ex) { ServerConnection.throwProtocolViolated("MSG length must be int"); len = 0; } String msg = readMSG(len); line = line + "\r\n" + msg; } if (Defines.DEBUG && CAT.isDebugEnabled()) CAT.debug("S: " + line); return line; } }
/** * UTF byte count is appended to the asciiPrefix, then UTF bytes are appended to the result; the * final result is sent. */ public final void writeMSG(String asciiPrefix, String msgBody) throws IOException { if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException(); OutputStream os = getOutputStream(); synchronized (os) { byte[] utfBytes = msgBody.getBytes("UTF-8"); asciiPrefix = asciiPrefix + ' ' + utfBytes.length; if (Defines.DEBUG && CAT.isDebugEnabled()) CAT.debug("C: " + asciiPrefix + "\r\n" + msgBody); os.write(asciiPrefix.getBytes("ASCII")); os.write((byte) 13); os.write((byte) 10); os.write(utfBytes); os.flush(); } }