Java 8 CharsetEncoder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 CharsetEncoder class.

This code example shows how to use the following methods:maxBytesPerChar, onMalformedInput, onUnmappableCharacter, reset
            final Charset charset = charsets[i];
            byte[] actual = AsciiString.getBytes(new AsciiString(bString), charset);
            assertArrayEquals("failure for " + charset, expected, actual);
        } 
    } 
 
    private static byte[] getBytesWithEncoder(CharSequence value, Charset charset) {
        final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
        final ByteBuffer nativeBuffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * value.length()));
        encoder.encode(CharBuffer.wrap(value), nativeBuffer, true);
        return nativeBuffer.array();
    } 
} 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:onMalformedInput, onUnmappableCharacter, reset
        } 
 
        Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
        CharsetEncoder e = map.get(charset);
        if (e != null) {
            e.reset();
            e.onMalformedInput(CodingErrorAction.REPLACE);
            e.onUnmappableCharacter(CodingErrorAction.REPLACE);
            return e;
        } 
 
        e = charset.newEncoder();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        map.put(charset, e);
        return e;
    } 
 
    /** 
     * Returns a cached thread-local {@link CharsetDecoder} for the specified 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:encode, onMalformedInput, onUnmappableCharacter
 
	public static ByteBuffer encode(String charsetName, CharBuffer charBuffer) {
		try { 
			CharsetEncoder charsetEncoder = getCharsetEncoder(charsetName);
 
			return charsetEncoder.encode(charBuffer);
		} 
		catch (CharacterCodingException cce) {
			throw new Error(cce);
		} 
	} 
 
	public static ByteBuffer encode(String charsetName, String string) {
		return encode(charsetName, CharBuffer.wrap(string));
	} 
 
	public static CharsetEncoder getCharsetEncoder(String charsetName) {
		Charset charset = Charset.forName(charsetName);
 
		CharsetEncoder charsetEncoder = charset.newEncoder();

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:encode, flush, maxBytesPerChar, reset
	 */ 
	static public ByteBuffer encodeUTF8(String src) {
		// char[] ca = src.toCharArray(); 
		int len = src.length();
		int off = 0;
		int en = (int) (len * ce.maxBytesPerChar());
		byte[] ba = new byte[en];
		if (len == 0)
			return null; 
 
		ce.reset();
		ByteBuffer bb = ByteBuffer.wrap(ba);
		CharBuffer cb = CharBuffer.wrap(src, off, len);
		try { 
			CoderResult cr = ce.encode(cb, bb, true);
			if (!cr.isUnderflow())
				cr.throwException();
			cr = ce.flush(bb);
			if (!cr.isUnderflow())
				cr.throwException();

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:maxBytesPerChar, onMalformedInput, onUnmappableCharacter
		_charsetName = charsetName;
 
		if (inputBufferSize <= 0) {
			throw new IllegalArgumentException(
				"Input buffer size " + inputBufferSize +
					" must be a positive number"); 
		} 
 
		_inputBuffer = CharBuffer.allocate(inputBufferSize);
 
		_charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(charsetName);
 
		_maxBytesPerChar = (int)Math.ceil(_charsetEncoder.maxBytesPerChar());
 
		if (outputBufferSize < _maxBytesPerChar) {
			throw new IllegalArgumentException(
				"Output buffer size " + outputBufferSize + " is less than " +
					_maxBytesPerChar);
		} 
 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:encode, flush, maxBytesPerChar, reset
	public byte[] encode(char[] chars, int off, int len) {
		if (len == 0) {
			return new byte[0]; 
		} 
 
		encoder.reset();
 
		int bytesLength = scale(len, encoder.maxBytesPerChar());
 
		byte[] bytes = ThreadLocalCache.getBytes(bytesLength);
 
		return encode(chars, off, len, bytes);
	} 
 
	public CharsetEncoder getEncoder() {
		return encoder;
	} 
 
	public byte[] encode(char[] chars, int off, int len, byte[] bytes) {
		ByteBuffer byteBuf = ByteBuffer.wrap(bytes);

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:malformedInputAction, replacement, unmappableCharacterAction
    } 
    private static native int setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, char[] subChars);
 
    public static int setCallbackEncode(long converterHandle, CharsetEncoder encoder) {
        return setCallbackEncode(converterHandle,
                translateCodingErrorAction(encoder.malformedInputAction()),
                translateCodingErrorAction(encoder.unmappableCharacterAction()),
                encoder.replacement());
    } 
    private static native int setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes);
} 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:canEncode, onMalformedInput, onUnmappableCharacter
     * @see 
     * com.alimama.mdrill.utils.zip.ZipEncoding#canEncode(java.lang.String) 
     */ 
    public boolean canEncode(String name) {
        CharsetEncoder enc = this.charset.newEncoder();
        enc.onMalformedInput(CodingErrorAction.REPORT);
        enc.onUnmappableCharacter(CodingErrorAction.REPORT);
 
        return enc.canEncode(name);
    } 
 
    /** 
     * @see 
     * com.alimama.mdrill.utils.zip.ZipEncoding#encode(java.lang.String) 
     */ 
    public ByteBuffer encode(String name) {
        CharsetEncoder enc = this.charset.newEncoder();
 
        enc.onMalformedInput(CodingErrorAction.REPORT);
        enc.onUnmappableCharacter(CodingErrorAction.REPORT);
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:encode, flush, reset
    public void testInternalState_Encode() throws CharacterCodingException {
        CharsetEncoder newEncoder = cs.newEncoder();
        //Init - > encode 
        { 
            CharBuffer in = CharBuffer.wrap("A");
            newEncoder.encode(in);
        } 
 
        //Reset - > encode 
        { 
            newEncoder.reset();
            CharBuffer in = CharBuffer.wrap("A");
            newEncoder.encode(in);
        } 
 
        //Encoding -> encode 
        { 
            newEncoder.reset();
            CharBuffer in = CharBuffer.wrap("A");
            ByteBuffer out = ByteBuffer.allocate(0x10);

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:encode, onMalformedInput, onUnmappableCharacter
 
	public static ByteBuffer encode(String charsetName, CharBuffer charBuffer) {
		try { 
			CharsetEncoder charsetEncoder = getCharsetEncoder(charsetName);
 
			return charsetEncoder.encode(charBuffer);
		} 
		catch (CharacterCodingException cce) {
			throw new Error(cce);
		} 
	} 
 
	public static ByteBuffer encode(String charsetName, String string) {
		return encode(charsetName, CharBuffer.wrap(string));
	} 
 
	public static CharsetEncoder getCharsetEncoder(String charsetName) {
		Charset charset = Charset.forName(charsetName);
 
		CharsetEncoder charsetEncoder = charset.newEncoder();

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 CharsetEncoder Questions & Answers:

See Code Examples for Java 8 CharsetEncoder Methods: