Ejemplo n.º 1
0
  public ByteBuffer convertChunk(
      byte[] b, int offset, int length, ByteBuffer dst, boolean endOfInput) throws SVNException {
    myInputByteBuffer = allocate(myInputByteBuffer, length);
    myInputByteBuffer.put(b, offset, length);
    myInputByteBuffer.flip();
    myCharBuffer =
        allocate(myCharBuffer, (int) (myDecoder.maxCharsPerByte() * myInputByteBuffer.remaining()));

    CoderResult result = myDecoder.decode(myInputByteBuffer, myCharBuffer, endOfInput);
    if (result.isError()) {
      throwException(result);
    } else if (result.isUnderflow()) {
      myInputByteBuffer.compact();
    } else {
      myInputByteBuffer.clear();
    }

    myCharBuffer.flip();
    dst = allocate(dst, (int) (myEncoder.maxBytesPerChar() * myCharBuffer.remaining()));

    result = myEncoder.encode(myCharBuffer, dst, false);
    if (result.isError()) {
      throwException(result);
    } else if (result.isUnderflow()) {
      myCharBuffer.compact();
    } else {
      myCharBuffer.clear();
    }

    return dst;
  }
Ejemplo n.º 2
0
 private void fillBuffer() throws IOException {
   if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) {
     encoderIn.compact();
     int i = encoderIn.position();
     int j = reader.read(encoderIn.array(), i, encoderIn.remaining());
     if (j == -1) {
       endOfInput = true;
     } else {
       encoderIn.position(i + j);
     }
     encoderIn.flip();
   }
   encoderOut.compact();
   lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput);
   encoderOut.flip();
 }
  @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.º 4
0
    @Override
    public void read(Multiplexer multiplexer, SelectionKey key) throws Exception {
      /* Protect against clients sending large commands. We
       * could enlarge the command buffer, but this would open
       * the server to DOS attacks from clients sending very
       * large commands.
       */
      if (!_command.hasRemaining()) {
        throw new FTPException("Command buffer full");
      }

      /* Read available data.
       */
      long nbytes = _socket.read(_command);
      if (nbytes == -1) {
        if (_state == SenderState.WAIT_READY) {
          /* From the GridFTP v2 spec: "Passive receiver may
           * close new data socket without sending 'READY'
           * message or even stop accepting new
           * connections."
           *
           * We extend this to also cover active receivers.
           */
          close(multiplexer, key, _eof);
          return;
        } else {
          throw new FTPException("Lost connection");
        }
      }

      /* Decode buffer.
       */
      _command.flip();
      _decoder.decode(_command, _decodedCommand, false);
      _command.compact();

      /* Remove first line from buffer.
       */
      char c;
      StringBuffer line = new StringBuffer();
      _decodedCommand.flip();
      do {
        /* Return early if command is incomplete.
         */
        if (!_decodedCommand.hasRemaining()) {
          _decodedCommand.limit(_decodedCommand.capacity());
          return;
        }
        c = _decodedCommand.get();
        line.append(c);
      } while (c != '\n');
      _decodedCommand.compact();

      /* Split line into arguments.
       */
      String[] arg = Pattern.compile("\\s").split(line);
      if (arg.length == 0) {
        throw new FTPException("Empty command received (protocol violation)");
      }

      /* Interpret command.
       */
      String cmd = arg[0];
      if (cmd.equals("READY") && _state == SenderState.WAIT_READY) {
        _state = SenderState.NEXT_BLOCK;
        key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
      } else if (cmd.equals("BYE") && _state == SenderState.WAIT_BYE) {
        // shut down
        close(multiplexer, key, _eof);
      } else if (cmd.equals("CLOSE")) {
        // shutdown the channel at the end of the current block
        _closeAtNextBlock = true;
      } else if (cmd.equals("RESEND")) {
        // resend a block
        throw new FTPException("RESEND is not implemented");
      } else {
        throw new FTPException("Unexpected command '" + cmd + "' in state " + _state);
      }
    }
Ejemplo n.º 5
0
  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;
  }