コード例 #1
0
  public ByteBuffer encode(
      final CodePointBuffer text, ByteBuffer buffer, final EncodingErrorType errorHandling)
      throws EncodingException {
    final int textLength = text.getLength();
    if (buffer == null) {
      buffer = new ByteBuffer(textLength);
    } else if (buffer.getLength() < textLength) {
      buffer.ensureSize(textLength);
    }

    final byte[] targetArray = buffer.getData();
    final int[] sourceArray = text.getData();

    int targetIdx = buffer.getOffset();
    final int endPos = text.getCursor();
    for (int i = text.getOffset(); i < endPos; i++) {
      final int sourceItem = sourceArray[i];
      if (isUnicodeCharacterSupported(sourceItem)) {
        targetArray[targetIdx] = (byte) (sourceItem & 0xff);
        targetIdx += 1;
      } else {
        if (EncodingErrorType.REPLACE.equals(errorHandling)) {
          targetArray[targetIdx] = (byte) ('?' & 0xff);
          targetIdx += 1;
        } else if (EncodingErrorType.FAIL.equals(errorHandling)) {
          throw new EncodingException();
        }
      }
    }

    buffer.setCursor(targetIdx);
    return buffer;
  }
コード例 #2
0
  public CodePointBuffer decode(final ByteBuffer text, CodePointBuffer buffer) {
    final int textLength = text.getLength();
    if (buffer == null) {
      buffer = new CodePointBuffer(textLength);
    } else if (buffer.getLength() < textLength) {
      buffer.ensureSize(textLength);
    }

    final int[] targetArray = buffer.getData();
    final byte[] sourceArray = text.getData();

    int targetIdx = buffer.getOffset();
    final int endPos = text.getCursor();
    for (int i = text.getOffset(); i < endPos; i++) {
      targetArray[targetIdx] = (sourceArray[i] & 0xff);
      targetIdx += 1;
    }

    buffer.setCursor(targetIdx);
    return buffer;
  }
コード例 #3
0
  /**
   * Encode, but ignore errors.
   *
   * @param text
   * @param buffer
   * @return
   */
  public ByteBuffer encode(final CodePointBuffer text, ByteBuffer buffer) {
    final int textLength = text.getLength();
    if (buffer == null) {
      buffer = new ByteBuffer(textLength);
    } else if (buffer.getLength() < textLength) {
      buffer.ensureSize(textLength);
    }

    final byte[] targetArray = buffer.getData();
    final int[] sourceArray = text.getData();

    int targetIdx = buffer.getOffset();
    final int endPos = text.getCursor();
    for (int i = text.getOffset(); i < endPos; i++) {
      final int sourceItem = sourceArray[i];
      if (isUnicodeCharacterSupported(sourceItem)) {
        targetArray[targetIdx] = (byte) (sourceItem & 0xff);
        targetIdx += 1;
      }
    }

    buffer.setCursor(targetIdx);
    return buffer;
  }