// s is the effective dimension if > 0, otherwise it is dim
  private void readData(Reader re, int r1, int s1) throws IOException, NumberFormatException {
    try {
      StreamTokenizer st = new StreamTokenizer(re);
      if (st == null) return;
      st.eolIsSignificant(false);
      st.slashSlashComments(true);

      int i = st.nextToken();
      if (i != StreamTokenizer.TT_NUMBER) throw new NumberFormatException();
      b = (int) st.nval;
      st.nextToken();
      numCols = (int) st.nval;
      st.nextToken();
      numRows = (int) st.nval;
      st.nextToken();
      numPoints = (int) st.nval;
      st.nextToken();
      dim = (int) st.nval;
      if (dim < 1) {
        System.err.println(PrintfFormat.NEWLINE + "DigitalNetBase2FromFile:   dimension dim <= 0");
        throw new IllegalArgumentException("dimension dim <= 0");
      }
      if (r1 > numRows)
        throw new IllegalArgumentException(
            "DigitalNetBase2FromFile:   One must have   r1 <= Max num rows");
      if (s1 > dim) {
        throw new IllegalArgumentException("s1 is too large");
      }
      if (s1 > 0) dim = s1;
      if (r1 > 0) numRows = r1;

      if (b != 2) {
        System.err.println("***** DigitalNetBase2FromFile:    only base 2 allowed");
        throw new IllegalArgumentException("only base 2 allowed");
      }
      genMat = new int[dim * numCols];
      for (i = 0; i < dim; i++)
        for (int c = 0; c < numCols; c++) {
          st.nextToken();
          genMat[i * numCols + c] = (int) st.nval;
        }

    } catch (NumberFormatException e) {
      System.err.println("   DigitalNetBase2FromFile:   not a number  " + e);
      throw e;
    }
  }
Example #2
0
  public void run() {
    Socket sock = null;

    try {
      int code = StreamTokenizer.TT_EOL;
      FileReader reader = new FileReader(filename);
      StreamTokenizer tokenizer = new StreamTokenizer(reader);
      tokenizer.ordinaryChars('0', '9');
      tokenizer.wordChars('0', '9');
      tokenizer.slashSlashComments(true);

      System.out.println("Connecting to socket 10576.");
      try {
        sock = new Socket("127.0.0.1", 10576);
        System.out.println("Connection to socket 10576 established.");
      } catch (Exception e) {
        System.out.println(
            "Inputting packets from file must be done while running Tossim with the -ri option");
        System.exit(-1);
      }

      DataOutputStream output = new DataOutputStream(sock.getOutputStream());

      while (true) {
        code = tokenizer.nextToken();
        if (code == tokenizer.TT_EOF) {
          break;
        } else if (code == StreamTokenizer.TT_EOL) {
        } else if (code == StreamTokenizer.TT_WORD) {
          String word = tokenizer.sval;
          long lval = Long.parseLong(word);

          code = tokenizer.nextToken();
          if (code != StreamTokenizer.TT_WORD) {
            break;
          }
          word = tokenizer.sval;
          short sval = Short.parseShort(word);

          byte[] data = new byte[36];
          for (int i = 0; i < 36; i++) {
            code = tokenizer.nextToken();
            if (code != StreamTokenizer.TT_WORD) {
              break;
            }
            String datum = tokenizer.sval;
            try {
              data[i] = (byte) (Integer.parseInt(datum, 16) & 0xff);
            } catch (NumberFormatException e) {
              System.out.println(e);
              System.out.println(datum);
            }
          }

          output.writeLong(lval);
          output.writeShort(sval);
          output.write(data);
        } else if (code == StreamTokenizer.TT_NUMBER) {
        }
      }
    } catch (Exception exception) {
      System.err.println("Exception thrown.");
      exception.printStackTrace();
    } finally {
      try {
        sock.close();
      } catch (Exception e) {
      }
    }
    /// ServerSocket server = new ServerSocket(10576, 1);
    // System.out.println("Waiting on socket 10576.");
    // Socket sock = server.accept();
    // System.out.println("Accepted connection from " + sock);

    // DataOutputStream input = new DataOutputStream(sock.getOutputStream());
  }