Package java.nio.charset

Examples of java.nio.charset.CharsetEncoder.maxBytesPerChar()


    }

    static ByteBuffer encodeString(CharBuffer src, Charset charset) {
        final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
        final ByteBuffer dst = ByteBuffer.allocate(
                (int) ((double) src.remaining() * encoder.maxBytesPerChar()));
        try {
            CoderResult cr = encoder.encode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
View Full Code Here


        .onUnmappableCharacter(CodingErrorAction.REPLACE);
    byte[] bytes = new byte[numBytes];
    int maxChars = (int) (decoder.maxCharsPerByte() * numBytes) + 1;
    char[] charsDecoded =
        new char[(int) (decoder.maxCharsPerByte() * numBytes) + 1];
    int maxBytes = (int) (encoder.maxBytesPerChar() * maxChars) + 1;
    byte[] bytesReencoded = new byte[maxBytes];

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    CharBuffer cb = CharBuffer.wrap(charsDecoded);
    ByteBuffer bbReencoded = ByteBuffer.wrap(bytesReencoded);
View Full Code Here

    // normal case
    CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES);
    assertSame(ec.charset(), cs);
    assertEquals(1.0, ec.averageBytesPerChar(), 0);
    assertTrue(ec.maxBytesPerChar() == MAX_BYTES);

    /*
     * ------------------------ Exceptional cases -------------------------
     */
    // NullPointerException: null charset
View Full Code Here

    byte[] ba = getLegalByteArray();
    // normal case
    CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, ba);
    assertSame(ec.charset(), cs);
    assertEquals(1.0, ec.averageBytesPerChar(), 0.0);
    assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
    assertSame(ba, ec.replacement());

    /*
     * ------------------------ Exceptional cases -------------------------
     */
 
View Full Code Here

        .onUnmappableCharacter(CodingErrorAction.REPLACE);
    byte[] bytes = new byte[numBytes];
    int maxChars = (int) (decoder.maxCharsPerByte() * numBytes) + 1;
    char[] charsDecoded =
        new char[(int) (decoder.maxCharsPerByte() * numBytes) + 1];
    int maxBytes = (int) (encoder.maxBytesPerChar() * maxChars) + 1;
    byte[] bytesReencoded = new byte[maxBytes];

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    CharBuffer cb = CharBuffer.wrap(charsDecoded);
    ByteBuffer bbReencoded = ByteBuffer.wrap(bytesReencoded);
View Full Code Here

    }

    static ByteBuffer encodeString(CharBuffer src, Charset charset) {
        final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
        final ByteBuffer dst = ByteBuffer.allocate(
                (int) ((double) src.remaining() * encoder.maxBytesPerChar()));
        try {
            CoderResult cr = encoder.encode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
View Full Code Here

        currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);

        // --- check & prepare encoding ---
        Charset charset = Charsets.toCharset(encoding);
        CharsetEncoder charsetEncoder = charset.newEncoder();
        float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
        if(maxBytesPerChar==1f) {
            // all one byte encodings are no problem
            byteDecrement = 1;
        } else if(charset == Charset.forName("UTF-8")) {
            // UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte
View Full Code Here

        if (string == null || string.isEmpty()) {
            return new byte[0];
        }

        CharsetEncoder ce = charset.newEncoder();
        int en = scale(string.length(), ce.maxBytesPerChar());
        byte[] ba = new byte[en];
        if (string.length() == 0)
            return ba;

        ce.reset();
View Full Code Here

        {
            String data = stringArg(args, 0);
            Charset charset = resolveEncoding(args, 1);
            CharsetEncoder encoder = Charsets.get().getEncoder(charset);

            if (encoder.averageBytesPerChar() == encoder.maxBytesPerChar()) {
                // Optimize for ASCII
                return Math.ceil(encoder.maxBytesPerChar() * data.length());
            }

            // Encode into a small temporary buffer to make counting easiest.
View Full Code Here

            Charset charset = resolveEncoding(args, 1);
            CharsetEncoder encoder = Charsets.get().getEncoder(charset);

            if (encoder.averageBytesPerChar() == encoder.maxBytesPerChar()) {
                // Optimize for ASCII
                return Math.ceil(encoder.maxBytesPerChar() * data.length());
            }

            // Encode into a small temporary buffer to make counting easiest.
            // I don't know of a better way.
            CharBuffer chars = CharBuffer.wrap(data);
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.