public static CharsetEncoderICU newInstance(Charset cs, String icuCanonicalName) { // This complexity is necessary to ensure that even if the constructor, superclass // constructor, or call to updateCallback throw, we still free the native peer. long address = 0; try { address = NativeConverter.openConverter(icuCanonicalName); float averageBytesPerChar = NativeConverter.getAveBytesPerChar(address); float maxBytesPerChar = NativeConverter.getMaxBytesPerChar(address); byte[] replacement = makeReplacement(icuCanonicalName, address); CharsetEncoderICU result = new CharsetEncoderICU(cs, averageBytesPerChar, maxBytesPerChar, replacement, address); address = 0; // CharsetEncoderICU has taken ownership; its finalizer will do the free. return result; } finally { if (address != 0) { NativeConverter.closeConverter(address); } } }
@Override protected CoderResult implFlush(ByteBuffer out) { try { // ICU needs to see an empty input. input = EmptyArray.CHAR; inEnd = 0; data[INPUT_OFFSET] = 0; data[OUTPUT_OFFSET] = getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, true); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_TRUNCATED_CHAR_FOUND) { if (data[INPUT_OFFSET] > 0) { return CoderResult.malformedForLength(data[INPUT_OFFSET]); } } } return CoderResult.UNDERFLOW; } finally { setPosition(out); implReset(); } }
public CharsetEncoder newEncoder() { return CharsetEncoderICU.newInstance(this, icuCanonicalName); }
@Override protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { if (!in.hasRemaining()) { return CoderResult.UNDERFLOW; } data[INPUT_OFFSET] = getArray(in); data[OUTPUT_OFFSET]= getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. try { int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, false); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_INVALID_CHAR_FOUND) { return CoderResult.unmappableForLength(data[INVALID_CHARS]); } else if (error == ICU.U_ILLEGAL_CHAR_FOUND) { return CoderResult.malformedForLength(data[INVALID_CHARS]); } else { throw new AssertionError(error); } } // Decoding succeeded: give us more data. return CoderResult.UNDERFLOW; } finally { setPosition(in); setPosition(out); } }
private CharsetEncoderICU(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement, long address) { super(cs, averageBytesPerChar, maxBytesPerChar, replacement, true); // Our native peer needs to know what just happened... this.converterHandle = address; updateCallback(); }
@Override protected void implReplaceWith(byte[] newReplacement) { updateCallback(); }
@Override protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { if (!in.hasRemaining()) { return CoderResult.UNDERFLOW; } data[INPUT_OFFSET] = getArray(in); data[OUTPUT_OFFSET]= getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. try { int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, false); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_INVALID_CHAR_FOUND) { return CoderResult.unmappableForLength(data[INVALID_CHARS]); } else if (error == ICU.U_ILLEGAL_CHAR_FOUND) { return CoderResult.malformedForLength(data[INVALID_CHARS]); } else { throw new AssertionError(error); } } // Decoding succeeded: give us more data. return CoderResult.UNDERFLOW; } finally { setPosition(in); setPosition(out); } }
@Override protected CoderResult implFlush(ByteBuffer out) { try { // ICU needs to see an empty input. input = EmptyArray.CHAR; inEnd = 0; data[INPUT_OFFSET] = 0; data[OUTPUT_OFFSET] = getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, true); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_TRUNCATED_CHAR_FOUND) { if (data[INPUT_OFFSET] > 0) { return CoderResult.malformedForLength(data[INPUT_OFFSET]); } } } return CoderResult.UNDERFLOW; } finally { setPosition(out); implReset(); } }
@Override protected void implOnMalformedInput(CodingErrorAction newAction) { updateCallback(); }
public static CharsetEncoderICU newInstance(Charset cs, String icuCanonicalName) { // This complexity is necessary to ensure that even if the constructor, superclass // constructor, or call to updateCallback throw, we still free the native peer. long address = 0; try { address = NativeConverter.openConverter(icuCanonicalName); float averageBytesPerChar = NativeConverter.getAveBytesPerChar(address); float maxBytesPerChar = NativeConverter.getMaxBytesPerChar(address); byte[] replacement = makeReplacement(icuCanonicalName, address); CharsetEncoderICU result = new CharsetEncoderICU(cs, averageBytesPerChar, maxBytesPerChar, replacement, address); address = 0; // CharsetEncoderICU has taken ownership; its finalizer will do the free. return result; } finally { if (address != 0) { NativeConverter.closeConverter(address); } } }
@Override protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { if (!in.hasRemaining()) { return CoderResult.UNDERFLOW; } data[INPUT_OFFSET] = getArray(in); data[OUTPUT_OFFSET]= getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. try { int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, false); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_INVALID_CHAR_FOUND) { return CoderResult.unmappableForLength(data[INVALID_CHARS]); } else if (error == ICU.U_ILLEGAL_CHAR_FOUND) { return CoderResult.malformedForLength(data[INVALID_CHARS]); } else { throw new AssertionError(error); } } // Decoding succeeded: give us more data. return CoderResult.UNDERFLOW; } finally { setPosition(in); setPosition(out); } }
public CharsetEncoder newEncoder() { return CharsetEncoderICU.newInstance(this, icuCanonicalName); }
@Override protected CoderResult implFlush(ByteBuffer out) { try { // ICU needs to see an empty input. input = EmptyArray.CHAR; inEnd = 0; data[INPUT_OFFSET] = 0; data[OUTPUT_OFFSET] = getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, true); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_TRUNCATED_CHAR_FOUND) { if (data[INPUT_OFFSET] > 0) { return CoderResult.malformedForLength(data[INPUT_OFFSET]); } } } return CoderResult.UNDERFLOW; } finally { setPosition(out); implReset(); } }
@Override protected void implOnUnmappableCharacter(CodingErrorAction newAction) { updateCallback(); }
public static CharsetEncoderICU newInstance(Charset cs, String icuCanonicalName) { // This complexity is necessary to ensure that even if the constructor, superclass // constructor, or call to updateCallback throw, we still free the native peer. long address = 0; try { address = NativeConverter.openConverter(icuCanonicalName); float averageBytesPerChar = NativeConverter.getAveBytesPerChar(address); float maxBytesPerChar = NativeConverter.getMaxBytesPerChar(address); byte[] replacement = makeReplacement(icuCanonicalName, address); CharsetEncoderICU result = new CharsetEncoderICU(cs, averageBytesPerChar, maxBytesPerChar, replacement, address); address = 0; // CharsetEncoderICU has taken ownership; its finalizer will do the free. return result; } finally { if (address != 0) { NativeConverter.closeConverter(address); } } }
@Override protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { if (!in.hasRemaining()) { return CoderResult.UNDERFLOW; } data[INPUT_OFFSET] = getArray(in); data[OUTPUT_OFFSET]= getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. try { int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, false); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_INVALID_CHAR_FOUND) { return CoderResult.unmappableForLength(data[INVALID_CHARS]); } else if (error == ICU.U_ILLEGAL_CHAR_FOUND) { return CoderResult.malformedForLength(data[INVALID_CHARS]); } else { throw new AssertionError(error); } } // Decoding succeeded: give us more data. return CoderResult.UNDERFLOW; } finally { setPosition(in); setPosition(out); } }
public CharsetEncoder newEncoder() { return CharsetEncoderICU.newInstance(this, icuCanonicalName); }
@Override protected CoderResult implFlush(ByteBuffer out) { try { // ICU needs to see an empty input. input = EmptyArray.CHAR; inEnd = 0; data[INPUT_OFFSET] = 0; data[OUTPUT_OFFSET] = getArray(out); data[INVALID_CHARS] = 0; // Make sure we don't see earlier errors. int error = NativeConverter.encode(converterHandle, input, inEnd, output, outEnd, data, true); if (ICU.U_FAILURE(error)) { if (error == ICU.U_BUFFER_OVERFLOW_ERROR) { return CoderResult.OVERFLOW; } else if (error == ICU.U_TRUNCATED_CHAR_FOUND) { if (data[INPUT_OFFSET] > 0) { return CoderResult.malformedForLength(data[INPUT_OFFSET]); } } } return CoderResult.UNDERFLOW; } finally { setPosition(out); implReset(); } }
private CharsetEncoderICU(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement, long address) { super(cs, averageBytesPerChar, maxBytesPerChar, replacement, true); // Our native peer needs to know what just happened... this.converterHandle = address; updateCallback(); }
public static CharsetEncoderICU newInstance(Charset cs, String icuCanonicalName) { // This complexity is necessary to ensure that even if the constructor, superclass // constructor, or call to updateCallback throw, we still free the native peer. long address = 0; try { address = NativeConverter.openConverter(icuCanonicalName); float averageBytesPerChar = NativeConverter.getAveBytesPerChar(address); float maxBytesPerChar = NativeConverter.getMaxBytesPerChar(address); byte[] replacement = makeReplacement(icuCanonicalName, address); CharsetEncoderICU result = new CharsetEncoderICU(cs, averageBytesPerChar, maxBytesPerChar, replacement, address); address = 0; // CharsetEncoderICU has taken ownership; its finalizer will do the free. return result; } finally { if (address != 0) { NativeConverter.closeConverter(address); } } }