Example usage for java.nio CharBuffer hasRemaining

List of usage examples for java.nio CharBuffer hasRemaining

Introduction

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

Prototype

public final boolean hasRemaining() 

Source Link

Document

Indicates if there are elements remaining in this buffer, that is if position < limit .

Usage

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();/*from www  .  j  av a2 s. com*/
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();/*from  w ww.  j av  a 2 s .  c o  m*/
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
        System.out.println(bb.position() + " -> " + bb.get());
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
    FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
    System.out.println("Float Buffer");
    while (fb.hasRemaining())
        System.out.println(fb.position() + " -> " + fb.get());
    IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
    System.out.println("Int Buffer");
    while (ib.hasRemaining())
        System.out.println(ib.position() + " -> " + ib.get());
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
        System.out.println(lb.position() + " -> " + lb.get());
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
        System.out.println(sb.position() + " -> " + sb.get());
    DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
    System.out.println("Double Buffer");
    while (db.hasRemaining())
        System.out.println(db.position() + " -> " + db.get());
}

From source file:MainClass.java

License:asdf

private static void drainBuffer(CharBuffer buffer) {
    while (buffer.hasRemaining()) {
        System.out.print(buffer.get());
    }/* ww w.ja va 2 s  .c  o  m*/

    System.out.println("");
}

From source file:MainClass.java

private static void symmetricScramble(CharBuffer buffer) {
    while (buffer.hasRemaining()) {
        buffer.mark();//w ww .j  av a2 s.  c om
        char c1 = buffer.get();
        char c2 = buffer.get();
        buffer.reset();
        buffer.put(c2).put(c1);
    }
}

From source file:Main.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string//from   ww w  . ja  v a  2 s .c  o m
 */
private static String urldecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:com.gistlabs.mechanize.util.apache.URLEncodedUtils.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 * /*from   ww w .ja  v  a 2s .  c  o m*/
 * @param content the portion to decode
 * @param charset the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return
 */
private static String urldecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null)
        return null;
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1)
                bb.put((byte) ((u << 4) + l));
            else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+')
            bb.put((byte) ' ');
        else
            bb.put((byte) c);
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:com.mcxiaoke.next.http.util.URLUtils.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string//from   w w w . j a v a  2  s .com
 */
private static String urlDecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    final ByteBuffer bb = ByteBuffer.allocate(content.length());
    final CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        final char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            final char uc = cb.get();
            final char lc = cb.get();
            final int u = Character.digit(uc, 16);
            final int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:de.sainth.recipe.backend.security.AuthFilter.java

private Optional<RecipeManagerAuthenticationToken> parseBasicAuth(String header)
        throws IOException, ServletException {
    byte[] base64Token = header.substring(6).getBytes();
    CharBuffer decoded = StandardCharsets.UTF_8
            .decode(Base64.getDecoder().decode(ByteBuffer.wrap(base64Token)));
    char c = decoded.get();
    StringBuilder sBuilder = new StringBuilder();
    while (decoded.hasRemaining() && c != ':') {
        sBuilder.append(c);/*from  w w  w  .j a  v  a  2s  .  c  o  m*/
        c = decoded.get();
    }
    String username = sBuilder.toString();
    Optional<String> encryptedPassword = userRepository.getEncryptedPassword(username);
    if (encryptedPassword.isPresent()) {
        sBuilder.setLength(0);
        while (decoded.hasRemaining()) {
            sBuilder.append(decoded.get());
        }
        boolean matches = ScryptPasswordEncoder.sMatches(sBuilder, encryptedPassword.get());
        if (matches) {
            User user = userRepository.findOne(username);
            return Optional.of(new RecipeManagerAuthenticationToken(user.getId(), user.getPermission().name()));
        } else {
            return Optional.empty();
        }
    } else {
        return Optional.empty();
    }
}

From source file:org.apache.tika.parser.html.charsetdetector.charsets.XUserDefinedCharset.java

public CharsetDecoder newDecoder() {
    return new CharsetDecoder(this, 1, 1) {
        @Override/*  ww w.  jav  a 2s  .c  o  m*/
        protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
            while (true) {
                if (!in.hasRemaining())
                    return CoderResult.UNDERFLOW;
                if (!out.hasRemaining())
                    return CoderResult.OVERFLOW;
                byte b = in.get();
                out.append((char) ((b >= 0) ? b : 0xF700 + (b & 0xFF)));
            }
        }
    };
}

From source file:org.openhab.io.transport.cul.internal.network.CULNetworkHandlerImpl.java

private void onRead(ByteBuffer readBuf) {
    CharBuffer charBuf = cs.decode(readBuf);
    while (charBuf.hasRemaining()) {
        char currentChar = charBuf.get();
        if (currentChar == '\r' || currentChar == '\n') {
            String command = commandBuffer.toString();
            if (!StringUtils.isEmpty(command)) {
                processNextLine(command);
            }//w w  w  .j  a v a 2 s .c om
            commandBuffer = new StringBuffer();
        } else {
            commandBuffer.append(currentChar);
        }
    }
}