Java Code Examples for java.nio.charset.CharsetEncoder#averageBytesPerChar()

The following examples show how to use java.nio.charset.CharsetEncoder#averageBytesPerChar() . 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: JDBCClobFile.java    From evosql with Apache License 2.0 6 votes vote down vote up
protected final void setEncoding(final String encoding)
throws UnsupportedEncodingException {

    final Charset charSet = charsetForName(encoding);
    final CharsetEncoder encoder = charSet.newEncoder().onMalformedInput(
        CodingErrorAction.REPLACE).onUnmappableCharacter(
        CodingErrorAction.REPLACE);
    final float maxBytesPerChar     = encoder.maxBytesPerChar();
    final float averageBytesPerChar = encoder.averageBytesPerChar();
    final boolean fixedWidthCharset =
        (maxBytesPerChar == Math.round(maxBytesPerChar))
        && (maxBytesPerChar == averageBytesPerChar);

    //
    m_fixedWidthCharset = fixedWidthCharset;
    m_maxCharWidth      = Math.round(maxBytesPerChar);
    m_charset           = charSet;
    m_encoder           = encoder;
    m_encoding          = m_charset.name();
}
 
Example 2
Source File: Utf7ImeHelper.java    From uiautomator-unicode-input-helper with Apache License 2.0 6 votes vote down vote up
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}
 
Example 3
Source File: Utf7ImeHelper.java    From uiautomator-unicode-input-helper with Apache License 2.0 6 votes vote down vote up
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}
 
Example 4
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 5
Source File: DataBuffer.java    From java-technology-stack 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(outBuffer.position());
				int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
				ensureCapacity(maximumSize);
				outBuffer = asByteBuffer(writePosition(), writableByteCount());
			}
		}
		writePosition(outBuffer.position());
	}
	return this;
}
 
Example 6
Source File: FileEncodingQuery.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private ProxyEncoder (final CharsetEncoder defaultEncoder) {
    super (ProxyCharset.this, defaultEncoder.averageBytesPerChar(), defaultEncoder.maxBytesPerChar(), defaultEncoder.replacement());
    this.currentEncoder = defaultEncoder;
    this.initialized = true;
}
 
Example 7
Source File: PerforceShiftJISCharset.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Call the superclass constructor with the Charset object and the
 * encodings sizes from the encoder.
 */
Encoder(Charset cs, CharsetEncoder encoder) {
	super(cs, encoder.averageBytesPerChar(), encoder.maxBytesPerChar());
	this.encoder = encoder;
}
 
Example 8
Source File: PerforceShiftJISCharset.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Call the superclass constructor with the Charset object and the
 * encodings sizes from the encoder.
 */
Encoder(Charset cs, CharsetEncoder encoder) {
	super(cs, encoder.averageBytesPerChar(), encoder.maxBytesPerChar());
	this.encoder = encoder;
}
 
Example 9
Source File: PerforceShiftJISCharset.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Call the superclass constructor with the Charset object and the
 * encodings sizes from the encoder.
 */
Encoder(Charset cs, CharsetEncoder encoder) {
	super(cs, encoder.averageBytesPerChar(), encoder.maxBytesPerChar());
	this.encoder = encoder;
}
 
Example 10
Source File: PerforceShiftJISCharset.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Call the superclass constructor with the Charset object and the
 * encodings sizes from the encoder.
 */
Encoder(Charset cs, CharsetEncoder encoder) {
	super(cs, encoder.averageBytesPerChar(), encoder.maxBytesPerChar());
	this.encoder = encoder;
}
 
Example 11
Source File: PerforceShiftJISCharset.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Call the superclass constructor with the Charset object and the
 * encodings sizes from the encoder.
 */
Encoder(Charset cs, CharsetEncoder encoder) {
	super(cs, encoder.averageBytesPerChar(), encoder.maxBytesPerChar());
	this.encoder = encoder;
}
 
Example 12
Source File: PerforceShiftJISCharset.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Call the superclass constructor with the Charset object and the
 * encodings sizes from the encoder.
 */
Encoder(Charset cs, CharsetEncoder encoder) {
	super(cs, encoder.averageBytesPerChar(), encoder.maxBytesPerChar());
	this.encoder = encoder;
}