コード例 #1
0
 public int read(CharBuffer cb) {
   if (count-- == 0) return -1; // Indicates end of input
   cb.append(capitals[rand.nextInt(capitals.length)]);
   for (int i = 0; i < 4; i++) {
     cb.append(vowels[rand.nextInt(vowels.length)]);
     cb.append(lowers[rand.nextInt(lowers.length)]);
   }
   cb.append(" ");
   return 10; // Number of characters appended
 }
コード例 #2
0
  @Override
  public int read(CharBuffer cb) throws IOException {
    if (count-- == 0) {
      return -1;
    }
    cb.append(capitals[rand.nextInt(capitals.length)]);
    for (int i = 0; i < 4; i++) {
      cb.append(vowels[rand.nextInt(vowels.length)]);
      cb.append(lowers[rand.nextInt(lowers.length)]);
    }
    cb.append(" ");

    return 10;
  }
コード例 #3
0
  /**
   * Adds the data to the buffer for textual data. If a binary message is currently in progress that
   * message will be completed and a new textual message started. If the buffer for textual data is
   * full, the buffer will be flushed and a new textual continuation fragment started.
   *
   * @param c The character to send to the client.
   * @throws IOException If a flush is required and an error occurs writing the WebSocket frame to
   *     the client
   */
  public void writeTextData(char c) throws IOException {
    try {
      synchronized (stateLock) {
        if (closed) {
          throw new IOException(sm.getString("outbound.closed"));
        }

        if (cb.position() == cb.capacity()) {
          doFlush(false);
        }

        if (text == null) {
          text = Boolean.TRUE;
        } else if (text == Boolean.FALSE) {
          // Flush the binary data
          flush();
          text = Boolean.TRUE;
        }
        cb.append(c);
      }
    } catch (IOException ioe) {
      // Any IOException is terminal. Make sure the Inbound side knows
      // that something went wrong.
      // The exception handling needs to be outside of the sync to avoid
      // possible deadlocks (e.g. BZ55524) when triggering the inbound
      // close as that will execute user code
      streamInbound.doOnClose(Constants23.getStatusClosedUnexpectedly());
      throw ioe;
    }
  }
コード例 #4
0
  private void unescape() {
    int len = buffer.position();
    buffer.position(0);
    Matcher m = pattern.matcher(buffer);
    int pos = 0;
    StringBuilder tmp = new StringBuilder(len);
    String seq = null;
    while (m.find(pos)) {
      // Copy any previous text
      if (m.start() > pos) {
        // Get text before
        tmp.append(buffer.subSequence(pos, m.start()));
      }
      pos = m.end();

      // Treat the escape sequence
      seq = m.group();
      int value = -1;
      int uIndex = seq.indexOf('u');
      if (seq.indexOf('x') == 2) {
        // Hexadecimal NCR "&#xHHH;"
        value = Integer.parseInt(seq.substring(3, seq.length() - 1), 16);
      } else if ((uIndex == 1) && (seq.charAt(uIndex - 1) == '\\')) {
        // Java style "\ and uHHH"
        value = Integer.parseInt(seq.substring(2), 16);
      } else if (seq.indexOf('#') == 1) {
        // Decimal NCR "&#DDD;"
        value = Integer.parseInt(seq.substring(2, seq.length() - 1));
      } else {
        // Character entity reference: &NAME;
        seq = seq.substring(1, seq.length() - 1);
        // Unidentified is -1: leave it like that
        value = entities.lookupName(seq);
      }

      // Append the parsed escape
      if (value < 128) {
        // Unknown pattern or ASCII values: Keep it as it
        // (so <, &, ", etc.. stay escaped)
        tmp.append(m.group());
      } else {
        tmp.append((char) value);
      }
    }

    // Copy last part and re-build the buffer
    if (seq != null) { // We had at least one match
      if (pos < len) {
        // Get text before
        tmp.append(buffer.subSequence(pos, len));
      }
      // Reset the buffer
      buffer.clear();
      buffer.append(tmp.toString(), 0, tmp.length());
    } else { // Else: nothing to un-escape
      buffer.position(len);
    }
  }
コード例 #5
0
  public int read(CharBuffer cb) {
    if (count-- == 0) {
      return -1;
    }

    String result = Double.toString(next()) + " ";
    cb.append(result);
    return result.length();
  }
コード例 #6
0
  /**
   * Converts camelcase and underscores to a string with spaces with some intelligence.
   *
   * @param column
   * @return
   */
  public static String camelCaseToSentence(String in) {
    CharBuffer buffer = CharBuffer.allocate(in.length() + 100);

    boolean last = false; // was last a capital?
    boolean lastu = false; // was last a capital?

    char ch;
    char next = in.charAt(0);

    for (int i = 1; i < in.length(); ++i) {

      ch = next;
      next = in.charAt(i);

      if ((Character.isUpperCase(ch) && last == false)
          || (Character.isUpperCase(ch)
              && last == true
              && Character.isUpperCase(next) == false
              && next != '_')) {

        buffer.append(" ");
      }

      if (ch == '_') {
        buffer.append(" ");
      } else if (lastu) {
        buffer.append(Character.toUpperCase(ch));
      } else {
        buffer.append(ch);
      }

      last = Character.isUpperCase(ch) ? true : false;
      lastu = ch == '_' ? true : false;
    }

    buffer.append(next);

    buffer.put(0, Character.toUpperCase(buffer.get(0)));
    buffer.flip();

    return buffer.toString();
  }
コード例 #7
0
  public void testCopyHeapCharBuffer() {
    String s = "abcde";
    CharBuffer buffer = CharBuffer.allocate(s.length());
    buffer.append(s);
    buffer.rewind();

    assertNotNull(CharArrayUtil.fromSequenceWithoutCopying(buffer));
    assertNotNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(0, 5)));
    // assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(0, 4))); // end index
    // is not checked
    assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(1, 5)));
    assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(1, 2)));
  }
コード例 #8
0
  @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);
        }
      }
    }
  }
コード例 #9
0
  private String checkDeclaration(String defEncoding) {
    // Convert the CharBuffer to a string
    buffer.limit(buffer.position());
    buffer.position(0);
    StringBuffer text = new StringBuffer(buffer.toString());

    // Look for XML encoding declaration
    String encoding = defEncoding;
    Matcher m = xmlEncDecl.matcher(text);
    if (m.find()) { // We have an XML encoding declaration
      isXML = true;
      // Get the declared encoding
      String delim = String.valueOf(text.charAt(m.end() - 1));
      int end = text.indexOf(delim, m.end());
      if (end != -1) {
        encoding = text.substring(m.end(), end);
        // End replace the current declaration by the new one
        text.replace(m.end(), end, outputEncoding);
      }
    } else { // No XML encoding declaration found: Check if it is XML
      m = xmlDecl.matcher(text);
      if (m.find()) { // It is XML without encoding declaration
        isXML = true;
        // Encoding should UTF-8 or UTF-16/32, we will detect those later
        encoding = "UTF-8";
        // Add the encoding after the version
        String delim = String.valueOf(text.charAt(m.end() - 1));
        int end = text.indexOf(delim, m.end());
        if (end != -1) {
          text.insert(end + 1, " encoding=\"" + outputEncoding + "\"");
        }
      } else { // No XML declaration found, maybe it's an XML without one
        if (isXML) { // Was a .xml extension, assume UTF-8
          encoding = "UTF-8";
          text.insert(0, "<?xml version=\"1.0\" encoding=\"" + outputEncoding + "\" ?>");
        }
      }
    }

    // Look for HTML declarations
    m = htmlEncDecl.matcher(text);
    if (m.find()) {
      isHTML = true;
      // Group 11 contains the encoding name
      encoding = m.group(11);
      // Replace it by the new encoding
      int n = text.indexOf(encoding, m.start());
      text.replace(n, n + encoding.length(), outputEncoding);
    } else if (isHTML) { // No HTML encoding found, but try to update if it was seen as HTML from
                         // extension
      // Try to place it after <head>
      m = htmlHead.matcher(text);
      if (m.find()) {
        text.insert(
            m.end(),
            String.format(
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\"></meta>",
                outputEncoding));
      } else { // If no <head>, try <html>
        m = htmlDecl.matcher(text);
        if (m.find()) {
          int n = text.indexOf(">", m.end());
          if (n != -1) {
            text.insert(
                n + 1,
                String.format(
                    "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\"></meta></head>",
                    outputEncoding));
          }
        }
      }
    }

    // Convert the string back to a CharBuffer
    int len = text.length();
    // Make sure we have room for added characters
    if (len > buffer.capacity()) {
      buffer = CharBuffer.allocate(len);
    } else {
      buffer.clear();
    }
    buffer.append(text.toString());
    buffer.limit(len);
    return encoding;
  }
コード例 #10
0
  @Override
  protected Event handleRawDocument(Event event) {
    RawDocument rawDoc = (RawDocument) event.getResource();
    BufferedReader reader = null;
    OutputStreamWriter writer = null;
    try {
      // Try to detect the type of file from extension
      isXML = false;
      isHTML = false;
      String ext = Util.getExtension(inputURI.getPath());
      if (!Util.isEmpty(ext)) {
        isHTML = (ext.toLowerCase().indexOf(".htm") == 0);
        isXML = ext.equalsIgnoreCase(".xml");
      }

      // === Try to detect the encoding

      InputStream is = rawDoc.getStream();
      // First: guess from a possible BOM
      BOMNewlineEncodingDetector detector =
          new BOMNewlineEncodingDetector(is, rawDoc.getEncoding());
      detector.detectAndRemoveBom();
      rawDoc.setEncoding(detector.getEncoding());

      String inputEncoding = rawDoc.getEncoding();
      // Then try internal detection for XML/HTML type files
      if (!detector.isAutodetected()) {
        reader = new BufferedReader(rawDoc.getReader());
        reader.read(buffer);
        String detectedEncoding = checkDeclaration(inputEncoding);
        if (!detectedEncoding.equalsIgnoreCase(inputEncoding)) {
          inputEncoding = detectedEncoding;
        }
        reader.close();
      }

      // Open the input document
      // TODO: Where did we reset the reader - can't call this twice unless we reset it
      reader = new BufferedReader(rawDoc.getReader());
      logger.info("Input encoding: " + inputEncoding);

      // Open the output document
      File outFile;
      if (isLastOutputStep()) {
        outFile = rawDoc.createOutputFile(outputURI);
      } else {
        try {
          outFile = File.createTempFile("okp-enc_", ".tmp");
        } catch (Throwable e) {
          throw new OkapiIOException("Cannot create temporary output.", e);
        }
        outFile.deleteOnExit();
      }
      writer =
          new OutputStreamWriter(
              new BufferedOutputStream(new FileOutputStream(outFile)), outputEncoding);
      outputEncoder = Charset.forName(outputEncoding).newEncoder();
      logger.info("Output encoding: " + outputEncoding);
      Util.writeBOMIfNeeded(writer, params.BOMonUTF8, outputEncoding);

      int n;
      CharBuffer tmpBuf = CharBuffer.allocate(1);
      ByteBuffer encBuf;
      boolean canEncode;
      boolean checkDeclaration = true;

      while (true) {
        buffer.clear();
        // Start with previous buffer remains if needed
        if (prevBuf != null) {
          buffer.append(prevBuf);
        }
        // Read the next block
        n = reader.read(buffer);
        // Check if we need to stop here
        boolean needSplitCheck = true;
        if (n == -1) {
          // Make sure we do not start an endless loop by
          // re-checking the last previous buffer
          if (prevBuf != null) {
            needSplitCheck = false;
            prevBuf = null;
            buffer.limit(buffer.position());
          } else break; // No previous, no read: Done
        }

        if (checkDeclaration) {
          checkDeclaration(inputEncoding);
          checkDeclaration = false;
        }

        // Un-escape if requested
        if (pattern != null) {
          if (needSplitCheck) checkSplitSequence();
          unescape();
        }

        // Output
        n = buffer.position();
        buffer.position(0);
        for (int i = 0; i < n; i++) {
          if (!(canEncode = outputEncoder.canEncode(buffer.get(i)))) {
            if (params.reportUnsupported) {
              logger.warning(
                  String.format(
                      "Un-supported character: U+%04X ('%c')", (int) buffer.get(i), buffer.get(i)));
            }
          }

          if ((params.escapeAll && (buffer.get(i) > 127)) || !canEncode) {
            boolean fallBack = false;
            // Write escape form
            if (useCER) {
              String tmp = entities.getName(buffer.get(i));
              if (tmp == null) fallBack = true;
              else writer.write("&" + tmp + ";");
            } else {
              if (params.useBytes) { // Escape bytes
                if (canEncode) {
                  tmpBuf.put(0, buffer.get(i));
                  tmpBuf.position(0);
                  encBuf = outputEncoder.encode(tmpBuf);
                  for (int j = 0; j < encBuf.limit(); j++) {
                    writer.write(
                        String.format(
                            outFormat,
                            (encBuf.get(j) < 0 ? (0xFF ^ ~encBuf.get(j)) : encBuf.get(j))));
                  }
                } else fallBack = true;
              } else { // Escape character
                writer.write(String.format(outFormat, (int) buffer.get(i)));
              }
            }
            if (fallBack) { // Default escaping when nothing else works
              writer.write(String.format("&#x%X;", (int) buffer.get(i)));
            }
          } else { // Normal raw forms
            writer.write(buffer.get(i));
          }
        }
      }

      // Done: close the files
      reader.close();
      reader = null;
      writer.close();
      writer = null;
      rawDoc.finalizeOutput();

      // Set the new raw-document URI and the encoding (in case one was auto-detected)
      // Other info stays the same
      RawDocument newDoc =
          new RawDocument(
              outFile.toURI(), outputEncoding, rawDoc.getSourceLocale(), rawDoc.getTargetLocale());
      event.setResource(newDoc);

    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      try {
        if (writer != null) {
          writer.close();
          writer = null;
        }
        if (reader != null) {
          reader.close();
          reader = null;
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    return event;
  }