Exemple #1
0
  // Check if the datagramsocket adaptor can send with a packet
  // that has not been initialized with an address; the legacy
  // datagram socket will send in this case
  private static void test2() throws Exception {
    DatagramChannel sndChannel = DatagramChannel.open();
    sndChannel.socket().bind(null);
    InetSocketAddress sender =
        new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort());

    DatagramChannel rcvChannel = DatagramChannel.open();
    rcvChannel.socket().bind(null);
    InetSocketAddress receiver =
        new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort());

    rcvChannel.connect(sender);
    sndChannel.connect(receiver);

    byte b[] = "hello".getBytes("UTF-8");
    DatagramPacket pkt = new DatagramPacket(b, b.length);
    sndChannel.socket().send(pkt);

    ByteBuffer bb = ByteBuffer.allocate(256);
    rcvChannel.receive(bb);
    bb.flip();
    CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed");

    // Check that the pkt got set with the target address;
    // This is legacy behavior
    if (!pkt.getSocketAddress().equals(receiver)) throw new RuntimeException("Test failed");

    rcvChannel.close();
    sndChannel.close();
  }
Exemple #2
0
  // Check if DatagramChannel.send while connected can include
  // address without throwing
  private static void test1() throws Exception {

    DatagramChannel sndChannel = DatagramChannel.open();
    sndChannel.socket().bind(null);
    InetSocketAddress sender =
        new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort());

    DatagramChannel rcvChannel = DatagramChannel.open();
    rcvChannel.socket().bind(null);
    InetSocketAddress receiver =
        new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort());

    rcvChannel.connect(sender);
    sndChannel.connect(receiver);

    ByteBuffer bb = ByteBuffer.allocate(256);
    bb.put("hello".getBytes());
    bb.flip();
    int sent = sndChannel.send(bb, receiver);
    bb.clear();
    rcvChannel.receive(bb);
    bb.flip();
    CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed");

    rcvChannel.close();
    sndChannel.close();
  }
Exemple #3
0
  /*
   * Mon, 17 Jan 1994 11:14:55 -0500 (EST)
   */
  public String printDate() {
    if (_lastDate != null && _lastTime == _localTimeOfEpoch) return _lastDate;

    CharBuffer cb = new CharBuffer();

    printDate(cb);

    _lastDate = cb.toString();
    _lastTime = _localTimeOfEpoch;

    return _lastDate;
  }
Exemple #4
0
  /** Prints just the date component of ISO 8601 */
  public String printISO8601Date() {
    CharBuffer cb = new CharBuffer();

    if (_year > 0) {
      cb.append((_year / 1000) % 10);
      cb.append((_year / 100) % 10);
      cb.append((_year / 10) % 10);
      cb.append(_year % 10);
      cb.append('-');
      cb.append(((_month + 1) / 10) % 10);
      cb.append((_month + 1) % 10);
      cb.append('-');
      cb.append(((_dayOfMonth + 1) / 10) % 10);
      cb.append((_dayOfMonth + 1) % 10);
    }

    return cb.toString();
  }
Exemple #5
0
  /*
   * XXX: okay, this is vile.
   * Mon, 17 Jan 1994 11:14:55 -0500 (EST)
   *
   * In GMT time
   */
  public long parseDate(String string) throws Exception {
    try {
      int strlen = string.length();

      if (strlen == 0) return 0;

      int i = skipWhitespace(string, strlen, 0);

      int ch = string.charAt(i);
      if (ch >= '0' && ch <= '9'
          || (ch == 'T'
              && i + 1 < strlen
              && string.charAt(i + 1) >= '0'
              && string.charAt(i + 1) <= '9')) return parseISO8601Date(string, i);

      CharBuffer cb = new CharBuffer();

      i = scan(string, 0, cb, true);
      if (cb.length() == 0 || !Character.isDigit(cb.charAt(0))) i = scan(string, i, cb, true);

      int dayOfMonth = parseInt(cb);
      i = scan(string, i, cb, true);
      String smonth = cb.toString();
      int month;
      for (month = 0; month < 12; month++) {
        if (MONTH_NAMES[(int) month].equalsIgnoreCase(smonth)) break;
      }
      if (month == 12) throw new Exception("Unexpected month: " + month);

      i = scan(string, i, cb, true);

      int year = parseInt(cb);
      if (cb.length() < 3 && year < 50) year += 2000;
      else if (cb.length() < 3 && year < 100) year += 1900;

      i = scan(string, i, cb, false);
      long timeOfDay = parseInt(cb) * 3600000;

      i = scan(string, i, cb, false);
      timeOfDay += parseInt(cb) * 60000;

      i = scan(string, i, cb, false);
      timeOfDay += parseInt(cb) * 1000;

      // XXX: gross hack
      if (year <= 1600) dayOfMonth--;

      long time =
          (MS_PER_DAY
                  * (yearToDayOfEpoch(year)
                      + monthToDayOfYear(month, isLeapYear(year))
                      + dayOfMonth
                      - 1)
              + timeOfDay);

      try {
        i = scan(string, i, cb, false);
        for (int j = 0; j < cb.length(); j++) {
          if ((ch = cb.charAt(j)) == ';' || ch == ' ') cb.setLength(j);
        }

        ch = cb.length() > 0 ? cb.charAt(0) : 0;
        if (ch == '-' || ch == '+' || ch >= '0' && ch <= '9') {
          long zoneOffset;
          zoneOffset = parseInt(cb);
          zoneOffset = 60000 * (60 * (zoneOffset / 100) + zoneOffset % 100);

          time -= zoneOffset;

          setGMTTime(time);
        } else if (cb.equalsIgnoreCase("gmt") || cb.equalsIgnoreCase("utc")) {
          setGMTTime(time);
        } else {
          setLocalTime(time);
        }
      } catch (Exception e) {
        log.log(Level.FINER, e.toString(), e);
      }

      return _localTimeOfEpoch - _zoneOffset;
    } catch (Exception e) {
      log.log(Level.FINER, e.toString(), e);

      return Long.MAX_VALUE;
    }
  }
  public static void main(String[] args) throws Exception {
    System.out.println(">>> StringCharBufferSliceTest-main: testing the slice method...");

    final String in = "for testing";

    System.out.println(">>> StringCharBufferSliceTest-main: testing with the position 0.");

    CharBuffer buff = CharBuffer.wrap(in);
    test(buff, buff.slice());

    System.out.println(">>> StringCharBufferSliceTest-main: testing with new position.");

    buff.position(2);
    test(buff, buff.slice());

    System.out.println(
        ">>> StringCharBufferSliceTest-main: testing with non zero initial position.");

    buff = CharBuffer.wrap(in, 3, in.length());
    test(buff, buff.slice());

    System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get()");
    buff.position(4);
    buff.limit(7);
    CharBuffer slice = buff.slice();
    for (int i = 0; i < 3; i++) {
      if (slice.get() != buff.get()) {
        throw new RuntimeException("Wrong characters in slice result.");
      }
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
    buff.position(4);
    buff.limit(7);
    slice = buff.slice();
    for (int i = 0; i < 3; i++) {
      if (slice.get(i) != buff.get(4 + i)) {
        throw new RuntimeException("Wrong characters in slice result.");
      }
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing slice with result of slice");
    buff.position(0);
    buff.limit(buff.capacity());
    slice = buff.slice();
    for (int i = 0; i < 4; i++) {
      slice.position(i);
      CharBuffer nextSlice = slice.slice();
      if (nextSlice.position() != 0)
        throw new RuntimeException("New buffer's position should be zero");
      if (!nextSlice.equals(slice)) throw new RuntimeException("New buffer should be equal");
      slice = nextSlice;
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing toString.");
    buff.position(4);
    buff.limit(7);
    slice = buff.slice();
    if (!slice.toString().equals("tes")) {
      throw new RuntimeException("bad toString() after slice(): " + slice.toString());
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing subSequence.");
    buff.position(4);
    buff.limit(8);
    slice = buff.slice();
    CharSequence subSeq = slice.subSequence(1, 3);
    if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
      throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing duplicate.");
    buff.position(4);
    buff.limit(8);
    slice = buff.slice();
    CharBuffer dupe = slice.duplicate();
    if (dupe.charAt(0) != 't'
        || dupe.charAt(1) != 'e'
        || dupe.charAt(2) != 's'
        || dupe.charAt(3) != 't') {
      throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
    }

    System.out.println(">>> StringCharBufferSliceTest-main: done!");
  }
  /** Parses the access log string. */
  private ArrayList<Segment> parseFormat(String format) {
    ArrayList<Segment> segments = new ArrayList<Segment>();
    CharBuffer cb = new CharBuffer();

    int i = 0;
    while (i < _format.length()) {
      char ch = _format.charAt(i++);

      if (ch != '%' || i >= _format.length()) {
        cb.append((char) ch);
        continue;
      }

      String arg = null;
      ch = _format.charAt(i++);
      if (ch == '>') ch = _format.charAt(i++);
      else if (ch == '{') {
        if (cb.length() > 0) segments.add(new Segment(this, Segment.TEXT, cb.toString()));
        cb.clear();
        while (i < _format.length() && _format.charAt(i++) != '}') cb.append(_format.charAt(i - 1));
        arg = cb.toString();
        cb.clear();

        ch = _format.charAt(i++);
      }

      switch (ch) {
        case 'b':
        case 'c':
        case 'h':
        case 'i':
        case 'l':
        case 'n':
        case 'r':
        case 's':
        case 'T':
        case 'D':
        case 'o':
        case 'u':
        case 'U':
        case 'v':
          if (cb.length() > 0) segments.add(new Segment(this, Segment.TEXT, cb.toString()));
          cb.clear();
          segments.add(new Segment(this, ch, arg));
          break;

        case 't':
          if (cb.length() > 0) segments.add(new Segment(this, Segment.TEXT, cb.toString()));
          cb.clear();
          if (arg != null) _timeFormat = arg;
          segments.add(new Segment(this, ch, arg));
          break;

        default:
          cb.append('%');
          i--;
          break;
      }
    }

    cb.append(CauchoSystem.getNewlineString());
    segments.add(new Segment(this, Segment.TEXT, cb.toString()));

    return segments;
  }