예제 #1
0
  public static String streamToString(InputStream is) throws IOException {
    if (is != null) {
      StringBuilder sb = new StringBuilder();
      String line;

      try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        while ((line = reader.readLine()) != null) {
          sb.append(line).append("\n");
        }
      } finally {
        is.close();
      }
      return sb.toString();
    } else {
      return "";
    }
  }
 public void receiveTarget() {
   // 接收用户输入的地址,向targets队列中加入任务
   try {
     BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
     String msg = null;
     while ((msg = localReader.readLine()) != null) {
       if (!msg.equals("bye")) {
         Target target = new Target(msg);
         addTarget(target);
       } else {
         shutdown = true;
         selector.wakeup();
         break;
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #3
0
  /*
   * Checks if file exists, if it doesn't return null. If it does
   * reads each line from the file and returns an Array of Strings,
   * each index being 1 line from the file.
   */
  public static String[] readLinesFromFile(File filename) throws IOException {
    BufferedReader reader = null;
    // TODO: Convert to stringbuilder
    String lines = "";
    String temp = "";

    if (!filename.exists()) {
      return null;
    }

    try {
      reader = new BufferedReader(new FileReader(filename));
      temp = reader.readLine();

      while (temp != null) {
        lines += temp + "\n";
        temp = reader.readLine();
      }
    } finally {
      reader.close();
    }

    return lines.split("\n");
  }
예제 #4
0
  public static void readCommand() {
    try {

      if (!reader.ready()) {
        return;
      }

      StringTokenizer tokens = new StringTokenizer(reader.readLine());

      if (tokens.hasMoreTokens()) {

        switch (tokens.nextToken()) {
          case "/listen":
            listen(tokens);
            break;

          case "/stop":
            stop();
            break;

          case "/list":
            if (port[0] == -1) {
              System.err.println("Error: start listening before");
            }

            for (Entry<String, SocketChannel> client : clients.entrySet()) {
              System.out.println(client.getKey());
            }

            if (clients.entrySet().isEmpty()) {
              System.out.println("list: no clients online");
            }
            break;

          case "/send":
            if (tokens.hasMoreTokens()) {
              String name = tokens.nextToken();
              if (clients.containsKey(name)) {
                if (tokens.hasMoreTokens()) {
                  sendMessage(
                      clients.get(name),
                      MessageUtils.message("<server>", getMessageFromTokens(tokens)));
                } else {
                  System.err.println("send: message is empty");
                }
              } else {
                System.err.println("send: no such client");
              }
            } else {
              System.err.println("send: no client name");
            }
            break;

          case "/sendall":
            if (tokens.hasMoreTokens()) {
              notifyAllClients(getMessageFromTokens(tokens));
            } else {
              System.err.println("sendall: message is empty");
            }
            break;

          case "/kill":
            kill(tokens);
            break;

          case "/exit":
            if (port[0] != -1) {
              stop();
            }
            Utils.tryClose(selector);
            System.exit(0);
            break;

          default:
            System.err.print("Error: unknown command");
            break;
        }

      } else {
        System.err.println("Error: empty input");
      }

    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }