Example usage for java.nio CharBuffer length

List of usage examples for java.nio CharBuffer length

Introduction

In this page you can find the example usage for java.nio CharBuffer length.

Prototype

public final int length() 

Source Link

Document

Returns the number of remaining chars.

Usage

From source file:org.paxle.core.norm.impl.ReferenceNormalizer.java

static String urlDecode(final String str, final Charset charset) throws ParseException {
    int percent = str.indexOf('%');
    if (percent == -1)
        return str;

    final StringBuffer sb = new StringBuffer(str.length()); // buffer to build the converted string
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(8); // buffer for conversion of contiguous %-encoded bytes
    int last = 0;
    final int len = str.length();
    do {//from   www.  ja v a 2 s .c  om
        sb.append(str.substring(last, percent)); // write non-encoded part

        // loop to convert sequence of %-encoded tokens into bytes. Contiguous byte-sequences have to be dealt with
        // in one block before decoding, because - dependant on the charset - more than one byte may be needed to
        // represent a single character. If the conversion to bytes was done sequentially, decoding might fail
        do {
            if (percent + 3 > str.length())
                throw new ParseException("unexpected end of input", percent + 3);
            final String token = str.substring(percent + 1, percent + 3);
            if (!token.matches("[0-9a-fA-F]{2}"))
                throw new ParseException("illegal url-encoded token '" + token + "'", percent);

            final int tokenValue = Integer.parseInt(token, 16) & 0xFF;
            baos.write(tokenValue);
            percent += 3;
        } while (percent < len && str.charAt(percent) == '%');

        if (baos.size() > 0) {
            final CharBuffer decoded = charset.decode(ByteBuffer.wrap(baos.toByteArray()));
            baos.reset(); // reuse the ByteArrayOutputStream in the next run
            for (int i = 0; i < decoded.length(); i++) {
                final char c = decoded.charAt(i);
                switch (c) {
                case '#':
                    sb.append("%23");
                    continue;
                case '%':
                    sb.append("%25");
                    continue;
                case '&':
                    sb.append("%26");
                    continue;
                case '=':
                    sb.append("%3D");
                    continue;
                case '?':
                    sb.append("%3F");
                    continue;
                default:
                    sb.append(c);
                    continue;
                }
            }
        }

        last = percent; // byte after the token
        percent = str.indexOf('%', last); // search for next token, returns -1 if last > len
    } while (percent != -1);
    return sb.append(str.substring(last)).toString();
}

From source file:org.sonatype.nexus.obr.metadata.DefaultObrResourceReader.java

public int read(final CharBuffer cb) throws IOException {
    // just here to complete the Reader API, it's not actually used

    try {/*from   w  w  w  .  j  a va  2  s.  c om*/
        parser.nextToken();
    } catch (final XmlPullParserException e) {
        throw new LocalStorageException("Error parsing XML token", e);
    }

    final int n = cb.length();
    cb.append(parser.getText());
    return cb.length() - n;
}