www.pudn.com > jnp-src.rar > Finger.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] [][{@}[:]]"); 
     
    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 (); 
  } 
}