java.nio.charset.CharsetEncoder源码实例Demo

类java.nio.charset.CharsetEncoder源码实例Demo

下面列出了java.nio.charset.CharsetEncoder 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


@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);
}
 
源代码2 项目: mdict-java   文件: Response.java

/**
 * 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);
    }
}
 
源代码3 项目: Bats   文件: AbstractSqlPatternMatcher.java

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();
}
 
源代码4 项目: spotbugs   文件: ClassHash.java

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.
    }
}
 

/**
 * 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;

}
 
源代码6 项目: neoscada   文件: ProtocolEncoderImpl.java

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

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]);
}
 
源代码8 项目: openjdk-8   文件: StringCoding.java

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);
    }
}
 
源代码9 项目: TencentKona-8   文件: EncoderFactory.java

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;
}
 
源代码10 项目: presto   文件: PrestoConnection.java

/**
 * 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);
}
 
源代码11 项目: spring-analysis-note   文件: DataBuffer.java

/**
 * 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;
}
 
源代码12 项目: vespa   文件: PageTemplatesXmlRenderer.java

@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());
}
 
源代码13 项目: jdk8u_jdk   文件: FontDescriptor.java

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

}
 
源代码14 项目: dragonwell8_jdk   文件: SJIS_OLD.java

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);
    }
 
源代码15 项目: TencentKona-8   文件: ZipCoder.java

private CharsetEncoder encoder() {
    CharsetEncoder enc = encTL.get();
    if (enc == null) {
        enc = cs.newEncoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
        encTL.set(enc);
    }
    return enc;
}
 
源代码16 项目: nifi   文件: TestUtil.java

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;
}
 

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));
       }
}
 
源代码18 项目: JDKSourceCode1.8   文件: StringCoding.java

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);
    }
}
 
源代码19 项目: openjdk-jdk8u   文件: EUC_JP_OLD.java

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);
    }
 
源代码20 项目: CloverETL-Engine   文件: DataFieldImpl.java

@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);
	}
}
 
源代码21 项目: CloverETL-Engine   文件: NumericDataField.java

/**
 * 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);
	}
}
 
源代码22 项目: TencentKona-8   文件: EUC_JP_OLD.java

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);
    }
 
源代码23 项目: CloverETL-Engine   文件: ByteDataField.java

@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);
	}
}
 
源代码24 项目: CloverETL-Engine   文件: DataFieldImpl.java

@Override
public int toByteBuffer(CloverBuffer dataBuffer, CharsetEncoder encoder) throws CharacterCodingException {
	return toByteBuffer(dataBuffer, encoder, Integer.MAX_VALUE);
}
 
源代码25 项目: hottub   文件: EUC_JP_LINUX_OLD.java

public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
源代码26 项目: openjdk-8   文件: SJIS_0213.java

public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
源代码27 项目: dragonwell8_jdk   文件: IBM943C_OLD.java

public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
源代码28 项目: openjdk-jdk9   文件: UTF_32.java

public CharsetEncoder newEncoder() {
    return new UTF_32Coder.Encoder(this, UTF_32Coder.BIG, false);
}
 
源代码29 项目: openjdk-8-source   文件: Big5_HKSCS_2001.java

public CharsetEncoder newEncoder() {
    return new Encoder(this);
}
 
源代码30 项目: hottub   文件: UTF_16LE_BOM.java

public CharsetEncoder newEncoder() {
    return new Encoder(this);
}