Beispiel #1
0
  /**
   * Get text content of a source file.
   *
   * @param path The canonical path of source file.
   * @param charset Source file encoding.
   * @return Source file content.
   */
  public String read(String path, String charset) {
    String str = null;
    byte[] bin = read(path);

    try {
      str = Charset.forName(charset).newDecoder().decode(ByteBuffer.wrap(bin)).toString();
    } catch (CharacterCodingException e) {
      App.exit("Cannot read " + path + " as " + charset + " encoded file");
    }

    return str;
  }
  /**
   * Fills the time buffer with the formatted time.
   *
   * @param date current time in milliseconds
   */
  private void fillTime(long date) throws IOException {
    synchronized (_timeBuffer) {
      if (date / 1000 == _lastTime / 1000) return;

      if (_timeFormatSecondOffset >= 0 && date / 3600000 == _lastTime / 3600000) {
        byte[] bBuf = _timeBuffer.getBuffer();

        int min = (int) (date / 60000 % 60);
        int sec = (int) (date / 1000 % 60);

        bBuf[_timeFormatMinuteOffset + 0] = (byte) ('0' + min / 10);
        bBuf[_timeFormatMinuteOffset + 1] = (byte) ('0' + min % 10);

        bBuf[_timeFormatSecondOffset + 0] = (byte) ('0' + sec / 10);
        bBuf[_timeFormatSecondOffset + 1] = (byte) ('0' + sec % 10);

        _lastTime = date;

        return;
      }

      _timeCharBuffer.clear();
      QDate.formatLocal(_timeCharBuffer, date, _timeFormat);

      if (_timeFormatSecondOffset >= 0) {
        _timeFormatSecondOffset = _timeCharBuffer.lastIndexOf(':') + 1;
        _timeFormatMinuteOffset = _timeFormatSecondOffset - 3;
      }

      char[] cBuf = _timeCharBuffer.getBuffer();
      int length = _timeCharBuffer.getLength();

      _timeBuffer.setLength(length);
      byte[] bBuf = _timeBuffer.getBuffer();

      for (int i = length - 1; i >= 0; i--) bBuf[i] = (byte) cBuf[i];
    }

    _lastTime = date;
  }
Beispiel #3
0
  /**
   * Converts the given value into a desired byte order.
   *
   * @param aValue the value to convert;
   * @param aByteCount the number of bytes that are supposed to be in the given value;
   * @param aByteOrder the desired byte order.
   * @return the converted value.
   */
  public static int convertByteOrder(
      final int aValue, final int aByteCount, final ByteOrder aByteOrder) {
    if ((aByteCount <= 0) || (aByteCount > 32)) {
      throw new IllegalArgumentException("Bit count cannot be zero, negative or beyond 32-bits!");
    }

    final ByteBuffer buf = ByteBuffer.allocate(aByteCount);
    buf.putInt(aValue);
    buf.order(aByteOrder);
    buf.position(0);
    final int result = buf.getInt();
    return result;
  }
  /**
   * Logs a request using the current format.
   *
   * @param request the servlet request.
   * @param response the servlet response.
   * @param buffer byte buffer containing the response
   * @param offset buffer starting offset
   * @param length length allowed in the buffer
   * @return the new tail of the buffer
   */
  private int log(
      HttpServletRequestImpl request,
      HttpServletResponseImpl responseFacade,
      AbstractHttpResponse response,
      byte[] buffer,
      int offset,
      int length)
      throws IOException {
    AbstractHttpRequest absRequest = request.getAbstractHttpRequest();

    int len = _segments.length;
    for (int i = 0; i < len; i++) {
      Segment segment = _segments[i];
      String value = null;
      CharSegment csValue = null;

      switch (segment._code) {
        case Segment.TEXT:
          int sublen = segment._data.length;
          byte[] data = segment._data;
          for (int j = 0; j < sublen; j++) buffer[offset++] = data[j];
          break;

        case Segment.CHAR:
          buffer[offset++] = segment._ch;
          break;

        case 'b':
          if (responseFacade.getStatus() == 304) buffer[offset++] = (byte) '-';
          else offset = print(buffer, offset, response.getContentLength());
          break;

          // cookie
        case 'c':
          Cookie cookie = request.getCookie(segment._string);
          if (cookie == null) cookie = responseFacade.getCookie(segment._string);
          if (cookie == null) buffer[offset++] = (byte) '-';
          else offset = print(buffer, offset, cookie.getValue());
          break;

          // set cookie
        case Segment.SET_COOKIE:
          ArrayList<Cookie> cookies = responseFacade.getCookies();
          if (cookies == null || cookies.size() == 0) buffer[offset++] = (byte) '-';
          else {
            _cb.clear();
            response.fillCookie(_cb, (Cookie) cookies.get(0), 0, 0, false);

            offset = print(buffer, offset, _cb.getBuffer(), 0, _cb.getLength());
          }
          break;

        case 'h':
          if (isHostnameDnsLookup()) {
            String addrName = request.getRemoteAddr();
            InetAddress addr = InetAddress.getByName(addrName);

            offset = print(buffer, offset, addr.getHostName());
          } else offset = absRequest.printRemoteAddr(buffer, offset);
          break;

          // input header
        case 'i':
          csValue = absRequest.getHeaderBuffer(segment._string);
          if (csValue == null) buffer[offset++] = (byte) '-';
          else offset = print(buffer, offset, csValue);
          break;

        case 'l':
          buffer[offset++] = (byte) '-';
          break;

          // request attribute
        case 'n':
          Object oValue = request.getAttribute(segment._string);
          if (oValue == null) buffer[offset++] = (byte) '-';
          else offset = print(buffer, offset, String.valueOf(oValue));
          break;

          // output header
        case 'o':
          value = response.getHeader(segment._string);
          if (value == null) buffer[offset++] = (byte) '-';
          else offset = print(buffer, offset, value);
          break;

        case 'r':
          offset = print(buffer, offset, request.getMethod());

          buffer[offset++] = (byte) ' ';

          data = absRequest.getUriBuffer();
          sublen = absRequest.getUriLength();

          // server/02e9
          if (buffer.length - offset - 128 < sublen) {
            sublen = buffer.length - offset - 128;
            System.arraycopy(data, 0, buffer, offset, sublen);
            offset += sublen;
            buffer[offset++] = (byte) '.';
            buffer[offset++] = (byte) '.';
            buffer[offset++] = (byte) '.';
          } else {
            System.arraycopy(data, 0, buffer, offset, sublen);
            offset += sublen;
          }

          buffer[offset++] = (byte) ' ';

          offset = print(buffer, offset, request.getProtocol());
          break;

        case 's':
          int status = responseFacade.getStatus();
          buffer[offset++] = (byte) ('0' + (status / 100) % 10);
          buffer[offset++] = (byte) ('0' + (status / 10) % 10);
          buffer[offset++] = (byte) ('0' + status % 10);
          break;

        case 't':
          long date = Alarm.getCurrentTime();

          if (date / 1000 != _lastTime / 1000) fillTime(date);

          sublen = _timeBuffer.getLength();
          data = _timeBuffer.getBuffer();

          synchronized (_timeBuffer) {
            System.arraycopy(data, 0, buffer, offset, sublen);
          }

          offset += sublen;
          break;

        case 'T':
          {
            long startTime = request.getStartTime();
            long endTime = Alarm.getCurrentTime();

            offset = print(buffer, offset, (int) ((endTime - startTime + 500) / 1000));
            break;
          }

        case 'D':
          {
            long startTime = request.getStartTime();
            long endTime = Alarm.getCurrentTime();

            offset = print(buffer, offset, (int) ((endTime - startTime) * 1000));
            break;
          }

        case 'u':
          value = request.getRemoteUser(false);
          if (value == null) buffer[offset++] = (byte) '-';
          else {
            buffer[offset++] = (byte) '"';
            offset = print(buffer, offset, value);
            buffer[offset++] = (byte) '"';
          }
          break;

        case 'v':
          value = request.getServerName();
          if (value == null) buffer[offset++] = (byte) '-';
          else {
            offset = print(buffer, offset, value);
          }
          break;

        case 'U':
          offset = print(buffer, offset, request.getRequestURI());
          break;

        default:
          throw new IOException();
      }
    }

    return offset;
  }
 private ByteEncoder() {
   _buf = ByteBuffer.allocateDirect(MAX_OBJECT_SIZE + 2048);
   _buf.order(Bytes.ORDER);
 }
  /**
   * method to extract the next monitor entry from the instrumentation memory. assumes that
   * nextEntry is the offset into the byte array at which to start the search for the next entry.
   * method leaves next entry pointing to the next entry or to the end of data.
   */
  protected Monitor getNextMonitorEntry() throws MonitorException {
    Monitor monitor = null;

    // entries are always 4 byte aligned.
    if ((nextEntry % 4) != 0) {
      throw new MonitorStructureException("Entry index not properly aligned: " + nextEntry);
    }

    // protect against a corrupted shared memory region.
    if ((nextEntry < 0) || (nextEntry > buffer.limit())) {
      throw new MonitorStructureException(
          "Entry index out of bounds: nextEntry = " + nextEntry + ", limit = " + buffer.limit());
    }

    // check for the end of the buffer
    if (nextEntry == buffer.limit()) {
      lognl("getNextMonitorEntry():" + " nextEntry == buffer.limit(): returning");
      return null;
    }

    buffer.position(nextEntry);

    int entryStart = buffer.position();
    int entryLength = buffer.getInt();

    // check for valid entry length
    if ((entryLength < 0) || (entryLength > buffer.limit())) {
      throw new MonitorStructureException("Invalid entry length: entryLength = " + entryLength);
    }

    // check if last entry occurs before the eof.
    if ((entryStart + entryLength) > buffer.limit()) {
      throw new MonitorStructureException(
          "Entry extends beyond end of buffer: "
              + " entryStart = "
              + entryStart
              + " entryLength = "
              + entryLength
              + " buffer limit = "
              + buffer.limit());
    }

    if (entryLength == 0) {
      // end of data
      return null;
    }

    int nameLength = buffer.getInt();
    int vectorLength = buffer.getInt();
    byte dataType = buffer.get();
    byte flags = buffer.get();
    Units u = Units.toUnits(buffer.get());
    Variability v = Variability.toVariability(buffer.get());
    boolean supported = (flags & 0x01) != 0;

    // defend against corrupt entries
    if ((nameLength <= 0) || (nameLength > entryLength)) {
      throw new MonitorStructureException("Invalid Monitor name length: " + nameLength);
    }

    if ((vectorLength < 0) || (vectorLength > entryLength)) {
      throw new MonitorStructureException("Invalid Monitor vector length: " + vectorLength);
    }

    // read in the perfData item name, casting bytes to chars. skip the
    // null terminator
    //
    byte[] nameBytes = new byte[nameLength - 1];
    for (int i = 0; i < nameLength - 1; i++) {
      nameBytes[i] = buffer.get();
    }

    // convert name into a String
    String name = new String(nameBytes, 0, nameLength - 1);

    if (v == Variability.INVALID) {
      throw new MonitorDataException(
          "Invalid variability attribute:" + " entry index = " + perfDataItem + " name = " + name);
    }
    if (u == Units.INVALID) {
      throw new MonitorDataException(
          "Invalid units attribute: " + " entry index = " + perfDataItem + " name = " + name);
    }

    int offset;
    if (vectorLength == 0) {
      // scalar Types
      if (dataType == BasicType.LONG.intValue()) {
        offset = entryStart + entryLength - 8; /* 8 = sizeof(long) */
        buffer.position(offset);
        LongBuffer lb = buffer.asLongBuffer();
        lb.limit(1);
        monitor = new PerfLongMonitor(name, u, v, supported, lb);
        perfDataItem++;
      } else {
        // bad data types.
        throw new MonitorTypeException(
            "Invalid Monitor type:"
                + " entry index = "
                + perfDataItem
                + " name = "
                + name
                + " type = "
                + dataType);
      }
    } else {
      // vector types
      if (dataType == BasicType.BYTE.intValue()) {
        if (u != Units.STRING) {
          // only byte arrays of type STRING are currently supported
          throw new MonitorTypeException(
              "Invalid Monitor type:"
                  + " entry index = "
                  + perfDataItem
                  + " name = "
                  + name
                  + " type = "
                  + dataType);
        }

        offset = entryStart + PERFDATA_NAME_OFFSET + nameLength;
        buffer.position(offset);
        ByteBuffer bb = buffer.slice();
        bb.limit(vectorLength);
        bb.position(0);

        if (v == Variability.CONSTANT) {
          monitor = new PerfStringConstantMonitor(name, supported, bb);
        } else if (v == Variability.VARIABLE) {
          monitor = new PerfStringVariableMonitor(name, supported, bb, vectorLength - 1);
        } else {
          // Monotonically increasing byte arrays are not supported
          throw new MonitorDataException(
              "Invalid variability attribute:"
                  + " entry index = "
                  + perfDataItem
                  + " name = "
                  + name
                  + " variability = "
                  + v);
        }
        perfDataItem++;
      } else {
        // bad data types.
        throw new MonitorTypeException(
            "Invalid Monitor type:"
                + " entry index = "
                + perfDataItem
                + " name = "
                + name
                + " type = "
                + dataType);
      }
    }

    // setup index to next entry for next iteration of the loop.
    nextEntry = entryStart + entryLength;
    return monitor;
  }