CharsetEncoder encode - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for CharsetEncoder encode method.

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
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
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
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
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();

6
CodeRank
This code example shows how to use the following methods:encode
                encoderIn.position(position + c);
            } 
            encoderIn.flip();
        } 
        encoderOut.compact();
        lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput);
        encoderOut.flip();
    } 
 
    /** 
     * Read the specified number of bytes into an array. 
     * 
     * @param b   the byte array to read into 
     * @param off the offset to start reading bytes into 
     * @param len the number of bytes to read 
     * @return the number of bytes read or <code>-1</code> 
     *         if the end of the stream has been reached 
     * @throws java.io.IOException if an I/O error occurs 
     */ 
    @Override 
6
CodeRank
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
This code example shows how to use the following methods:canEncode, encode, flush, reset
	// test illegal states for encode facade 
	public void testEncodeFacadeIllegalState() throws CharacterCodingException {
		// encode facade can be execute in anywhere 
		CharBuffer in = CharBuffer.wrap("aaa");
		// Normal case: just created 
		encoder.encode(in);
		in.rewind();
 
		// Normal case: just after encode facade 
		encoder.encode(in);
		in.rewind();
 
		// Normal case: just after canEncode 
		assertSame(encoder, encoder.reset());
		encoder.canEncode("\ud902\udc00");
		encoder.encode(in);
		in.rewind();
		assertSame(encoder, encoder.reset());
		encoder.canEncode('\ud902');
		encoder.encode(in);

5
CodeRank
This code example shows how to use the following methods:encode
    ts.end();
    ts.close();
  } 
   
  private static void setKey(Key k, String s) throws IOException {
    ByteBuffer buffer = encoder.encode(CharBuffer.wrap(s.toCharArray()));
    k.set(buffer.array(), 1.0);
  } 
   
  private static void validateTokens(String[] expected, TokenStream ts) throws IOException {
    int pos = 0;
    while (ts.incrementToken()) {
      assertTrue("Analyzer produced too many tokens", pos <= expected.length);
      CharTermAttribute termAttr = ts.getAttribute(CharTermAttribute.class);
      assertEquals("Unexpected term", expected[pos++], termAttr.toString());
    } 
    assertEquals("Analyzer produced too few terms", expected.length, pos);
  } 
 
  private static Filter getFilter(String[] tokens) throws IOException {
5
CodeRank
This code example shows how to use the following methods:encode, maxBytesPerChar
            charBuff = newChBuf;
        } 
    } 
 
    private void updateWithCharBuf() { 
        final int reqSize = (int) charEncoder.maxBytesPerChar() * charBuff.position();
        if (byteBuff.capacity() < reqSize) {
            byteBuff = java.nio.ByteBuffer.allocate(2 * reqSize);
        } 
 
        // Make ready for read 
        charBuff.flip();
 
        final CoderResult cr = charEncoder.encode(charBuff, byteBuff, true);
        try { 
 
            if (cr.isError()) cr.throwException();
 
            // Make ready for read 
            byteBuff.flip();
5
CodeRank

Related CharsetEncoder encode Questions & Answers:

encode Method Overview
Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer.
See Code Examples for other Java 8 CharsetEncoder Methods: