Example usage for java.nio CharBuffer get

List of usage examples for java.nio CharBuffer get

Introduction

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

Prototype

public abstract char get();

Source Link

Document

Returns the char at the current position and increases the position by 1.

Usage

From source file:com.odiago.flumebase.io.CharBufferUtils.java

/**
 * Parses a CharSequence into a long in base 10.
 *//*from w  w w  . ja v  a2  s  .  c o m*/
public static long parseLong(CharBuffer chars) throws ColumnParseException {
    long result = 0L;

    final int limit = chars.limit();
    final int start = chars.position();
    if (0 == limit - start) {
        // The empty string can not be parsed as an integer.
        throw new ColumnParseException("No value provided");
    }

    boolean isNegative = false;
    for (int pos = start; pos < limit; pos++) {
        char cur = chars.get();
        if (pos == start && cur == '-') {
            isNegative = true;
            if (limit - start == 1) {
                // "-" is not an integer we accept.
                throw new ColumnParseException("No integer part provided");
            }
        } else if (Character.isDigit(cur)) {
            byte digitVal = (byte) (cur - '0');
            result = result * 10 - digitVal;
            // TODO: Detect over/underflow and signal exception?
        } else {
            throw new ColumnParseException("Invalid character in number");
        }
    }

    // We built up the value as a negative, to use the larger "half" of the
    // integer range. If it's not negative, flip it on return.
    return isNegative ? result : -result;
}

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 ww w.  j  av  a 2 s .  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:Base64Encoder.java

/**
 * Encodes one or more characters into one or more bytes.
 *
 * <p> This method encapsulates the basic encoding loop, encoding as many
 * characters as possible until it either runs out of input, runs out of room
 * in the output buffer, or encounters an encoding error.  This method is
 * invoked by the {@link #encode encode} method, which handles result
 * interpretation and error recovery./* ww w.  j a v  a  2s . c o m*/
 *
 * <p> The buffers are read from, and written to, starting at their current
 * positions.  At most {@link Buffer#remaining in.remaining()} characters
 * will be read, and at most {@link Buffer#remaining out.remaining()}
 * bytes will be written.  The buffers' positions will be advanced to
 * reflect the characters read and the bytes written, but their marks and
 * limits will not be modified.
 *
 * <p> This method returns a {@link CoderResult} object to describe its
 * reason for termination, in the same manner as the {@link #encode encode}
 * method.  Most implementations of this method will handle encoding errors
 * by returning an appropriate result object for interpretation by the
 * {@link #encode encode} method.  An optimized implementation may instead
 * examine the relevant error action and implement that action itself.
 *
 * <p> An implementation of this method may perform arbitrary lookahead by
 * returning {@link CoderResult#UNDERFLOW} until it receives sufficient
 * input.  </p>
 *
 * @param  in
 *         The input character buffer
 *
 * @param  out
 *         The output byte buffer
 *
 * @return  A coder-result object describing the reason for termination
 */
public java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer in, java.nio.ByteBuffer out) {
    if (excessByte != null) {
        if (out.remaining() > 0) {
            out.put(excessByte.byteValue());
            excessByte = null;
        } else
            return CoderResult.OVERFLOW;

    }
    while (in.remaining() > 0) {
        char inch = in.get();
        int code = (int) inch >= encTable.length ? CHARCODE_INVALID : encTable[(int) inch];
        if (encState < 4) {
            switch (code) {
            case CHARCODE_INVALID:
                throw new IllegalArgumentException("Invalid base-64 character'" + inch + "'");
            case CHARCODE_WHITESPACE:
                break;
            case CHARCODE_PADDER:
                if (encState == 1)
                    throw new IllegalArgumentException("Mal-formed base-64 (= after one character");
                encState = 4;
                break;
            default:
                switch (encState) {
                case 0:
                    bits = code << 2;
                    encState = 1;
                    break;
                case 1:
                    encState = 2;
                    int v = bits | ((code >> 4) & 3);
                    bits = (code << 4) & 0xF0;
                    if (!out(out, v))
                        return CoderResult.OVERFLOW;
                    break;
                case 2:
                    encState = 3;
                    v = bits | (code >> 2) & 0x0f;
                    bits = (code << 6) & 0xC0;
                    if (!out(out, v))
                        return CoderResult.OVERFLOW;
                    break;
                case 3:
                    encState = 0;
                    bits |= (code & 0x3f);
                    if (!out(out, bits))
                        return CoderResult.OVERFLOW;
                    break;
                }

                break;
            }

        }
    }
    return CoderResult.UNDERFLOW;
}

From source file:de.undercouch.bson4jackson.BsonGeneratorTest.java

private void assertRaw(byte[] r) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(r);
    BSONDecoder decoder = new BasicBSONDecoder();
    BSONObject obj = decoder.readObject(bais);
    byte[] o = (byte[]) obj.get("Test");
    CharBuffer buf = ByteBuffer.wrap(o).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
    assertEquals(2, buf.remaining());// w  ww .  j a  va2  s . c  o m
    char a = buf.get();
    char b = buf.get();
    assertEquals('a', a);
    assertEquals('b', b);
}

From source file:com.asakusafw.runtime.io.csv.CsvParser.java

private int getNextCharacter() throws IOException {
    CharBuffer buf = readerBuffer;
    if (buf.remaining() == 0) {
        buf.clear();//from   w  ww  .j a  v  a  2 s  . c  o m
        int read = reader.read(buf);
        buf.flip();
        assert read != 0;
        if (read < 0) {
            return EOF;
        }
    }
    return buf.get();
}

From source file:com.cloudera.sqoop.lib.RecordParser.java

/**
 * Return a list of strings representing the fields of the input line.
 * This list is backed by an internal buffer which is cleared by the
 * next call to parseRecord()./*from   w  w w . j a  v  a 2  s  .  c  o  m*/
 */
public List<String> parseRecord(CharBuffer input) throws ParseError {
    if (null == input) {
        throw new ParseError("null input string");
    }

    /*
      This method implements the following state machine to perform
      parsing.
            
      Note that there are no restrictions on whether particular characters
      (e.g., field-sep, record-sep, etc) are distinct or the same. The
      state transitions are processed in the order seen in this comment.
            
      Starting state is FIELD_START
        encloser -> ENCLOSED_FIELD
        escape char -> UNENCLOSED_ESCAPE
        field delim -> FIELD_START (for a new field)
        record delim -> stops processing
        all other letters get added to current field, -> UNENCLOSED FIELD
            
      ENCLOSED_FIELD state:
        escape char goes to ENCLOSED_ESCAPE
        encloser goes to ENCLOSED_EXPECT_DELIMITER
        field sep or record sep gets added to the current string
        normal letters get added to the current string
            
      ENCLOSED_ESCAPE state:
        any character seen here is added literally, back to ENCLOSED_FIELD
            
      ENCLOSED_EXPECT_DELIMITER state:
        field sep goes to FIELD_START
        record sep halts processing.
        all other characters are errors.
            
      UNENCLOSED_FIELD state:
        ESCAPE char goes to UNENCLOSED_ESCAPE
        FIELD_SEP char goes to FIELD_START
        RECORD_SEP char halts processing
        normal chars or the enclosing char get added to the current string
            
      UNENCLOSED_ESCAPE:
        add charater literal to current string, return to UNENCLOSED_FIELD
    */

    char curChar = DelimiterSet.NULL_CHAR;
    ParseState state = ParseState.FIELD_START;
    int len = input.length();
    StringBuilder sb = null;

    outputs.clear();

    char enclosingChar = delimiters.getEnclosedBy();
    char fieldDelim = delimiters.getFieldsTerminatedBy();
    char recordDelim = delimiters.getLinesTerminatedBy();
    char escapeChar = delimiters.getEscapedBy();
    boolean enclosingRequired = delimiters.isEncloseRequired();

    for (int pos = 0; pos < len; pos++) {
        curChar = input.get();
        switch (state) {
        case FIELD_START:
            // ready to start processing a new field.
            if (null != sb) {
                // We finished processing a previous field. Add to the list.
                outputs.add(sb.toString());
            }

            sb = new StringBuilder();
            if (enclosingChar == curChar) {
                // got an opening encloser.
                state = ParseState.ENCLOSED_FIELD;
            } else if (escapeChar == curChar) {
                state = ParseState.UNENCLOSED_ESCAPE;
            } else if (fieldDelim == curChar) {
                // we have a zero-length field. This is a no-op.
                continue;
            } else if (recordDelim == curChar) {
                // we have a zero-length field, that ends processing.
                pos = len;
            } else {
                // current char is part of the field.
                state = ParseState.UNENCLOSED_FIELD;
                sb.append(curChar);

                if (enclosingRequired) {
                    throw new ParseError("Opening field-encloser expected at position " + pos);
                }
            }

            break;

        case ENCLOSED_FIELD:
            if (escapeChar == curChar) {
                // the next character is escaped. Treat it literally.
                state = ParseState.ENCLOSED_ESCAPE;
            } else if (enclosingChar == curChar) {
                // we're at the end of the enclosing field. Expect an EOF or EOR char.
                state = ParseState.ENCLOSED_EXPECT_DELIMITER;
            } else {
                // this is a regular char, or an EOF / EOR inside an encloser. Add to
                // the current field string, and remain in this state.
                sb.append(curChar);
            }

            break;

        case UNENCLOSED_FIELD:
            if (escapeChar == curChar) {
                // the next character is escaped. Treat it literally.
                state = ParseState.UNENCLOSED_ESCAPE;
            } else if (fieldDelim == curChar) {
                // we're at the end of this field; may be the start of another one.
                state = ParseState.FIELD_START;
            } else if (recordDelim == curChar) {
                pos = len; // terminate processing immediately.
            } else {
                // this is a regular char. Add to the current field string,
                // and remain in this state.
                sb.append(curChar);
            }

            break;

        case ENCLOSED_ESCAPE:
            // Treat this character literally, whatever it is, and return to
            // enclosed field processing.
            sb.append(curChar);
            state = ParseState.ENCLOSED_FIELD;
            break;

        case ENCLOSED_EXPECT_DELIMITER:
            // We were in an enclosed field, but got the final encloser. Now we
            // expect either an end-of-field or an end-of-record.
            if (fieldDelim == curChar) {
                // end of one field is the beginning of the next.
                state = ParseState.FIELD_START;
            } else if (recordDelim == curChar) {
                // stop processing.
                pos = len;
            } else {
                // Don't know what to do with this character.
                throw new ParseError("Expected delimiter at position " + pos);
            }

            break;

        case UNENCLOSED_ESCAPE:
            // Treat this character literally, whatever it is, and return to
            // non-enclosed field processing.
            sb.append(curChar);
            state = ParseState.UNENCLOSED_FIELD;
            break;

        default:
            throw new ParseError("Unexpected parser state: " + state);
        }
    }

    if (state == ParseState.FIELD_START && curChar == fieldDelim) {
        // we hit an EOF/EOR as the last legal character and we need to mark
        // that string as recorded. This if block is outside the for-loop since
        // we don't have a physical 'epsilon' token in our string.
        if (null != sb) {
            outputs.add(sb.toString());
            sb = new StringBuilder();
        }
    }

    if (null != sb) {
        // There was a field that terminated by running out of chars or an EOR
        // character. Add to the list.
        outputs.add(sb.toString());
    }

    return outputs;
}

From source file:org.apache.sqoop.lib.RecordParser.java

/**
 * Return a list of strings representing the fields of the input line.
 * This list is backed by an internal buffer which is cleared by the
 * next call to parseRecord()./*from w  w w  .j a va  2 s. c  o  m*/
 */
public List<String> parseRecord(CharBuffer input) throws com.cloudera.sqoop.lib.RecordParser.ParseError {
    if (null == input) {
        throw new com.cloudera.sqoop.lib.RecordParser.ParseError("null input string");
    }

    /*
      This method implements the following state machine to perform
      parsing.
            
      Note that there are no restrictions on whether particular characters
      (e.g., field-sep, record-sep, etc) are distinct or the same. The
      state transitions are processed in the order seen in this comment.
            
      Starting state is FIELD_START
        encloser -> ENCLOSED_FIELD
        escape char -> UNENCLOSED_ESCAPE
        field delim -> FIELD_START (for a new field)
        record delim -> stops processing
        all other letters get added to current field, -> UNENCLOSED FIELD
            
      ENCLOSED_FIELD state:
        escape char goes to ENCLOSED_ESCAPE
        encloser goes to ENCLOSED_EXPECT_DELIMITER
        field sep or record sep gets added to the current string
        normal letters get added to the current string
            
      ENCLOSED_ESCAPE state:
        any character seen here is added literally, back to ENCLOSED_FIELD
            
      ENCLOSED_EXPECT_DELIMITER state:
        field sep goes to FIELD_START
        record sep halts processing.
        all other characters are errors.
            
      UNENCLOSED_FIELD state:
        ESCAPE char goes to UNENCLOSED_ESCAPE
        FIELD_SEP char goes to FIELD_START
        RECORD_SEP char halts processing
        normal chars or the enclosing char get added to the current string
            
      UNENCLOSED_ESCAPE:
        add charater literal to current string, return to UNENCLOSED_FIELD
    */

    char curChar = com.cloudera.sqoop.lib.DelimiterSet.NULL_CHAR;
    ParseState state = ParseState.FIELD_START;
    int len = input.length();
    StringBuilder sb = null;

    outputs.clear();

    char enclosingChar = delimiters.getEnclosedBy();
    char fieldDelim = delimiters.getFieldsTerminatedBy();
    char recordDelim = delimiters.getLinesTerminatedBy();
    char escapeChar = delimiters.getEscapedBy();
    boolean enclosingRequired = delimiters.isEncloseRequired();

    for (int pos = 0; pos < len; pos++) {
        curChar = input.get();
        switch (state) {
        case FIELD_START:
            // ready to start processing a new field.
            if (null != sb) {
                // We finished processing a previous field. Add to the list.
                outputs.add(sb.toString());
            }

            sb = new StringBuilder();
            if (enclosingChar == curChar) {
                // got an opening encloser.
                state = ParseState.ENCLOSED_FIELD;
            } else if (escapeChar == curChar) {
                state = ParseState.UNENCLOSED_ESCAPE;
            } else if (fieldDelim == curChar) {
                // we have a zero-length field. This is a no-op.
                continue;
            } else if (recordDelim == curChar) {
                // we have a zero-length field, that ends processing.
                pos = len;
            } else {
                // current char is part of the field.
                state = ParseState.UNENCLOSED_FIELD;
                sb.append(curChar);

                if (enclosingRequired) {
                    throw new com.cloudera.sqoop.lib.RecordParser.ParseError(
                            "Opening field-encloser expected at position " + pos);
                }
            }

            break;

        case ENCLOSED_FIELD:
            if (escapeChar == curChar) {
                // the next character is escaped. Treat it literally.
                state = ParseState.ENCLOSED_ESCAPE;
            } else if (enclosingChar == curChar) {
                // we're at the end of the enclosing field. Expect an EOF or EOR char.
                state = ParseState.ENCLOSED_EXPECT_DELIMITER;
            } else {
                // this is a regular char, or an EOF / EOR inside an encloser. Add to
                // the current field string, and remain in this state.
                sb.append(curChar);
            }

            break;

        case UNENCLOSED_FIELD:
            if (escapeChar == curChar) {
                // the next character is escaped. Treat it literally.
                state = ParseState.UNENCLOSED_ESCAPE;
            } else if (fieldDelim == curChar) {
                // we're at the end of this field; may be the start of another one.
                state = ParseState.FIELD_START;
            } else if (recordDelim == curChar) {
                pos = len; // terminate processing immediately.
            } else {
                // this is a regular char. Add to the current field string,
                // and remain in this state.
                sb.append(curChar);
            }

            break;

        case ENCLOSED_ESCAPE:
            // Treat this character literally, whatever it is, and return to
            // enclosed field processing.
            sb.append(curChar);
            state = ParseState.ENCLOSED_FIELD;
            break;

        case ENCLOSED_EXPECT_DELIMITER:
            // We were in an enclosed field, but got the final encloser. Now we
            // expect either an end-of-field or an end-of-record.
            if (fieldDelim == curChar) {
                // end of one field is the beginning of the next.
                state = ParseState.FIELD_START;
            } else if (recordDelim == curChar) {
                // stop processing.
                pos = len;
            } else {
                // Don't know what to do with this character.
                throw new com.cloudera.sqoop.lib.RecordParser.ParseError(
                        "Expected delimiter at position " + pos);
            }

            break;

        case UNENCLOSED_ESCAPE:
            // Treat this character literally, whatever it is, and return to
            // non-enclosed field processing.
            sb.append(curChar);
            state = ParseState.UNENCLOSED_FIELD;
            break;

        default:
            throw new com.cloudera.sqoop.lib.RecordParser.ParseError("Unexpected parser state: " + state);
        }
    }

    if (state == ParseState.FIELD_START && curChar == fieldDelim) {
        // we hit an EOF/EOR as the last legal character and we need to mark
        // that string as recorded. This if block is outside the for-loop since
        // we don't have a physical 'epsilon' token in our string.
        if (null != sb) {
            outputs.add(sb.toString());
            sb = new StringBuilder();
        }
    }

    if (null != sb) {
        // There was a field that terminated by running out of chars or an EOR
        // character. Add to the list.
        outputs.add(sb.toString());
    }

    return outputs;
}

From source file:org.bimserver.ifc.step.deserializer.IfcStepDeserializer.java

private String readString(String value) throws DeserializeException {
    String result = value.substring(1, value.length() - 1);
    // Replace all '' with '
    while (result.contains("''")) {
        int index = result.indexOf("''");
        result = result.substring(0, index) + "'" + result.substring(index + 2);
    }//from   w  w  w  .  j ava 2  s.  co m
    while (result.contains("\\S\\")) {
        int index = result.indexOf("\\S\\");
        char x = result.charAt(index + 3);
        ByteBuffer b = ByteBuffer.wrap(new byte[] { (byte) (x + 128) });
        CharBuffer decode = Charsets.ISO_8859_1.decode(b);
        result = result.substring(0, index) + decode.get() + result.substring(index + 4);
    }
    while (result.contains("\\X\\")) {
        int index = result.indexOf("\\X\\");
        int code = Integer.parseInt(result.substring(index + 3, index + 5), 16);
        ByteBuffer b = ByteBuffer.wrap(new byte[] { (byte) (code) });
        CharBuffer decode = Charsets.ISO_8859_1.decode(b);
        result = result.substring(0, index) + decode.get() + result.substring(index + 5);
    }
    while (result.contains("\\X2\\")) {
        int index = result.indexOf("\\X2\\");
        int indexOfEnd = result.indexOf("\\X0\\");
        if (indexOfEnd == -1) {
            throw new DeserializeException("\\X2\\ not closed with \\X0\\");
        }
        if ((indexOfEnd - index) % 4 != 0) {
            throw new DeserializeException("Number of hex chars in \\X2\\ definition not divisible by 4");
        }
        try {
            ByteBuffer buffer = ByteBuffer
                    .wrap(Hex.decodeHex(result.substring(index + 4, indexOfEnd).toCharArray()));
            CharBuffer decode = Charsets.UTF_16BE.decode(buffer);
            result = result.substring(0, index) + decode.toString() + result.substring(indexOfEnd + 4);
        } catch (DecoderException e) {
            throw new DeserializeException(e);
        }
    }
    while (result.contains("\\X4\\")) {
        int index = result.indexOf("\\X4\\");
        int indexOfEnd = result.indexOf("\\X0\\");
        if (indexOfEnd == -1) {
            throw new DeserializeException("\\X4\\ not closed with \\X0\\");
        }
        if ((indexOfEnd - index) % 8 != 0) {
            throw new DeserializeException("Number of hex chars in \\X4\\ definition not divisible by 8");
        }
        try {
            ByteBuffer buffer = ByteBuffer
                    .wrap(Hex.decodeHex(result.substring(index + 4, indexOfEnd).toCharArray()));
            CharBuffer decode = Charset.forName("UTF-32").decode(buffer);
            result = result.substring(0, index) + decode.toString() + result.substring(indexOfEnd + 4);
        } catch (DecoderException e) {
            throw new DeserializeException(e);
        } catch (UnsupportedCharsetException e) {
            throw new DeserializeException("UTF-32 is not supported on your system", e);
        }
    }
    // Replace all \\ with \
    while (result.contains("\\\\")) {
        int index = result.indexOf("\\\\");
        result = result.substring(0, index) + "\\" + result.substring(index + 2);
    }
    return result;
}

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);
            }/*from w  w  w. j ava2  s .  com*/
            commandBuffer = new StringBuffer();
        } else {
            commandBuffer.append(currentChar);
        }
    }
}