Package java.nio.charset

Examples of java.nio.charset.CharsetDecoder.maxCharsPerByte()


    }

    static String decodeString(ByteBuffer src, Charset charset) {
        final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
        final CharBuffer dst = CharBuffer.allocate(
                (int) ((double) src.remaining() * decoder.maxCharsPerByte()));
        try {
            CoderResult cr = decoder.decode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
View Full Code Here


        int n = 0;
        if (buffer.remaining() > 0) {
            n = (int) (buffer.remaining() * decoder.averageCharsPerByte());
            if (n == 0)
                n = (int) (buffer.remaining() * decoder.maxCharsPerByte());
        }
        CharBuffer result = CharBuffer.allocate(n);
        if (n == 0)
            return result;

View Full Code Here

    private char[] get14Buffer(byte[] data, String encoding) throws IOException {
        if (!Charset.isSupported(encoding)) throw new IOException("Unsupported encoding " + encoding);
        Charset charset = Charset.forName(encoding);
        CharsetDecoder cd = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        int en = (int) (cd.maxCharsPerByte() * data.length);
        char[] ca = new char[en];
        ByteBuffer bb = ByteBuffer.wrap(data);
        CharBuffer cb = CharBuffer.wrap(ca);
        CoderResult cr = cd.decode(bb, cb, true);
        if (!cr.isUnderflow()) {
View Full Code Here

        CharsetDecoder dec = cs.newDecoder();
        StringBuilder encoded3 = new StringBuilder();
        for (int i = 0; i < ascii.length; i++) {
            ByteBuffer in = ByteBuffer.allocate(1);
            in.put(ascii[i]).flip();
            CharBuffer out = CharBuffer.allocate((int)dec.maxCharsPerByte());
            dec.decode(in, out, (i == (ascii.length - 1)));
            if (out.flip().hasRemaining()) {
                encoded3.append(out);
            }
        }
View Full Code Here

            if (out.flip().hasRemaining()) {
                encoded3.append(out);
            }
        }

        CharBuffer out = CharBuffer.allocate((int)dec.maxCharsPerByte());
        dec.flush(out);
        if (out.flip().hasRemaining()) {
            encoded3.append(out);
        }
        assertEquals(encoded, encoded3.toString());
View Full Code Here

    }

    static String decodeString(ByteBuffer src, Charset charset) {
        final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
        final CharBuffer dst = CharBuffer.allocate(
                (int) ((double) src.remaining() * decoder.maxCharsPerByte()));
        try {
            CoderResult cr = decoder.decode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
View Full Code Here

      de.reset();
      int len = bb.remaining();
      CoderResult rt = de.decode(bb, cb, true);
      if (rt == CoderResult.OVERFLOW) {
        cb.flip();
        CharBuffer lcb = CharBuffer.allocate((int)(len * (double)de.maxCharsPerByte()));
        lcb.put(cb);
        cb = lcb;
        rt = de.decode(bb, cb, true);
      }
     
View Full Code Here

    }

    static String decodeString(ByteBuffer src, Charset charset) {
        final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
        final CharBuffer dst = CharBuffer.allocate(
                (int) ((double) src.remaining() * decoder.maxCharsPerByte()));
        try {
            CoderResult cr = decoder.decode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
View Full Code Here

        }

        // 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);
View Full Code Here

                }
                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

                // report coding error (warn only pre 1.5)
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.