当前位置: 首页>>代码示例>>Java>>正文


Java CharsetDecoder.averageCharsPerByte方法代码示例

本文整理汇总了Java中java.nio.charset.CharsetDecoder.averageCharsPerByte方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetDecoder.averageCharsPerByte方法的具体用法?Java CharsetDecoder.averageCharsPerByte怎么用?Java CharsetDecoder.averageCharsPerByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.charset.CharsetDecoder的用法示例。


在下文中一共展示了CharsetDecoder.averageCharsPerByte方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ProxyDecoder

import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
private ProxyDecoder (final CharsetDecoder defaultDecoder) {
    super (ProxyCharset.this, defaultDecoder.averageCharsPerByte(), defaultDecoder.maxCharsPerByte());
    this.currentDecoder = defaultDecoder;
    initialized = true;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:6,代码来源:FileEncodingQuery.java

示例2: getPrefixedString

import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
/**
 * Reads a string which has a length field before the actual
 * encoded string, using the specified <code>decoder</code> and returns it.
 *
 * @param prefixLength the length of the length field (1, 2, or 4)
 * @param decoder the decoder to use for decoding the string
 * @return the prefixed string
 * @throws CharacterCodingException when decoding fails
 * @throws BufferUnderflowException when there is not enough data available
 */
@Override
public String getPrefixedString(int prefixLength, CharsetDecoder decoder) throws CharacterCodingException {
    if (!prefixedDataAvailable(prefixLength)) {
        throw new BufferUnderflowException();
    }

    int fieldSize = 0;

    switch (prefixLength) {
    case 1:
        fieldSize = getUnsigned();
        break;
    case 2:
        fieldSize = getUnsignedShort();
        break;
    case 4:
        fieldSize = getInt();
        break;
    }

    if (fieldSize == 0) {
        return "";
    }

    boolean utf16 = decoder.charset().name().startsWith("UTF-16");

    if (utf16 && (fieldSize & 1) != 0) {
        throw new BufferDataException("fieldSize is not even for a UTF-16 string.");
    }

    int oldLimit = limit();
    int end = position() + fieldSize;

    if (oldLimit < end) {
        throw new BufferUnderflowException();
    }

    limit(end);
    decoder.reset();

    int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
    CharBuffer out = CharBuffer.allocate(expectedLength);
    for (;;) {
        CoderResult cr;
        if (hasRemaining()) {
            cr = decoder.decode(buf(), out, true);
        } else {
            cr = decoder.flush(out);
        }

        if (cr.isUnderflow()) {
            break;
        }

        if (cr.isOverflow()) {
            CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
            out.flip();
            o.put(out);
            out = o;
            continue;
        }

        cr.throwException();
    }

    limit(oldLimit);
    position(end);
    return out.flip().toString();
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:80,代码来源:AbstractIoBuffer.java

示例3: decode

import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
@SuppressWarnings("cast")
public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
    String encName = getEncodingName();
    CharsetDecoder decoder;
    try {
        decoder = getDecoder(encName, ignoreEncodingErrors);
    } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
        log.error(Errors.UnsupportedEncoding(encName));
        return (CharBuffer)CharBuffer.allocate(1).flip();
    }

    // slightly overestimate the buffer size to avoid reallocation.
    float factor =
        decoder.averageCharsPerByte() * 0.8f +
        decoder.maxCharsPerByte() * 0.2f;
    CharBuffer dest = CharBuffer.
        allocate(10 + (int)(inbuf.remaining()*factor));

    while (true) {
        CoderResult result = decoder.decode(inbuf, dest, true);
        dest.flip();

        if (result.isUnderflow()) { // done reading
            // make sure there is at least one extra character
            if (dest.limit() == dest.capacity()) {
                dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
                dest.flip();
            }
            return dest;
        } else if (result.isOverflow()) { // buffer too small; expand
            int newCapacity =
                10 + dest.capacity() +
                (int)(inbuf.remaining()*decoder.maxCharsPerByte());
            dest = CharBuffer.allocate(newCapacity).put(dest);
        } else if (result.isMalformed() || result.isUnmappable()) {
            // bad character in input
            StringBuilder unmappable = new StringBuilder();
            int len = result.length();

            for (int i = 0; i < len; i++) {
                unmappable.append(String.format("%02X", inbuf.get()));
            }

            String charsetName = charset == null ? encName : charset.name();

            log.error(dest.limit(),
                      Errors.IllegalCharForEncoding(unmappable.toString(), charsetName));

            // undo the flip() to prepare the output buffer
            // for more translation
            dest.position(dest.limit());
            dest.limit(dest.capacity());
            dest.put((char)0xfffd); // backward compatible
        } else {
            throw new AssertionError(result);
        }
    }
    // unreached
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:60,代码来源:BaseFileManager.java

示例4: InputStreamReader

import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
/**
 * Constructs a new InputStreamReader on the InputStream {@code in} and
 * CharsetDecoder {@code dec}.
 *
 * @param in
 *            the source InputStream from which to read characters.
 * @param dec
 *            the CharsetDecoder used by the character conversion.
 */
public InputStreamReader(InputStream in, CharsetDecoder dec) {
    super(in);
    dec.averageCharsPerByte();
    this.in = in;
    decoder = dec;
    bytes.limit(0);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:InputStreamReader.java


注:本文中的java.nio.charset.CharsetDecoder.averageCharsPerByte方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。