java.nio.charset.CharsetEncoder Java Examples

The following examples show how to use java.nio.charset.CharsetEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TracingManagedHttpClientConnectionFactory.java    From caravan with Apache License 2.0 6 votes vote down vote up
@Override
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
    final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
    CharsetDecoder chardecoder = null;
    CharsetEncoder charencoder = null;
    final Charset charset = cconfig.getCharset();
    final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ? cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
    final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ? cconfig.getUnmappableInputAction()
            : CodingErrorAction.REPORT;
    if (charset != null) {
        chardecoder = charset.newDecoder();
        chardecoder.onMalformedInput(malformedInputAction);
        chardecoder.onUnmappableCharacter(unmappableInputAction);
        charencoder = charset.newEncoder();
        charencoder.onMalformedInput(malformedInputAction);
        charencoder.onUnmappableCharacter(unmappableInputAction);
    }
    final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
    return new TracingManagedHttpClientConnection(id, cconfig.getBufferSize(), cconfig.getFragmentSizeHint(), chardecoder, charencoder,
            cconfig.getMessageConstraints(), incomingContentStrategy, outgoingContentStrategy, requestWriterFactory, responseParserFactory, logFunc);
}
 
Example #2
Source File: Response.java    From mdict-java with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a text response with known length.
 */
public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) {
    ContentType contentType = new ContentType(mimeType);
    if (txt == null) {
        return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0);
    } else {
        byte[] bytes;
        try {
            CharsetEncoder newEncoder = Charset.forName(contentType.getEncoding()).newEncoder();
            if (!newEncoder.canEncode(txt)) {
                contentType = contentType.tryUTF8();
            }
            bytes = txt.getBytes(contentType.getEncoding());
        } catch (UnsupportedEncodingException e) {
            NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e);
            bytes = new byte[0];
        }
        return newFixedLengthResponse(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length);
    }
}
 
Example #3
Source File: AbstractSqlPatternMatcher.java    From Bats with Apache License 2.0 6 votes vote down vote up
public AbstractSqlPatternMatcher(String patternString) {
  this.patternString = patternString;

  final CharsetEncoder charsetEncoder = Charsets.UTF_8.newEncoder();
  final CharBuffer patternCharBuffer = CharBuffer.wrap(patternString);

  try {
    patternByteBuffer = charsetEncoder.encode(patternCharBuffer);
  } catch (CharacterCodingException e) {
    throw UserException.validationError(e)
        .message("Failure to encode pattern %s using UTF-8", patternString)
        .addContext("Message: ", e.getMessage())
        .build(logger);
  }
  patternLength = patternByteBuffer.limit();
}
 
Example #4
Source File: OldCharsetEncoderDecoderBufferTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testEncoderOutputBuffer() {
    CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();

    byte[] buffer = new byte[10];
    ByteBuffer out = ByteBuffer.wrap(buffer);

    assertTrue(out.hasArray());
    encoder.encode(CharBuffer.wrap("ab"), out, false);

    assertEquals('a', buffer[0]);
    assertEquals('b', buffer[1]);
    assertEquals(0, buffer[2]);

    out = ByteBuffer.allocateDirect(10);
    // It's no longer possible to get a byte buffer without a backing byte[] on Android.
    // This test is useless on Android, unless that changes again. (You can't even
    // subclass ByteBuffer because -- although it's non-final -- both the RI and Android
    // have [different] package-private abstract methods you'd need to implement but can't.)
    //assertFalse(out.hasArray());
    encoder.encode(CharBuffer.wrap("x"), out, true);

    // check whether the second decode corrupted the first buffer
    assertEquals('a', buffer[0]);
    assertEquals('b', buffer[1]);
    assertEquals(0, buffer[2]);
}
 
Example #5
Source File: ProtocolEncoderImpl.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private void encodeBrowseUpdate ( final IoSession session, final Object message, final IoBuffer data ) throws ProtocolCodecException
{
    // length
    data.putUnsignedShort ( ( (BrowseAdded)message ).getEntries ().size () );

    final CharsetEncoder encoder = Sessions.getCharsetEncoder ( session );

    // data
    for ( final BrowseAdded.Entry entry : ( (BrowseAdded)message ).getEntries () )
    {
        data.putUnsignedShort ( entry.getRegister () );
        data.put ( entry.getDataType ().getDataType () );
        data.putEnumSet ( entry.getFlags () );

        try
        {
            data.putPrefixedString ( entry.getName (), encoder );
            data.putPrefixedString ( entry.getDescription (), encoder );
            data.putPrefixedString ( entry.getUnit (), encoder );
        }
        catch ( final CharacterCodingException e )
        {
            throw new ProtocolCodecException ( e );
        }
    }
}
 
Example #6
Source File: ClassHash.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void work(MessageDigest digest, String s, CharsetEncoder encoder) {
    try {
        CharBuffer cbuf = CharBuffer.allocate(s.length());
        cbuf.put(s);
        cbuf.flip();

        ByteBuffer buf = encoder.encode(cbuf);
        // System.out.println("pos="+buf.position() +",limit=" +
        // buf.limit());
        int nbytes = buf.limit();
        byte[] encodedBytes = new byte[nbytes];
        buf.get(encodedBytes);

        digest.update(encodedBytes);
    } catch (CharacterCodingException e) {
        // This should never happen, since we're encoding to UTF-8.
    }
}
 
Example #7
Source File: Utf7ImeHelper.java    From uiautomator-unicode-input-helper with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new byte array containing the characters of the specified
 * string encoded using the given charset.
 * 
 * It is equivalent to <code>input.getBytes(charset)</code> except it has
 * workaround for the bug ID 61917.
 * 
 * @see https://code.google.com/p/android/issues/detail?id=61917
 */
//@formatter:off
/*
 * The original code is available from
 *     https://android.googlesource.com/platform/libcore/+/android-4.4_r1.2/libdvm/src/main/java/java/lang/String.java
 */
//@formatter:on
public static byte[] getBytes(String input, Charset charset) {
    CharBuffer chars = CharBuffer.wrap(input.toCharArray());
    // @formatter:off
    CharsetEncoder encoder = charset.newEncoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    // @formatter:on
    ByteBuffer buffer;
    buffer = encode(chars.asReadOnlyBuffer(), encoder);
    byte[] bytes = new byte[buffer.limit()];
    buffer.get(bytes);
    return bytes;

}
 
Example #8
Source File: SJIS_OLD.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public CharsetEncoder newEncoder() {

        // Need to force the replacement byte to 0x3f
        // because JIS_X_0208_Encoder defines its own
        // alternative 2 byte substitution to permit it
        // to exist as a self-standing Encoder

        byte[] replacementBytes = { (byte)0x3f };
        return new Encoder(this).replaceWith(replacementBytes);
    }
 
Example #9
Source File: PageTemplatesXmlRenderer.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Override
public void beginResponse(OutputStream stream) {
    Charset cs = Charset.forName(getRequestedEncoding(getResult().getQuery()));
    CharsetEncoder encoder = cs.newEncoder();
    writer = wrapWriter(new ByteWriter(stream, encoder));

    header(writer, getResult());
    if (getResult().hits().getError() != null || getResult().hits().getQuery().errors().size() > 0)
        error(writer, getResult());

    if (getResult().getContext(false) != null)
        queryContext(writer, getResult().getQuery());
}
 
Example #10
Source File: EncoderFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static CharsetEncoder createEncoder( String encodin ) {
    Charset cs = Charset.forName(System.getProperty("file.encoding"));
    CharsetEncoder encoder = cs.newEncoder();

    if( cs.getClass().getName().equals("sun.nio.cs.MS1252") ) {
        try {
            // at least JDK1.4.2_01 has a bug in MS1252 encoder.
            // specifically, it returns true for any character.
            // return a correct encoder to workaround this problem

            // statically binding to MS1252Encoder will cause a Link error
            // (at least in IBM JDK1.4.1)
            @SuppressWarnings("unchecked")
            Class<? extends CharsetEncoder> ms1252encoder = (Class<? extends CharsetEncoder>) Class.forName("com.sun.codemodel.internal.util.MS1252Encoder");
            Constructor<? extends CharsetEncoder> c = ms1252encoder.getConstructor(new Class[]{
                Charset.class
            });
            return c.newInstance(new Object[]{cs});
        } catch( Throwable t ) {
            // if something funny happens, ignore it and fall back to
            // a broken MS1252 encoder. It's probably still better
            // than choking here.
            return encoder;
        }
    }

    return encoder;
}
 
Example #11
Source File: PrestoConnection.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a session property.
 */
public void setSessionProperty(String name, String value)
{
    requireNonNull(name, "name is null");
    requireNonNull(value, "value is null");
    checkArgument(!name.isEmpty(), "name is empty");

    CharsetEncoder charsetEncoder = US_ASCII.newEncoder();
    checkArgument(name.indexOf('=') < 0, "Session property name must not contain '=': %s", name);
    checkArgument(charsetEncoder.canEncode(name), "Session property name is not US_ASCII: %s", name);
    checkArgument(charsetEncoder.canEncode(value), "Session property value is not US_ASCII: %s", value);

    sessionProperties.put(name, value);
}
 
Example #12
Source File: StringCoding.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #13
Source File: FontDescriptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public FontDescriptor(String nativeName, CharsetEncoder encoder,
                      int[] exclusionRanges){

    this.nativeName = nativeName;
    this.encoder = encoder;
    this.exclusionRanges = exclusionRanges;
    this.useUnicode = false;
    Charset cs = encoder.charset();
    if (cs instanceof HistoricallyNamedCharset)
        this.charsetName = ((HistoricallyNamedCharset)cs).historicalName();
    else
        this.charsetName = cs.name();

}
 
Example #14
Source File: DataBuffer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Write the given {@code CharSequence} using the given {@code Charset},
 * starting at the current writing position.
 * @param charSequence the char sequence to write into this buffer
 * @param charset the charset to encode the char sequence with
 * @return this buffer
 * @since 5.1.4
 */
default DataBuffer write(CharSequence charSequence, Charset charset) {
	Assert.notNull(charSequence, "CharSequence must not be null");
	Assert.notNull(charset, "Charset must not be null");
	if (charSequence.length() != 0) {
		CharsetEncoder charsetEncoder = charset.newEncoder()
				.onMalformedInput(CodingErrorAction.REPLACE)
				.onUnmappableCharacter(CodingErrorAction.REPLACE);
		CharBuffer inBuffer = CharBuffer.wrap(charSequence);
		int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar());
		ByteBuffer outBuffer = ensureCapacity(estimatedSize)
				.asByteBuffer(writePosition(), writableByteCount());
		while (true) {
			CoderResult cr = (inBuffer.hasRemaining() ?
					charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW);
			if (cr.isUnderflow()) {
				cr = charsetEncoder.flush(outBuffer);
			}
			if (cr.isUnderflow()) {
				break;
			}
			if (cr.isOverflow()) {
				writePosition(writePosition() + outBuffer.position());
				int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
				ensureCapacity(maximumSize);
				outBuffer = asByteBuffer(writePosition(), writableByteCount());
			}
		}
		writePosition(writePosition() + outBuffer.position());
	}
	return this;
}
 
Example #15
Source File: CharacterEncodingValidatorImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
private void validateDynamicTagsMod(Mailing mailing, Set<EncodingError> unencodeableDynamicTags, CharsetEncoder charsetEncoder) {
	// No mailing? Nothing to validate!
	if (mailing == null) {
		return;
	}

	Collection<DynamicTag> dynTags = mailing.getDynTags().values();

	for (DynamicTag dynTag : dynTags) {
		unencodeableDynamicTags.addAll(validateContent(dynTag, charsetEncoder));
       }
}
 
Example #16
Source File: ZipCoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private CharsetEncoder encoder() {
    CharsetEncoder enc = encTL.get();
    if (enc == null) {
        enc = cs.newEncoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
        encTL.set(enc);
    }
    return enc;
}
 
Example #17
Source File: TestUtil.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static byte[] bytesFor(String content, Charset charset) throws CharacterCodingException {
    CharBuffer chars = CharBuffer.wrap(content);
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer buffer = encoder.encode(chars);
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    return bytes;
}
 
Example #18
Source File: StringCoding.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #19
Source File: EUC_JP_OLD.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public CharsetEncoder newEncoder() {

        // Need to force the replacement byte to 0x3f
        // because JIS_X_0208_Encoder defines its own
        // alternative 2 byte substitution to permit it
        // to exist as a self-standing Encoder

        byte[] replacementBytes = { (byte)0x3f };
        return new Encoder(this).replaceWith(replacementBytes);
    }
 
Example #20
Source File: DataFieldImpl.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
	try {
		String s = toString();
		if (s.length() > maxLength) {
			s = s.substring(0, maxLength);
		}
		dataBuffer.put(encoder.encode(CharBuffer.wrap(s)));
		return s.length();
	} catch (BufferOverflowException e) {
		throw new RuntimeException("The size of data buffer is only " + dataBuffer.limit() + ". Set appropriate parameter in defaultProperties file.", e);
	}
}
 
Example #21
Source File: NumericDataField.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * If the binary format is set, stores the data accordingly.
 * 
 * Call super otherwise.
 */
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
	if (binaryFormat != null) {
		switch (binaryFormat) {
		case DOUBLE_BIG_ENDIAN: 
		case DOUBLE_LITTLE_ENDIAN: 
		case FLOAT_BIG_ENDIAN: 
		case FLOAT_LITTLE_ENDIAN:
			ByteOrder originalByteOrder = dataBuffer.order();
			dataBuffer.order(binaryFormat.byteOrder); // set the field's byte order
			try {
				if (binaryFormat.size == 4) {
					dataBuffer.putFloat((float) this.value);
				} else if(binaryFormat.size == 8) {
					dataBuffer.putDouble(this.value);
				}
			} catch (BufferOverflowException boe) {
				throw new BadDataFormatException("Failed to store the data, the buffer is full", boe);
			} finally {
				dataBuffer.order(originalByteOrder); // restore the original byte order
			}
			break;
		default:
			throw new JetelRuntimeException("Invalid binary format: " + binaryFormat);
		}
		return 0;
	} else {
		return super.toByteBuffer(dataBuffer, encoder, maxLength);
	}
}
 
Example #22
Source File: EUC_JP_OLD.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public CharsetEncoder newEncoder() {

        // Need to force the replacement byte to 0x3f
        // because JIS_X_0208_Encoder defines its own
        // alternative 2 byte substitution to permit it
        // to exist as a self-standing Encoder

        byte[] replacementBytes = { (byte)0x3f };
        return new Encoder(this).replaceWith(replacementBytes);
    }
 
Example #23
Source File: ByteDataField.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
	try {
		if (!isNull()) {
			dataBuffer.put(getByteArray());
		} else {
			dataBuffer.put(encoder.encode(CharBuffer.wrap(metadata.getNullValue())));
		}
		return 0;
	} catch (BufferOverflowException e) {
		throw new RuntimeException("The size of data buffer is only " + dataBuffer.limit() + ". Set appropriate parameter in defaultProperties file.", e);
	}
}
 
Example #24
Source File: ISO2022_CN_GB.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
Example #25
Source File: SJIS_0213.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
Example #26
Source File: IBM943C_OLD.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
Example #27
Source File: Big5_HKSCS_2001.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
Example #28
Source File: EUC_TW.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
Example #29
Source File: TextTableFormatter.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void writeContainerDataField(DataField field, CloverBuffer buffer, CharsetEncoder encoder) throws CharacterCodingException {
	String s = field.toString();
	buffer.put(encoder.encode(CharBuffer.wrap(s)));
}
 
Example #30
Source File: ListDataField.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder, int maxLength) throws CharacterCodingException {
	throw new UnsupportedOperationException(getMetadata().toString() + " cannot be serialized to bytes. List and map container types are not supported.");
}