/** Recycle this <code>InputBuffer</code> for reuse. */
  public void recycle() {

    inputContentBuffer.tryDispose();
    inputContentBuffer = null;

    singleCharBuf.position(singleCharBuf.limit());

    connection = null;
    decoder = null;
    ctx = null;
    handler = null;

    processingChars = false;
    closed = false;
    contentRead = false;

    markPos = -1;
    readAheadLimit = -1;
    requestedSize = -1;
    readCount = 0;

    averageCharsPerByte = 1.0f;

    isWaitingDataAsynchronously = false;

    encoding = DEFAULT_HTTP_CHARACTER_ENCODING;
  }
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int len = Math.min(dl - dp, sl - sp);

      while (len-- > 0) {
        char c = sa[sp];
        int b = encode(c);
        if (b == UNMAPPABLE_ENCODING) {
          if (Character.isSurrogate(c)) {
            if (sgp == null) sgp = new Surrogate.Parser();
            if (sgp.parse(c, sa, sp, sl) < 0) {
              return withResult(sgp.error(), src, sp, dst, dp);
            }
            return withResult(sgp.unmappableResult(), src, sp, dst, dp);
          }
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = (byte) b;
        sp++;
      }
      return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW, src, sp, dst, dp);
    }
Esempio n. 3
0
 private void fillByteBuffer(Reader reader) throws IOException {
   CharBuffer cbuf = CharBuffer.allocate(DEFAULT_CHAR_BUFFER_SIZE);
   ByteBuffer bbuf = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
   List<byte[]> list = new ArrayList<byte[]>();
   while (true) {
     cbuf.clear();
     int size = reader.read(cbuf);
     if (size <= 0) {
       break;
     }
     cbuf.limit(cbuf.position());
     cbuf.rewind();
     boolean eof = false;
     while (!eof) {
       CoderResult cr = encoder.encode(cbuf, bbuf, eof);
       if (cr.isError()) {
         cr.throwException();
       } else if (cr.isUnderflow()) {
         appendBytes(list, bbuf);
         eof = true;
       } else if (cr.isOverflow()) {
         appendBytes(list, bbuf);
         bbuf.clear();
       }
     }
   }
   getByteArray(list);
 }
  @JRubyMethod
  public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) {
    if (!(srcBuffer instanceof RubyString)) {
      throw context.runtime.newTypeError(srcBuffer, context.runtime.getString());
    }

    RubyString srcString = (RubyString) srcBuffer;

    ByteList srcBL = srcString.getByteList();

    if (srcBL.getRealSize() == 0) return context.runtime.newSymbol("source_buffer_empty");

    ByteBuffer srcBB = ByteBuffer.wrap(srcBL.getUnsafeBytes(), srcBL.begin(), srcBL.getRealSize());
    try {
      CharBuffer srcCB =
          CharBuffer.allocate((int) (srcDecoder.maxCharsPerByte() * srcBL.getRealSize()) + 1);
      CoderResult decodeResult = srcDecoder.decode(srcBB, srcCB, true);
      srcCB.flip();

      ByteBuffer destBB =
          ByteBuffer.allocate((int) (destEncoder.maxBytesPerChar() * srcCB.limit()) + 1);
      CoderResult encodeResult = destEncoder.encode(srcCB, destBB, true);
      destBB.flip();

      byte[] destBytes = new byte[destBB.limit()];
      destBB.get(destBytes);

      srcDecoder.reset();
      destEncoder.reset();

      return context.runtime.newString(new ByteList(destBytes, destEncoding.getEncoding(), false));
    } catch (Exception e) {
      throw context.runtime.newRuntimeError(e.getLocalizedMessage());
    }
  }
Esempio n. 5
0
    private void endBuffer(ZipEntry ze) {
      if (ze == null) {
        return;
      }
      if (store != null) {
        store.close(); // 終了処理は別スレッドで実行中
        String digest = store.getDigest();
        logPersister.addDigest(addDigests, digest);
        store = null;
      } else if (charsetDecoder != null) {
        charBuffer.flip();
        charBuffer.array();
        String accessLogJson =
            new String(charBuffer.array(), charBuffer.position(), charBuffer.limit());
        charsetDecoder = null;
        charBuffer = null;
        AccessLog accessLog = AccessLog.fromJson(accessLogJson);
        if (accessLog == null) {
          return;
        }
        logPersister.addDigest(refDigests, accessLog.getRequestHeaderDigest());
        logPersister.addDigest(refDigests, accessLog.getRequestBodyDigest());
        logPersister.addDigest(refDigests, accessLog.getResponseHeaderDigest());
        logPersister.addDigest(refDigests, accessLog.getResponseBodyDigest());
        accessLog.setId(null);
        accessLog.setPersist(true);

        PersistenceManager pm = JdoUtil.getPersistenceManager();
        logPersister.executeInsert(pm, accessLog);
        if (pm.currentTransaction().isActive()) {
          pm.currentTransaction().rollback();
        }
      }
    }
Esempio n. 6
0
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();
      assert (sp <= sl);
      sp = (sp <= sl ? sp : sl);
      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      assert (dp <= dl);
      dp = (dp <= dl ? dp : dl);

      try {
        while (sp < sl) {
          byte b = sa[sp];
          if (b >= 0) {
            if (dp >= dl) return CoderResult.OVERFLOW;
            da[dp++] = (char) b;
            sp++;
            continue;
          }
          return CoderResult.malformedForLength(1);
        }
        return CoderResult.UNDERFLOW;
      } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
      }
    }
Esempio n. 7
0
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();
      assert (sp <= sl);
      sp = (sp <= sl ? sp : sl);
      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      assert (dp <= dl);
      dp = (dp <= dl ? dp : dl);

      try {
        while (sp < sl) {
          char c = sa[sp];
          if (c < 0x80) {
            if (dp >= dl) return CoderResult.OVERFLOW;
            da[dp] = (byte) c;
            sp++;
            dp++;
            continue;
          }
          if (sgp.parse(c, sa, sp, sl) < 0) return sgp.error();
          return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
      } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
      }
    }
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();

      CoderResult cr = CoderResult.UNDERFLOW;
      if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
      }

      while (sp < sl) {
        char c = decode(sa[sp]);
        if (c == UNMAPPABLE_DECODING) {
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = c;
        sp++;
      }
      return withResult(cr, src, sp, dst, dp);
    }
Esempio n. 9
0
 public static void main(String args[]) throws Exception {
   String s = "abc\uD800\uDC00qrst"; // Valid surrogate
   char[] c = s.toCharArray();
   CharsetEncoder enc =
       Charset.forName("ISO8859_1").newEncoder().onUnmappableCharacter(CodingErrorAction.REPLACE);
   /* Process the first 4 characters, including the high surrogate
   which should be stored */
   ByteBuffer bb = ByteBuffer.allocate(10);
   CharBuffer cb = CharBuffer.wrap(c);
   cb.limit(4);
   enc.encode(cb, bb, false);
   cb.limit(7);
   enc.encode(cb, bb, true);
   byte[] first = bb.array();
   for (int i = 0; i < 7; i++)
     System.err.printf("[%d]=%d was %d\n", i, (int) first[i] & 0xffff, (int) c[i] & 0xffff);
 }
Esempio n. 10
0
    @Override
    public void onStdoutChars(CharBuffer buffer, boolean closed, CoderResult coderResult) {
      charsRead += buffer.remaining();
      decodedStdout.append(buffer);
      buffer.position(buffer.limit());

      if (charsRead == charsWritten) {
        nuProcess.closeStdin();
      }
    }
Esempio n. 11
0
 private boolean trimWhitespaces() {
   boolean trim = false;
   for (int i = lineBuffer.position(), n = lineBuffer.limit(); i < n; i++) {
     char c = lineBuffer.get(i);
     if (Character.isWhitespace(c)) {
       trim = true;
       lineBuffer.position(i + 1);
     } else {
       break;
     }
   }
   for (int i = lineBuffer.limit() - 1, n = lineBuffer.position(); i >= n; i--) {
     char c = lineBuffer.get(i);
     if (Character.isWhitespace(c)) {
       trim = true;
       lineBuffer.limit(i);
     } else {
       break;
     }
   }
   return trim;
 }
  private void fillCharBuf() throws IOException {
    int limit = 0;
    int c = 0;
    do {
      c = _delegate.read();
      if (c >= 0) {
        _charBuf.put((char) c);
        limit++;
      }
    } while ((c >= 0xD800) && (c <= 0xDBFF));

    _charBuf.position(0);
    _charBuf.limit(limit);
  }
Esempio n. 13
0
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int dlASCII = dp + Math.min(sl - sp, dl - dp);

      // ASCII only loop
      while (dp < dlASCII && sa[sp] < '\u0080') da[dp++] = (byte) sa[sp++];
      while (sp < sl) {
        char c = sa[sp];
        if (c < 0x80) {
          // Have at most seven bits
          if (dp >= dl) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) c;
        } else if (c < 0x800) {
          // 2 bytes, 11 bits
          if (dl - dp < 2) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xc0 | (c >> 6));
          da[dp++] = (byte) (0x80 | (c & 0x3f));
        } else if (isSurrogate(c)) {
          // Have a surrogate pair
          if (sgp == null) sgp = new Parser();
          int uc = sgp.parse(c, sa, sp, sl);
          if (uc < 0) {
            updatePositions(src, sp, dst, dp);
            return sgp.error();
          }
          if (dl - dp < 4) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xf0 | ((uc >> 18)));
          da[dp++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
          da[dp++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
          da[dp++] = (byte) (0x80 | (uc & 0x3f));
          sp++; // 2 chars
        } else {
          // 3 bytes, 16 bits
          if (dl - dp < 3) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xe0 | ((c >> 12)));
          da[dp++] = (byte) (0x80 | ((c >> 6) & 0x3f));
          da[dp++] = (byte) (0x80 | (c & 0x3f));
        }
        sp++;
      }
      updatePositions(src, sp, dst, dp);
      return CoderResult.UNDERFLOW;
    }
 @Override
 public void run() {
   try {
     InputStream in = client.getInputStream();
     CharBuffer buffer = CharBuffer.allocate(5000);
     char ch;
     while (true) {
       ch = (char) in.read();
       if (((int) ch) == -1) {
         break;
       }
       while (ch != EOL) {
         buffer.put(ch);
         ch = (char) in.read();
       }
       buffer.flip();
       String s = new String(buffer.array(), 0, buffer.limit());
       messageReceiver.sendMessage(s + "\n");
       buffer.position(0);
       buffer.limit(5000);
     }
   } catch (Exception e) {
     e.printStackTrace();
     if (LOGGER.isLoggable(Level.WARNING)) {
       LOGGER.warning("Unable to process mesages from client" + client);
     }
   } finally {
     if (client != null) {
       try {
         client.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
 }
Esempio n. 15
0
  public int availableChar() {

    if (!singleCharBuf.hasRemaining()) {
      // fill the singleCharBuf to make sure we have at least one char
      singleCharBuf.clear();
      if (fillAvailableChars(1, singleCharBuf) == 0) {
        singleCharBuf.position(singleCharBuf.limit());
        return 0;
      }

      singleCharBuf.flip();
    }

    // we have 1 char pre-decoded + estimation for the rest byte[]->char[] count.
    return 1 + ((int) (inputContentBuffer.remaining() * averageCharsPerByte));
  }
Esempio n. 16
0
 private void seekBuffer() throws CsvFormatException {
   if (cellBeginPositions.remaining() < 2) {
     throw new CsvFormatException(
         new Status(
             Reason.TOO_SHORT_RECORD,
             path,
             currentPhysicalHeadLine,
             currentRecordNumber,
             cellBeginPositions.position() + 1,
             "more cells",
             "no more cells"),
         null);
   }
   lineBuffer.limit(cellBeginPositions.get(cellBeginPositions.position() + 1));
   lineBuffer.position(cellBeginPositions.get());
 }
  @NotNull
  public static IndexedCollection<Character> ofCharacter(@NotNull ByteBuffer byteBuffer) {
    int magic = byteBuffer.getInt();
    if (magic != MAGIC) {
      throw new IllegalArgumentException("bad magic number");
    }
    int version = byteBuffer.getInt();
    if (version != VERSION) {
      throw new IllegalArgumentException("bad version number");
    }

    int size = byteBuffer.getInt();
    CharBuffer values = byteBuffer.asCharBuffer();
    values.limit(size);
    byteBuffer.position(byteBuffer.position() + size * Integer.BYTES);
    return new CharacterBufferCollection(values.slice());
  }
Esempio n. 18
0
 private void checkSplitSequence() {
   int len = buffer.position();
   buffer.position(0);
   // Search for the first & or \ in the last 10 (or less) characters
   prevBuf = null;
   int j = 0;
   for (int i = len - 1; ((i >= 0) && (j < 10)); i--) {
     if ((buffer.charAt(i) == '&') || (buffer.charAt(i) == '\\')) {
       prevBuf = buffer.subSequence(i, len).toString();
       len = i;
       break;
     }
     j++;
   }
   buffer.position(len);
   buffer.limit(len);
 }
  public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {
    boolean marked = false;

    try {
      slice.reset();

      marked = true;
    } catch (InvalidMarkException ime) {
      // expected
    }

    if (marked
        || slice.position() != 0
        || buff.remaining() != slice.limit()
        || buff.remaining() != slice.capacity()) {

      throw new RuntimeException("Calling the CharBuffer.slice method failed.");
    }
  }
  private void fillBuffer() throws IOException {
    _byteBuf.clear();
    _charBuf.clear();

    while (_byteBuf.position() == 0) {
      fillCharBuf();
      if (_charBuf.limit() == 0) {
        _byteBuf.limit(0);
        return;
      }

      _encoder.reset();
      CoderResult rslt = _encoder.encode(_charBuf, _byteBuf, true);
      // FIXME - optionally throw on malformed input
      _encoder.flush(_byteBuf);
    }

    _byteBuf.limit(_byteBuf.position());
    _byteBuf.position(0);
  }
Esempio n. 21
0
 private void consumeDecoded() {
   decodeBuffer.flip();
   if (decodeBuffer.hasRemaining()) {
     char[] array = decodeBuffer.array();
     for (int i = decodeBuffer.position(), n = decodeBuffer.limit(); i < n; i++) {
       char c = array[i];
       if (c == '\t') {
         lineBuffer.append(ESCAPE_CHAR);
         lineBuffer.append(ESCAPE_HT);
       } else if (c == '\n') {
         lineBuffer.append(ESCAPE_CHAR);
         lineBuffer.append(ESCAPE_LF);
       } else if (c == '\\') {
         lineBuffer.append(ESCAPE_CHAR);
         lineBuffer.append(ESCAPE_CHAR);
       } else {
         lineBuffer.append(c);
       }
     }
   }
   decodeBuffer.clear();
 }
  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();
  }
Esempio n. 23
0
  /**
   * Creates a new big-endian buffer whose content is a subregion of the specified {@code string}
   * encoded in the specified {@code charset}. The new buffer's {@code readerIndex} and {@code
   * writerIndex} are {@code 0} and the length of the encoded string respectively.
   */
  public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
      throw new NullPointerException("string");
    }
    if (length == 0) {
      return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
      CharBuffer buf = (CharBuffer) string;
      if (buf.hasArray()) {
        return copiedBuffer(
            buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset);
      }

      buf = buf.slice();
      buf.limit(length);
      buf.position(offset);
      return copiedBuffer(buf, charset);
    }

    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
  }
 /**
  * Returns the password that is bound to the username and is stored for the specified <code>
  * forUser</code> If the <code>forUser</code> is not specified the system user is used.
  *
  * @param forUser the user for whom the password is being retrieved
  * @param username the matching username to go with the password
  * @return the password as a character array or empty object if password could not be loaded
  */
 public static Optional<char[]> getPassword(Optional<String> forUser, String username) {
   if (username == null) {
     return Optional.empty();
   }
   final String user = forUser.orElse(System.getProperty(SYSTEM_PROPERTY_USER_NAME));
   try {
     byte[] val =
         SecurePreferences.getSecurePreferences()
             .node(Activator.ID)
             .node(user)
             .node(username)
             .getByteArray(PREF_PASSWORD, null);
     if (val != null) {
       CharBuffer buffer = StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(val));
       return Optional.of(Arrays.copyOfRange(buffer.array(), buffer.position(), buffer.limit()));
     }
   } catch (StorageException | IOException e) {
     SaveRestoreService.LOGGER.log(
         Level.WARNING,
         e,
         () -> String.format("Could not read the password for '%s' from secured storage.", user));
   }
   return Optional.empty();
 }
Esempio n. 25
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);
      }
    }
Esempio n. 26
0
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      // This method is optimized for ASCII input.
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int dlASCII = dp + Math.min(sl - sp, dl - dp);

      // ASCII only loop
      while (dp < dlASCII && sa[sp] >= 0) da[dp++] = (char) sa[sp++];
      while (sp < sl) {
        int b1 = sa[sp];
        if (b1 >= 0) {
          // 1 byte, 7 bits: 0xxxxxxx
          if (dp >= dl) return xflow(src, sp, sl, dst, dp, 1);
          da[dp++] = (char) b1;
          sp++;
        } else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) {
          // 2 bytes, 11 bits: 110xxxxx 10xxxxxx
          if (sl - sp < 2 || dp >= dl) return xflow(src, sp, sl, dst, dp, 2);
          int b2 = sa[sp + 1];
          if (isNotContinuation(b2)) return malformedForLength(src, sp, dst, dp, 1);
          da[dp++] = (char) (((b1 << 6) ^ b2) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80)));
          sp += 2;
        } else if ((b1 >> 4) == -2) {
          // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
          int srcRemaining = sl - sp;
          if (srcRemaining < 3 || dp >= dl) {
            if (srcRemaining > 1 && isMalformed3_2(b1, sa[sp + 1]))
              return malformedForLength(src, sp, dst, dp, 1);
            return xflow(src, sp, sl, dst, dp, 3);
          }
          int b2 = sa[sp + 1];
          int b3 = sa[sp + 2];
          if (isMalformed3(b1, b2, b3)) return malformed(src, sp, dst, dp, 3);
          char c =
              (char)
                  ((b1 << 12)
                      ^ (b2 << 6)
                      ^ (b3 ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80))));
          if (isSurrogate(c)) return malformedForLength(src, sp, dst, dp, 3);
          da[dp++] = c;
          sp += 3;
        } else if ((b1 >> 3) == -2) {
          // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
          int srcRemaining = sl - sp;
          if (srcRemaining < 4 || dl - dp < 2) {
            if (srcRemaining > 1 && isMalformed4_2(b1, sa[sp + 1]))
              return malformedForLength(src, sp, dst, dp, 1);
            if (srcRemaining > 2 && isMalformed4_3(sa[sp + 2]))
              return malformedForLength(src, sp, dst, dp, 2);
            return xflow(src, sp, sl, dst, dp, 4);
          }
          int b2 = sa[sp + 1];
          int b3 = sa[sp + 2];
          int b4 = sa[sp + 3];
          int uc =
              ((b1 << 18)
                  ^ (b2 << 12)
                  ^ (b3 << 6)
                  ^ (b4
                      ^ (((byte) 0xF0 << 18)
                          ^ ((byte) 0x80 << 12)
                          ^ ((byte) 0x80 << 6)
                          ^ ((byte) 0x80))));
          if (isMalformed4(b2, b3, b4)
              ||
              // shortest form check
              !Character.isSupplementaryCodePoint(uc)) {
            return malformed(src, sp, dst, dp, 4);
          }
          da[dp++] = highSurrogate(uc);
          da[dp++] = lowSurrogate(uc);
          sp += 4;
        } else return malformed(src, sp, dst, dp, 1);
      }
      return xflow(src, sp, sl, dst, dp, 0);
    }
Esempio n. 27
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;
  }
  @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);
        }
      }
    }
  }
  public static void main(String[] args) throws Exception {
    System.out.println(">>> StringCharBufferSliceTest-main: testing the slice method...");

    final String in = "for testing";

    System.out.println(">>> StringCharBufferSliceTest-main: testing with the position 0.");

    CharBuffer buff = CharBuffer.wrap(in);
    test(buff, buff.slice());

    System.out.println(">>> StringCharBufferSliceTest-main: testing with new position.");

    buff.position(2);
    test(buff, buff.slice());

    System.out.println(
        ">>> StringCharBufferSliceTest-main: testing with non zero initial position.");

    buff = CharBuffer.wrap(in, 3, in.length());
    test(buff, buff.slice());

    System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get()");
    buff.position(4);
    buff.limit(7);
    CharBuffer slice = buff.slice();
    for (int i = 0; i < 3; i++) {
      if (slice.get() != buff.get()) {
        throw new RuntimeException("Wrong characters in slice result.");
      }
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
    buff.position(4);
    buff.limit(7);
    slice = buff.slice();
    for (int i = 0; i < 3; i++) {
      if (slice.get(i) != buff.get(4 + i)) {
        throw new RuntimeException("Wrong characters in slice result.");
      }
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing slice with result of slice");
    buff.position(0);
    buff.limit(buff.capacity());
    slice = buff.slice();
    for (int i = 0; i < 4; i++) {
      slice.position(i);
      CharBuffer nextSlice = slice.slice();
      if (nextSlice.position() != 0)
        throw new RuntimeException("New buffer's position should be zero");
      if (!nextSlice.equals(slice)) throw new RuntimeException("New buffer should be equal");
      slice = nextSlice;
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing toString.");
    buff.position(4);
    buff.limit(7);
    slice = buff.slice();
    if (!slice.toString().equals("tes")) {
      throw new RuntimeException("bad toString() after slice(): " + slice.toString());
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing subSequence.");
    buff.position(4);
    buff.limit(8);
    slice = buff.slice();
    CharSequence subSeq = slice.subSequence(1, 3);
    if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
      throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
    }

    System.out.println(">>> StringCharBufferSliceTest-main: testing duplicate.");
    buff.position(4);
    buff.limit(8);
    slice = buff.slice();
    CharBuffer dupe = slice.duplicate();
    if (dupe.charAt(0) != 't'
        || dupe.charAt(1) != 'e'
        || dupe.charAt(2) != 's'
        || dupe.charAt(3) != 't') {
      throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
    }

    System.out.println(">>> StringCharBufferSliceTest-main: done!");
  }
Esempio n. 30
0
 protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
   int bbRemaining = bb.remaining();
   if (CharsetProviderImpl.hasLoadedNatives()
       && bb.isDirect()
       && cb.hasRemaining()
       && cb.hasArray()) {
     int toProceed = cb.remaining();
     int cbPos = cb.position();
     int bbPos = bb.position();
     boolean throwOverflow = false;
     if (bbRemaining < toProceed) {
       toProceed = bbRemaining;
       throwOverflow = true;
     }
     int[] res = {toProceed, 0};
     nEncode(
         AddressUtil.getDirectBufferAddress(bb),
         bbPos,
         cb.array(),
         cb.arrayOffset() + cbPos,
         res);
     if (res[0] <= 0) {
       bb.position(bbPos - res[0]);
       cb.position(cbPos - res[0]);
       if (res[1] != 0) {
         if (res[1] < 0) return CoderResult.malformedForLength(-res[1]);
         else return CoderResult.unmappableForLength(res[1]);
       }
     } else {
       bb.position(bbPos + res[0]);
       cb.position(cbPos + res[0]);
       if (throwOverflow) return CoderResult.OVERFLOW;
     }
   } else {
     if (bb.hasArray() && cb.hasArray()) {
       byte[] byteArr = bb.array();
       char[] charArr = cb.array();
       int rem = cb.remaining();
       int byteArrStart = bb.position();
       rem = bbRemaining <= rem ? bbRemaining : rem;
       int x;
       for (x = cb.position(); x < cb.position() + rem; x++) {
         char c = charArr[x];
         if (c > (char) 0x20AC) {
           if (c >= 0xD800 && c <= 0xDFFF) {
             if (x + 1 < cb.limit()) {
               char c1 = charArr[x + 1];
               if (c1 >= 0xD800 && c1 <= 0xDFFF) {
                 cb.position(x);
                 bb.position(byteArrStart);
                 return CoderResult.unmappableForLength(2);
               }
             } else {
               cb.position(x);
               bb.position(byteArrStart);
               return CoderResult.UNDERFLOW;
             }
             cb.position(x);
             bb.position(byteArrStart);
             return CoderResult.malformedForLength(1);
           }
           cb.position(x);
           bb.position(byteArrStart);
           return CoderResult.unmappableForLength(1);
         } else {
           if (c < 0x04) {
             byteArr[byteArrStart++] = (byte) c;
           } else {
             int index = (int) c >> 8;
             index = encodeIndex[index];
             if (index < 0) {
               cb.position(x);
               bb.position(byteArrStart);
               return CoderResult.unmappableForLength(1);
             }
             index <<= 8;
             index += (int) c & 0xFF;
             if ((byte) arr[index] != 0) {
               byteArr[byteArrStart++] = (byte) arr[index];
             } else {
               cb.position(x);
               bb.position(byteArrStart);
               return CoderResult.unmappableForLength(1);
             }
           }
         }
       }
       cb.position(x);
       bb.position(byteArrStart);
       if (rem == bbRemaining && cb.hasRemaining()) {
         return CoderResult.OVERFLOW;
       }
     } else {
       while (cb.hasRemaining()) {
         if (bbRemaining == 0) return CoderResult.OVERFLOW;
         char c = cb.get();
         if (c > (char) 0x20AC) {
           if (c >= 0xD800 && c <= 0xDFFF) {
             if (cb.hasRemaining()) {
               char c1 = cb.get();
               if (c1 >= 0xD800 && c1 <= 0xDFFF) {
                 cb.position(cb.position() - 2);
                 return CoderResult.unmappableForLength(2);
               } else {
                 cb.position(cb.position() - 1);
               }
             } else {
               cb.position(cb.position() - 1);
               return CoderResult.UNDERFLOW;
             }
             cb.position(cb.position() - 1);
             return CoderResult.malformedForLength(1);
           }
           cb.position(cb.position() - 1);
           return CoderResult.unmappableForLength(1);
         } else {
           if (c < 0x04) {
             bb.put((byte) c);
           } else {
             int index = (int) c >> 8;
             index = encodeIndex[index];
             if (index < 0) {
               cb.position(cb.position() - 1);
               return CoderResult.unmappableForLength(1);
             }
             index <<= 8;
             index += (int) c & 0xFF;
             if ((byte) arr[index] != 0) {
               bb.put((byte) arr[index]);
             } else {
               cb.position(cb.position() - 1);
               return CoderResult.unmappableForLength(1);
             }
           }
           bbRemaining--;
         }
       }
     }
   }
   return CoderResult.UNDERFLOW;
 }