public String readString(QDataInputStream stream, int len) throws IOException {
    if (len > buflen) { // If the buffers we have are to small, make them bigger
      buf = ByteBuffer.allocate(len);
      charBuffer = CharBuffer.allocate(len);
      buflen = len;
    }

    // reset buffers, so we start from the beginning of them
    buf.clear();
    charBuffer.clear();

    // mark the start so we can reset back after we have read in the string
    charBuffer.mark();

    // Set the limit of the byte buffer, so we know where to stop the string.
    // Or else you get characters from old strings that was longer then this one
    buf.limit(len);

    // Read the string
    stream.readFully(buf.array(), 0, len);

    // Decode it with correct encoding
    try {
      decoder.decode(buf, charBuffer, false);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      Log.e(TAG, "Failed to decode buffer: " + buf);
    }

    // Set where the current string ends, it is the position we are at after decoding into the
    // buffer
    charBuffer.limit(charBuffer.position());

    // Reset buffer back to the mark(the start of the buffer) so we can convert to string
    charBuffer.reset();

    return charBuffer.toString();
  }
Exemple #2
0
    // Invert and truncate to five characters the phoneNumber so that we
    // can use it as the key in a hashtable.  We keep a mapping of this
    // key to a list of all contacts which have the same key.
    private String key(String phoneNumber, CharBuffer keyBuffer) {
      keyBuffer.clear();
      keyBuffer.mark();

      int position = phoneNumber.length();
      int resultCount = 0;
      while (--position >= 0) {
        char c = phoneNumber.charAt(position);
        if (Character.isDigit(c)) {
          keyBuffer.put(c);
          if (++resultCount == STATIC_KEY_BUFFER_MAXIMUM_LENGTH) {
            break;
          }
        }
      }
      keyBuffer.reset();
      if (resultCount > 0) {
        return keyBuffer.toString();
      } else {
        // there were no usable digits in the input phoneNumber
        return phoneNumber;
      }
    }
  ConfigElement processLine(String configfile, int lineno, CharBuffer buffer)
      throws ConfigParseException {
    char c;
    final StringBuilder processLineBuilder;
    final StringBuilder lineCommentBuilder;

    processLineBuilder = new StringBuilder(MAX_LINE_LENGTH);
    lineCommentBuilder = new StringBuilder(MAX_LINE_LENGTH);
    buffer.mark();

    while (buffer.hasRemaining()) {
      final int position;

      position = buffer.position();
      c = buffer.get();
      // System.out.println(position + ": " + c);

      if (c == COMMENT_META) {
        if (position >= 1 && buffer.get(position - 1) == '\\') {
          /* Escaped semicolons aren't comments. */
        } // NOPMD
        else if (buffer.remaining() >= 3
            && buffer.get(position + 1) == COMMENT_TAG
            && buffer.get(position + 2) == COMMENT_TAG
            && buffer.get(position + 3) != COMMENT_TAG) {
          /* Meta-Comment start detected ";--" */

          currentCommentLevel++;
          // System.out.println("Comment start, new level: " + currentCommentLevel);

          if (!inComment()) {
            commentBlock.append(";--");
            buffer.position(position + 3);
            buffer.mark();
            continue;
          }
        } else if (inComment()
            && position >= 2
            && buffer.get(position - 1) == COMMENT_TAG
            && buffer.get(position - 2) == COMMENT_TAG) {
          /* Meta-Comment end detected */

          currentCommentLevel--;

          if (!inComment()) {
            buffer.reset();

            // int commentLength = (position + 1) - buffer.position();

            // buffer.reset();
            // for (int i = 0; i < commentLength; i++)
            // {
            //    commentBlock.append(buffer.get());
            // }

            commentBlock.append(c);
            // System.out.println("Comment end at " + position + ": '" + commentBlock.toString() +
            // "'");

            buffer.position(position + 1);
            buffer.compact();
            buffer.flip();

            // System.out.println("Buffer compacted");
            continue;
          }
        } else {
          if (!inComment()) {
            /* If ; is found, and we are not nested in a comment, we immediately stop all comment processing */
            // System.out.println("Found ; while not in comment");
            while (buffer.hasRemaining()) {
              lineCommentBuilder.append(buffer.get());
            }
            break;
          } else {
            /* Found ';' while in comment */
          } // NOPMD
        }
      }

      if (inComment()) {
        commentBlock.append(c);
      } else {
        // System.out.println("Added '" + c + "' to processLine");
        processLineBuilder.append(c);
      }
    }

    String processLineString;
    String lineCommentString;
    ConfigElement configElement;

    processLineString = processLineBuilder.toString().trim();
    lineCommentString = lineCommentBuilder.toString().trim();

    // System.out.println("process line: '" + processLineString + "'");
    if (processLineString.length() == 0) {
      if (lineCommentString.length() != 0) {
        commentBlock.append(";");
        commentBlock.append(lineCommentString);
      }
      if (!inComment()) {
        commentBlock.append("\n");
      }
      return null;
    }

    try {
      configElement = processTextLine(configfile, lineno, processLineString);
    } catch (ConfigParseException e) {
      // some parsing exceptions are treated as warnings by Asterisk, we mirror this behavior.
      if (WARNING_CLASSES.contains(e.getClass())) {
        warnings.add(e);
        return null;
      } else {
        throw e;
      }
    }

    if (lineCommentString.length() != 0) {
      configElement.setComment(lineCommentString);
    }

    if (commentBlock.length() != 0) {
      configElement.setPreComment(commentBlock.toString());
      commentBlock.delete(0, commentBlock.length());
    }

    return configElement;
  }