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();
    } 
} 
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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 
Full Snippet Info
7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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
Stop searching for code, let great code find you!  Add Codota to your java IDE
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
Stop searching for code, let great code find you!  Add Codota to your java IDE
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);
		} 
 
Full Snippet Info
7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE

Related Java 8 CharsetEncoder Questions & Answers:

See Code Examples for Java 8 CharsetEncoder Methods: