コード例 #1
0
ファイル: Connection.java プロジェクト: egphilippov/openwood
  private final String readMSG(final int len)
      throws IOException, InterruptedException, MessagingNetworkException {
    if (len > 65000)
      ServerConnection.throwProtocolViolated("incoming message is too long: " + len + " bytes");
    byte[] b = new byte[len];
    InputStream is = getInputStream();
    synchronized (is) {
      long abortTime =
          System.currentTimeMillis() + 1000 * MSNMessagingNetwork.REQPARAM_SOCKET_TIMEOUT_SECONDS;
      int ofs = 0;

      while (ofs < len) {
        if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
        int read = is.read(b, ofs, len - ofs);
        if (read < 0) read = 0;
        ofs += read;
        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);
        */
      }

      String msg = new String(b, 0, len, "UTF-8");
      return msg;
    }
  }
コード例 #2
0
ファイル: Connection.java プロジェクト: egphilippov/openwood
 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();
   }
 }
コード例 #3
0
ファイル: Connection.java プロジェクト: egphilippov/openwood
  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;
    }
  }
コード例 #4
0
ファイル: Connection.java プロジェクト: egphilippov/openwood
 /**
  * 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();
   }
 }
コード例 #5
0
ファイル: Connection.java プロジェクト: egphilippov/openwood
 public final int available() throws IOException {
   if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
   if (isClosed()) throw new IOException("connection closed");
   return getInputStream().available();
 }