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

📄 finger.java

📁 JAVA网络编程技术内幕一书的源代码
💻 JAVA
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import java.net.*;
import java.util.*;

public class Finger {
  public static final int DEFAULT_PORT = 79;

  protected boolean verbose;
  protected int port;
  protected String host, query;

  public Finger (String request, boolean verbose) throws IOException {
    this.verbose = verbose;
    int at = request.lastIndexOf ('@');
    if (at == -1) {
      query = request;
      host = InetAddress.getLocalHost ().getHostName ();
      port = DEFAULT_PORT;
    } else {
      query = request.substring (0, at);
      int colon = request.indexOf (':', at + 1);
      if (colon == -1) {
        host = request.substring (at + 1);
        port = DEFAULT_PORT;
      } else {
        host = request.substring (at + 1, colon);
        port = Integer.parseInt (request.substring (colon + 1));
      }
      if (host.equals (""))
        host = InetAddress.getLocalHost ().getHostName ();
    }
  }

  public Finger (String query, String host, int port, boolean verbose) throws IOException {
    this.query = query;
    this.host = host.equals ("") ?
      InetAddress.getLocalHost ().getHostName () : host;
    this.port = (port == -1) ? DEFAULT_PORT : port;
    this.verbose = verbose;
  }

  public Reader finger () throws IOException {
    Socket socket = new Socket (host, port);
    OutputStream out = socket.getOutputStream ();
    OutputStreamWriter writer = new OutputStreamWriter (out, "latin1");
    if (verbose)
      writer.write ("/W");
    if (verbose && (query.length () > 0))
      writer.write (" ");
    writer.write (query);
    writer.write ("\r\n");
    writer.flush ();
    return new InputStreamReader (socket.getInputStream (), "latin1");
  }

  public static void display (Reader reader, Writer writer) throws IOException {
    PrintWriter printWriter = new PrintWriter (writer);
    BufferedReader bufferedReader = new BufferedReader (reader);
    String line;
    while ((line = bufferedReader.readLine ()) != null)
      printWriter.println (line);
    reader.close ();
  }

  public static void main (String[] args) throws IOException {
    if (((args.length == 2) && !args[0].equals ("-l")) || (args.length > 2))
      throw new IllegalArgumentException
        ("Syntax: Finger [-l] [<username>][{@<hostname>}[:<port>]]");
    
    boolean verbose = (args.length > 0) && args[0].equals ("-l");
    String query = (args.length > (verbose ? 1 : 0)) ?
      args[args.length - 1] : "";
    
    Finger finger = new Finger (query, verbose);
    Reader result = finger.finger ();
    Writer console = new FileWriter (FileDescriptor.out);
    display (result, console);
    console.flush ();
  }
}

⌨️ 快捷键说明

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