Ejemplo n.º 1
0
  @NotNull
  private static Pair<CharSequence, String> convertLineSeparators(@NotNull CharBuffer buffer) {
    int dst = 0;
    char prev = ' ';
    int crCount = 0;
    int lfCount = 0;
    int crlfCount = 0;

    final int length = buffer.length();
    final char[] bufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer);

    for (int src = 0; src < length; src++) {
      char c = bufferArray != null ? bufferArray[src] : buffer.charAt(src);
      switch (c) {
        case '\r':
          if (bufferArray != null) bufferArray[dst++] = '\n';
          else buffer.put(dst++, '\n');
          crCount++;
          break;
        case '\n':
          if (prev == '\r') {
            crCount--;
            crlfCount++;
          } else {
            if (bufferArray != null) bufferArray[dst++] = '\n';
            else buffer.put(dst++, '\n');
            lfCount++;
          }
          break;
        default:
          if (bufferArray != null) bufferArray[dst++] = c;
          else buffer.put(dst++, c);
          break;
      }
      prev = c;
    }

    String detectedLineSeparator = null;
    if (crlfCount > crCount && crlfCount > lfCount) {
      detectedLineSeparator = "\r\n";
    } else if (crCount > lfCount) {
      detectedLineSeparator = "\r";
    } else if (lfCount > 0) {
      detectedLineSeparator = "\n";
    }

    CharSequence result;
    if (buffer.length() == dst) {
      result = buffer;
    } else {
      // in Mac JDK CharBuffer.subSequence() signature differs from Oracle's
      // more than that, the signature has changed between jd6 and jdk7,
      // so use more generic CharSequence.subSequence() just in case
      @SuppressWarnings("UnnecessaryLocalVariable")
      CharSequence seq = buffer;
      result = seq.subSequence(0, dst);
    }
    return Pair.create(result, detectedLineSeparator);
  }
 public void matchWhiteSpaceDelimnatedWords(CharBuffer cb, WordListener wl) {
   Matcher m = SPACE_PATTERN.matcher(cb);
   int i = 0;
   int s = 0;
   while (m.find()) {
     s = m.start();
     if (s != i) {
       wl.word(i, s);
     }
     i = m.end();
   }
   if (i != cb.length()) wl.word(i, cb.length());
 }
 /*    */ public void matchWhiteSpaceDelimnatedWords(CharBuffer cb, WordListener wl) /*    */ {
   /* 50 */ Matcher m = SPACE_PATTERN.matcher(cb);
   /* 51 */ int i = 0;
   /* 52 */ int s = 0;
   /* 53 */ while (m.find()) {
     /* 54 */ s = m.start();
     /* 55 */ if (s != i) {
       /* 56 */ wl.word(i, s);
       /*    */ }
     /* 58 */ i = m.end();
     /*    */ }
   /* 60 */ if (i != cb.length()) /* 61 */ wl.word(i, cb.length());
   /*    */ }
Ejemplo n.º 4
0
 private static String decode(Charset charset, byte[] b) {
   if (b == null) {
     return null;
   }
   final CharBuffer cb = charset.decode(ByteBuffer.wrap(b));
   return new String(cb.array(), 0, cb.length());
 }
Ejemplo n.º 5
0
 /**
  * Sets the contents of this string to the contents of the given <tt>CharBuffer</tt>. The
  * characters between the buffer's current position (inclusive) and the buffer's limit (exclusive)
  * will be stored in this string.
  *
  * @param buffer The character buffer to read the characters from.
  */
 public void setValue(CharBuffer buffer) {
   checkNotNull(buffer);
   final int len = buffer.length();
   ensureSize(len);
   buffer.get(this.value, 0, len);
   this.len = len;
   this.hashCode = 0;
 }
Ejemplo n.º 6
0
 /**
  * prepares the response for sending if a template is set, it will be rendered if no charbuffer is
  * present, even after rendering the template, there is nothing to send and prepareForSending will
  * just return
  */
 public void prepareForSending(CharBuffer cb) {
   if (cb == null || cb.length() < 1) return;
   try {
     buf = Charset.forName("iso-8859-1").newEncoder().encode(cb);
     return;
   } catch (CharacterCodingException cce) {
     Server.debug(this, "prepareForSending: ", cce, Server.MSG_ERROR, Server.LVL_MINOR);
   }
 }
  @Override
  public void run() {
    BufferedReader reader = null;
    try {
      // Path logpath = Paths.get(logname);
      // File posfile = ogpath.getParent().resolve("."+logpath.getFileName()+".pos").toFile();
      reader = new BufferedReader(new FileReader(new File(logname)));

      long filesize = 0;
      while (true) {
        // 判断文件是否已经切换
        if (filesize > new File(logname).length()) {
          logger.debug(
              "filesize :{}     current system file size :{} . Log file switchover!",
              filesize,
              new File(logname).length());
          try {
            // 在切换读文件前,读取文件全部内容
            StringBuilder line = new StringBuilder();
            while (reader.read(buf) > 0) {
              buf.flip();
              synchronized (buf) {
                // 读buffer 并解析
                for (int i = 0; i < buf.limit(); i++) {
                  char c = buf.get();
                  line.append(c);
                  if ((c == '\n') || (c == '\r'))
                    if (line.length() > 0) {
                      queue.put(line.toString());
                      line = new StringBuilder();
                    }
                }
              }
            }
            queue.put(line.toString());
            buf.clear();

            // 切换读文件
            if (reader != null) reader.close();
            reader = new BufferedReader(new FileReader(new File(logname)));
          } catch (Exception e) {
            logger.error("文件 {} 不存在", logname, e);
            Thread.currentThread().sleep(10000);
            continue;
          }
        }

        for (int retrys = 10; retrys > 0; retrys--) {
          int bufread = reader.read(buf);
          if (bufread < 0) {
            if (retrys > 0) Thread.currentThread().sleep(1000);
            else {
              // 等待10s后无新数据读出
              synchronized (buf) {
                // 等待 cachetime 秒后文件仍未写入
                buf.flip();
                char[] dst = new char[buf.length()];
                buf.get(dst);
                buf.clear();
                queue.put(new String(dst));
              }
            }
          } else {
            filesize = new File(logname).length();
            retrys = -1;

            buf.flip();
            synchronized (buf) {
              // 读buffer 并解析
              StringBuilder line = new StringBuilder();
              for (int i = 0; i < buf.limit(); i++) {
                char c = buf.get();
                line.append(c);
                if ((c == '\n') || (c == '\r'))
                  if (line.length() > 0) {
                    queue.put(line.toString());
                    line = new StringBuilder();
                  }
              }
              // 接着写不完整的数据
              buf.compact();
              if (line.length() > 0) {
                buf.append(line);
              }
            }
            break;
          }
        }
      }
    } catch (Exception e) {
      logger.error("文件读取失败", e);
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          logger.error("文件 reader 关闭失败", e);
        }
      }
    }
  }
Ejemplo n.º 8
0
 protected CharSequence getText(int index) {
   int beginIndex = textIndexArray[index];
   int endIndex = (index + 1 < numEntries) ? textIndexArray[index + 1] : textArray.length();
   return this.textArray.subSequence(beginIndex, endIndex);
 }
Ejemplo n.º 9
0
 protected @Override Token getNewToken() throws ScannerException {
   try {
     currentToken.setLength(0);
     if (charBuffer == null) {
       // get new text
       Server.writeAnswer(out, Server.MORE_RC, getContextString());
       resetContext();
       int msgSize = Server.readMessageSize(in);
       if (msgSize <= 0) throw new ScannerException("Bad message size", this);
       final int command = in.read();
       if (command == -1) throw new ScannerException("EOF while reading command", this);
       final byte[] msg = new byte[--msgSize];
       if (Io.read(in, msg) < msgSize)
         throw new ScannerException("EOF from client while reading text", this);
       switch (command) {
         case Server.QUIT_CMD:
           Server.writeAnswer(out, Server.GOODBYE_RC, "");
           throw new ScannerException("Client suddenly wants to quit", this);
         case Server.TEXT_CMD:
           charBuffer = Server.CHARSET.newDecoder().decode(ByteBuffer.wrap(msg));
           charBufferSize = charBuffer.length();
           charBufferPos = 0;
           break;
         case Server.FINISH_CMD:
           return null;
         default:
           Server.writeAnswer(out, Server.CLIENT_ERR_RC, "Command not allowed here");
       }
     }
     ParserState parserState = ParserState.INITIAL;
     while (charBufferPos < charBufferSize) {
       final char c = charBuffer.get(charBufferPos++);
       final Char.Class charClass = (new Char(c)).getCharClass();
       if (charClass == Char.Class.INVALID)
         throw new ScannerException("Invalid character '" + c + "'", this);
       switch (parserState) {
         case INITIAL:
           switch (charClass) {
             case OPEN_PAREN:
               currentToken.append(c);
               return BEGIN_EXP;
             case CLOSE_PAREN:
               currentToken.append(c);
               return END_EXP;
             case HASHMARK:
               parserState = ParserState.COMMENT;
               appendToContext("<span class=\"comment\">#");
               break;
             case ATOM:
               parserState = ParserState.ATOM;
               currentToken.append(c);
               break;
             default:
               appendToContext(escapeHTML(c));
               break;
           }
           break;
         case ATOM:
           switch (charClass) {
             case ATOM:
               currentToken.append(c);
               break;
             default:
               --charBufferPos; // parser backup, ATOM -> INITIAL implied
               return new TokenImpl(currentToken.toString(), Token.Class.ATOM);
           }
           break;
         case COMMENT:
           switch (charClass) {
             case NEWLINE:
               parserState = ParserState.INITIAL;
               appendToContext("</span><br />\n");
               break;
             default:
               appendToContext(escapeHTML(c));
               break;
           }
           break;
         default:
           throw new AssertionError("This should not happen");
       }
     }
     charBuffer = null;
     if (parserState == ParserState.ATOM)
       return new TokenImpl(currentToken.toString(), Token.Class.ATOM);
     if (parserState == ParserState.COMMENT) appendToContext("</span>");
     return getNewToken();
   } catch (IOException e) {
     throw new ScannerException("I/O error" + e.getMessage(), this, e);
   }
 }
Ejemplo n.º 10
0
  public static String encodeURL(String rawURLString, String charsetName, boolean escapeSpaces) {

    if (rawURLString == null) {
      return null;
    }

    if (rawURLString.length() == 0) {
      return StringPool.BLANK;
    }

    StringBuilder sb = null;

    CharsetEncoder charsetEncoder = null;

    char[] hexes = new char[2];

    for (int i = 0; i < rawURLString.length(); i++) {
      char c = rawURLString.charAt(i);

      if (_validChars.get(c)) {
        if (sb != null) {
          sb.append(c);
        }

        continue;
      }

      if (sb == null) {
        sb = new StringBuilder(rawURLString.length());

        sb.append(rawURLString.substring(0, i));
      }

      // The cases are ordered by frequency and not alphabetically

      switch (c) {
        case CharPool.SLASH:
          sb.append("%2F");

          continue;

        case CharPool.EQUAL:
          sb.append("%3D");

          continue;

        case CharPool.AMPERSAND:
          sb.append("%26");

          continue;

        case CharPool.PERCENT:
          sb.append("%25");

          continue;

        case CharPool.SPACE:
          if (escapeSpaces) {
            sb.append("%20");
          } else {
            sb.append(CharPool.PLUS);
          }

          continue;

        case CharPool.COLON:
          sb.append("%3A");

          continue;

        case CharPool.QUESTION:
          sb.append("%3F");

          continue;
      }

      CharBuffer charBuffer = _getRawCharBuffer(rawURLString, i);

      if (charsetEncoder == null) {
        charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(charsetName);
      }

      i += charBuffer.length() - 1;

      ByteBuffer byteBuffer = null;

      try {
        byteBuffer = charsetEncoder.encode(charBuffer);
      } catch (CharacterCodingException cce) {
        _log.error(cce, cce);

        return StringPool.BLANK;
      }

      for (int j = byteBuffer.position(); j < byteBuffer.limit(); j++) {
        sb.append(CharPool.PERCENT);

        sb.append(UnicodeFormatter.byteToHex(byteBuffer.get(), hexes, true));
      }
    }

    if (sb == null) {
      return rawURLString;
    } else {
      return sb.toString();
    }
  }