Example usage for java.nio CharBuffer put

List of usage examples for java.nio CharBuffer put

Introduction

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

Prototype

public final CharBuffer put(String str) 

Source Link

Document

Writes all chars of the given string to the current position of this buffer, and increases the position by the length of string.

Usage

From source file:com.cloudbees.jenkins.support.filter.FilteredWriterTest.java

@Issue("JENKINS-21670")
@Test/*from ww  w.j a  v a  2s .  com*/
public void shouldSupportLinesLongerThanDefaultBufferSize() throws Exception {
    CharBuffer input = CharBuffer.allocate(FilteredConstants.DEFAULT_DECODER_CAPACITY * 10);
    for (int i = 0; i < input.capacity(); i++) {
        input.put('+');
    }
    input.flip();
    CharSequenceReader reader = new CharSequenceReader(input);
    ContentFilter filter = s -> s.replace('+', '-');
    StringWriter output = new StringWriter();
    FilteredWriter writer = new FilteredWriter(output, filter);

    IOUtils.copy(reader, writer);
    assertThat(output.toString()).isEmpty();

    writer.flush();

    assertThat(output.toString()).isNotEmpty().matches("^-+$");

}

From source file:com.cloudbees.jenkins.support.filter.FilteredOutputStreamTest.java

@Issue("JENKINS-21670")
@Test//w  w w . ja  v  a 2s .  c  om
public void shouldSupportLinesLargerThanDefaultBufferSize() throws IOException {
    CharBuffer input = CharBuffer.allocate(FilteredConstants.DEFAULT_DECODER_CAPACITY * 10);
    for (int i = 0; i < input.capacity(); i++) {
        input.put('*');
    }
    input.flip();
    InputStream in = new CharSequenceInputStream(input, UTF_8);
    FilteredOutputStream out = new FilteredOutputStream(testOutput, s -> s.replace('*', 'a'));

    IOUtils.copy(in, out);
    String contents = new String(testOutput.toByteArray(), UTF_8);

    assertThat(contents).isEmpty();

    out.flush();
    contents = new String(testOutput.toByteArray(), UTF_8);

    assertThat(contents).isNotEmpty().matches("^a+$");
}

From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.HMACAuthenticator.java

private String generateToken(final String username, final String salt, final String time) {
    try {//  ww  w  .  j  a v a 2 s.  c o m
        final CharBuffer secretAndSalt = CharBuffer.allocate(secret.length + salt.length() + 1);
        secretAndSalt.put(secret);
        secretAndSalt.put(":");
        secretAndSalt.put(salt);
        final String tokenPrefix = username + ":" + time.toString() + ":";
        final SecretKeySpec keySpec = new SecretKeySpec(toBytes(secretAndSalt.array()), hmacAlgo);
        final Mac hmac = Mac.getInstance(hmacAlgo);
        hmac.init(keySpec);
        hmac.update(username.getBytes());
        hmac.update(time.toString().getBytes());
        final Base64.Encoder encoder = Base64.getUrlEncoder();
        final byte[] hmacbytes = encoder.encode(hmac.doFinal());
        final byte[] tokenbytes = tokenPrefix.getBytes();
        final byte[] token = ByteBuffer.wrap(new byte[tokenbytes.length + hmacbytes.length]).put(tokenbytes)
                .put(hmacbytes).array();
        return new String(encoder.encode(token));
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.alpharogroup.random.RandomExtensionsTest.java

/**
 * Test method for {@link RandomExtensions#randomChar(java.lang.String)} .
 *//*from   www  . ja  v  a2s  .c o m*/
@Test
public void testRandomChar() {
    final String string = Constants.LOWCASECHARS;
    for (int i = 0; i < 100; i++) {
        final char randomChar = RandomExtensions.randomChar(string);
        final CharBuffer charBuffer = CharBuffer.allocate(1);
        charBuffer.put(randomChar);
        this.result = string.contains(charBuffer);
        AssertJUnit.assertTrue("", this.result);
    }
}

From source file:de.alpharogroup.random.RandomExtensionsTest.java

/**
 * Test method for {@link RandomExtensions#getRandomString(java.lang.String, int)} .
 *//*from   w w w  .j av a 2 s.c  o m*/
@Test
public void testRandomStringStringInt() {
    final CharBuffer charBuffer = CharBuffer.allocate(45);
    final int length = 5;
    final String chars = Constants.LCCHARSWNASC;
    charBuffer.put(chars);
    for (int i = 0; i < 100; i++) {
        final String randomString = RandomExtensions.getRandomString(chars, length);
        this.result = randomString.contains(charBuffer);
        AssertJUnit.assertTrue("", this.result);
    }
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

/**
 * Add characters(nodes) to tail array// w ww  .  j a v  a  2s  . co m
 *
 * @param node
 */
private void addToTail(Trie.Node node) {
    while (true) {
        if (tailBuffer.capacity() < tailIndex - TAIL_OFFSET + 1) {
            CharBuffer newTailBuffer = CharBuffer
                    .allocate(tailBuffer.capacity() + (int) (tailBuffer.capacity() * BUFFER_GROWTH_PERCENTAGE));
            tailBuffer.rewind();
            newTailBuffer.put(tailBuffer);
            tailBuffer = newTailBuffer;
        }
        tailBuffer.put(tailIndex++ - TAIL_OFFSET, node.getKey());// set character of current node

        if (node.getChildren().isEmpty()) { // if it reached the end of input, break.
            break;
        }
        node = node.getChildren().get(0); // Move to next node
    }
}

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

private void emit(int c) throws IOException {
    assert c >= 0;
    CharBuffer buf = lineBuffer;//from  w w  w .  j  a  v a 2s . c o m
    if (buf.remaining() == 0) {
        if (buf.capacity() == BUFFER_LIMIT) {
            throw new IOException(
                    MessageFormat.format("Line is too large (near {0}:{1}, size={2}, record-number={3})", path,
                            currentPhysicalHeadLine, BUFFER_LIMIT, currentRecordNumber));
        }
        CharBuffer newBuf = CharBuffer.allocate(Math.min(buf.capacity() * 2, BUFFER_LIMIT));
        newBuf.clear();
        buf.flip();
        newBuf.put(buf);
        buf = newBuf;
        lineBuffer = newBuf;
    }
    buf.put((char) c);
}

From source file:org.apache.fop.fonts.MultiByteFont.java

/**
 * Map sequence GS, comprising a sequence of Glyph Indices, to output sequence CS,
 * comprising a sequence of UTF-16 encoded Unicode Code Points.
 * @param gs a GlyphSequence containing glyph indices
 * @returns a CharSequence containing UTF-16 encoded Unicode characters
 *//*from  w  w  w. j  a  va 2  s.c om*/
private CharSequence mapGlyphsToChars(GlyphSequence gs) {
    int ng = gs.getGlyphCount();
    CharBuffer cb = CharBuffer.allocate(ng);
    int ccMissing = Typeface.NOT_FOUND;
    for (int i = 0, n = ng; i < n; i++) {
        int gi = gs.getGlyph(i);
        int cc = findCharacterFromGlyphIndex(gi);
        if ((cc == 0) || (cc > 0x10FFFF)) {
            cc = ccMissing;
            log.warn("Unable to map glyph index " + gi + " to Unicode scalar in font '" + getFullName()
                    + "', substituting missing character '" + (char) cc + "'");
        }
        if (cc > 0x00FFFF) {
            int sh;
            int sl;
            cc -= 0x10000;
            sh = ((cc >> 10) & 0x3FF) + 0xD800;
            sl = ((cc >> 0) & 0x3FF) + 0xDC00;
            cb.put((char) sh);
            cb.put((char) sl);
        } else {
            cb.put((char) cc);
        }
    }
    cb.flip();
    return (CharSequence) cb;
}