⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop3connection.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
              }            digest.append(Integer.toHexString((c & 0xf0) >> 4));            digest.append(Integer.toHexString(c & 0x0f));          }        // send command        String cmd = new StringBuffer(APOP)          .append(' ')          .append(username)          .append(' ')          .append(digest.toString())          .toString();        send(cmd);        return getResponse() == OK;      }    catch (NoSuchAlgorithmException e)      {        logger.log(POP3_TRACE, "MD5 algorithm not found");        return false;      }  }    /**   * Authenticate the user using the basic USER and PASS handshake.   * It is recommended to use a more secure authentication method such as   * the <code>auth</code> or <code>apop</code> method if the server   * understands them.   * @param username the user to authenticate   * @param password the user's password   */  public boolean login(String username, String password)    throws IOException  {    if (username == null || password == null)      {        return false;      }    // USER <username>    String cmd = USER + ' ' + username;    send(cmd);    if (getResponse() != OK)      {        return false;      }    // PASS <password>    cmd = PASS + ' ' + password;    send(cmd);    return getResponse() == OK;  }  /**   * Returns a configured SSLSocketFactory to use in creating new SSL   * sockets.   * @param tm an optional trust manager to use   */  protected SSLSocketFactory getSSLSocketFactory(TrustManager tm)    throws GeneralSecurityException  {    if (tm == null)      {        tm = new EmptyX509TrustManager();      }    SSLContext context = SSLContext.getInstance("TLS");    TrustManager[] trust = new TrustManager[] { tm };    context.init(null, trust, null);    return context.getSocketFactory();  }    /**   * Attempts to start TLS on the specified connection.   * See RFC 2595 for details   * @return true if successful, false otherwise   */  public boolean stls()    throws IOException  {    return stls(new EmptyX509TrustManager());  }    /**   * Attempts to start TLS on the specified connection.   * See RFC 2595 for details   * @param tm the custom trust manager to use   * @return true if successful, false otherwise   */  public boolean stls(TrustManager tm)    throws IOException  {    try      {        SSLSocketFactory factory = getSSLSocketFactory(tm);                send(STLS);        if (getResponse() != OK)          {            return false;          }                String hostname = socket.getInetAddress().getHostName();        int port = socket.getPort();        SSLSocket ss =          (SSLSocket) factory.createSocket(socket, hostname, port, true);        String[] protocols = { "TLSv1", "SSLv3" };        ss.setEnabledProtocols(protocols);        ss.setUseClientMode(true);        ss.startHandshake();                // set up streams        InputStream in = ss.getInputStream();        in = new BufferedInputStream(in);        in = new CRLFInputStream(in);        this.in = new LineInputStream(in);        OutputStream out = ss.getOutputStream();        out = new BufferedOutputStream(out);        this.out = new CRLFOutputStream(out);                return true;      }    catch (GeneralSecurityException e)      {        return false;      }  }    /**   * Returns the number of messages in the maildrop.   */  public int stat()    throws IOException  {    send(STAT);    if (getResponse() != OK)      {        throw new ProtocolException("STAT failed: " + response);      }    try      {        return          Integer.parseInt(response.substring(0, response.indexOf(' ')));      }    catch (NumberFormatException e)      {        throw new ProtocolException("Not a number: " + response);      }    catch (ArrayIndexOutOfBoundsException e)      {        throw new ProtocolException("Not a STAT response: " + response);      }  }    /**   * Returns the size of the specified message.   * @param msgnum the message number   */  public int list(int msgnum)    throws IOException  {    String cmd = LIST + ' ' + msgnum;    send(cmd);    if (getResponse() != OK)      {        throw new ProtocolException("LIST failed: " + response);      }    try      {        return          Integer.parseInt(response.substring(response.indexOf(' ') + 1));      }    catch (NumberFormatException e)      {        throw new ProtocolException("Not a number: " + response);      }  }    /**   * Returns an input stream containing the entire message.   * This input stream must be read in its entirety before further commands   * can be issued on this connection.   * @param msgnum the message number   */  public InputStream retr(int msgnum)    throws IOException  {    String cmd = RETR + ' ' + msgnum;    send(cmd);    if (getResponse() != OK)      {        throw new ProtocolException("RETR failed: " + response);      }    return new MessageInputStream(in);  }    /**   * Marks the specified message as deleted.   * @param msgnum the message number   */  public void dele(int msgnum)    throws IOException  {    String cmd = DELE + ' ' + msgnum;    send(cmd);    if (getResponse() != OK)      {        throw new ProtocolException("DELE failed: " + response);      }  }    /**   * Does nothing.   * This can be used to keep the connection alive.   */  public void noop()    throws IOException  {    send(NOOP);    if (getResponse() != OK)      {        throw new ProtocolException("NOOP failed: " + response);      }  }  /**   * If any messages have been marked as deleted, they are unmarked.   */  public void rset()    throws IOException  {    send(RSET);    if (getResponse() != OK)      {        throw new ProtocolException("RSET failed: " + response);      }  }    /**   * Closes the connection.   * No further commands may be issued on this connection after this method   * has been called.   * @return true if all deleted messages were successfully removed, false   * otherwise   */  public boolean quit()    throws IOException  {    send(QUIT);    int ret = getResponse();    socket.close();    return ret == OK;  }  /**   * Returns just the headers of the specified message as an input stream.   * The stream must be read in its entirety before further commands can be   * issued.   * @param msgnum the message number   */  public InputStream top(int msgnum)    throws IOException  {    String cmd = TOP + ' ' + msgnum + ' ' + '0';    send(cmd);    if (getResponse() != OK)      {        throw new ProtocolException("TOP failed: " + response);      }    return new MessageInputStream(in);  }    /**   * Returns a unique identifier for the specified message.   * @param msgnum the message number   */  public String uidl(int msgnum)    throws IOException  {    String cmd = UIDL + ' ' + msgnum;    send(cmd);    if (getResponse() != OK)      {        throw new ProtocolException("UIDL failed: " + response);      }    return response.substring(response.indexOf(' ') + 1);  }  /**   * Returns a map of message number to UID pairs.   * Message numbers are Integers, UIDs are Strings.   */  public Map uidl()    throws IOException  {    send(UIDL);    if (getResponse() != OK)      {        throw new ProtocolException("UIDL failed: " + response);      }    Map uids = new LinkedHashMap();    String line = in.readLine();    while (line != null && !(".".equals(line)))      {        int si = line.indexOf(' ');        if (si < 1)          {            throw new ProtocolException("Invalid UIDL response: " + line);          }        try          {            uids.put(new Integer(line.substring(0, si)),                      line.substring(si + 1));          }        catch (NumberFormatException e)          {            throw new ProtocolException("Invalid message number: " + line);          }      }    return Collections.unmodifiableMap(uids);  }  /**   * Returns a list of capabilities supported by the POP3 server.   * If the server does not support POP3 extensions, returns   * <code>null</code>.   */  public List capa()    throws IOException  {    send(CAPA);    if (getResponse() == OK)      {        final String DOT = ".";        List list = new ArrayList();        for (String line = in.readLine();             !DOT.equals(line);             line = in.readLine())          {            list.add(line);          }        return Collections.unmodifiableList(list);      }    return null;  }    /**    * Send the command to the server.   */  protected void send(String command)    throws IOException  {    logger.log(POP3_TRACE, "> " + command);    out.write(command);    out.writeln();    out.flush();  }    /**   * Parse the response from the server.   */  protected int getResponse()    throws IOException  {    response = in.readLine();    if (response == null)      {        throw new EOFException("unexpected EOF");      }    logger.log(POP3_TRACE, "< " + response);    if (response.indexOf(_OK) == 0)      {        response = response.substring(3).trim();        return OK;      }    else if (response.indexOf(_ERR) == 0)      {        response = response.substring(4).trim();        return ERR;      }    else if (response.indexOf(_READY) == 0)      {        response = response.substring(2).trim();        return READY;      }    else      {        throw new ProtocolException("Unexpected response: " + response);      }  }    /*   * Parse the APOP timestamp from the server's banner greeting.   */  byte[] parseTimestamp(String greeting)    throws IOException  {    int bra = greeting.indexOf('<');    if (bra != -1)      {        int ket = greeting.indexOf('>', bra);        if (ket != -1)          {            String mid = greeting.substring(bra, ket + 1);            int at = mid.indexOf('@');            if (at != -1)           // This is a valid RFC822 msg-id              {                return mid.getBytes("US-ASCII");              }          }      }    return null;  }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -