Examples of CharBuffer


Examples of java.nio.CharBuffer

      throw new EOFException("Attempted to read " + stringLength + " bytes but no more than "
          + encodedString.length + " were available");
    }

    ByteBuffer byteBuf = ByteBuffer.wrap(encodedString);
    CharBuffer charBuf = charsetDecoder.decode(byteBuf);

    return charBuf.toString();
  }
View Full Code Here

Examples of java.nio.CharBuffer

        // 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);
            dest.flip();

            if (result.isUnderflow()) { // done reading
                // make sure there is at least one extra character
                if (dest.limit() == dest.capacity()) {
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
                    dest.flip();
                }
                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)
                if (!getSource().allowEncodingErrors()) {
                    log.error(new SimpleDiagnosticPosition(dest.limit()),
                              "illegal.char.for.encoding",
                              charset == null ? encodingName : charset.name());
                } else {
                    log.warning(new SimpleDiagnosticPosition(dest.limit()),
                                "illegal.char.for.encoding",
                                charset == null ? encodingName : charset.name());
                }

                // skip past the coding error
                inbuf.position(inbuf.position() + result.length());

                // undo the flip() to prepare the output buffer
                // for more translation
                dest.position(dest.limit());
                dest.limit(dest.capacity());
                dest.put((char)0xfffd); // backward compatible
            } else {
                throw new AssertionError(result);
            }
        }
        // unreached
View Full Code Here

Examples of java.nio.CharBuffer

      throw new EOFException("Attempted to read " + stringLength + " bytes but no more than "
          + encodedString.length + " were available");
    }

    ByteBuffer byteBuf = ByteBuffer.wrap(encodedString);
    CharBuffer charBuf = charsetDecoder.decode(byteBuf);

    return charBuf.toString();
  }
View Full Code Here

Examples of java.nio.CharBuffer

      return s;

  byte[] ba = new byte[n];
  StringBuffer sb = new StringBuffer(n);
  ByteBuffer bb = ByteBuffer.allocate(n);
  CharBuffer cb = CharBuffer.allocate(n);
  CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
      .onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE);

  // This is not horribly efficient, but it will do for now
  char c = s.charAt(0);
      boolean betweenBrackets = false;

  for (int i = 0; i < n;) {
      assert c == s.charAt(i)// Loop invariant
      if (c == '[') {
    betweenBrackets = true;
      } else if (betweenBrackets && c == ']') {
    betweenBrackets = false;
      }
      if (c != '%' || betweenBrackets) {
    sb.append(c);
    if (++i >= n)
        break;
    c = s.charAt(i);
    continue;
      }
      bb.clear();
      int ui = i;
      for (;;) {
    assert (n - i >= 2);
    bb.put(decode(s.charAt(++i), s.charAt(++i)));
    if (++i >= n)
        break;
    c = s.charAt(i);
    if (c != '%')
        break;
      }
      bb.flip();
      cb.clear();
      dec.reset();
      CoderResult cr = dec.decode(bb, cb, true);
      assert cr.isUnderflow();
      cr = dec.flush(cb);
      assert cr.isUnderflow();
      sb.append(cb.flip().toString());
  }

  return sb.toString();
    }
View Full Code Here

Examples of java.nio.CharBuffer

      char[] ca = new char[en];
      if (len == 0)
    return ca;
      cd.reset();
      ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
      CharBuffer cb = CharBuffer.wrap(ca);
      try {
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        cr.throwException();
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        cr.throwException();
      } catch (CharacterCodingException x) {
    // Substitution is always enabled,
    // so this shouldn't happen
    throw new Error(x);
      }
      return safeTrim(ca, cb.position(), cs);
  }
View Full Code Here

Examples of java.nio.CharBuffer

      if (len == 0)
    return ba;

      ce.reset();
      ByteBuffer bb = ByteBuffer.wrap(ba);
      CharBuffer cb = CharBuffer.wrap(ca, off, len);
      try {
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        cr.throwException();
    cr = ce.flush(bb);
View Full Code Here

Examples of java.nio.CharBuffer

   * @param to the object to write to
   * @return the number of characters copied
   * @throws IOException if an I/O error occurs
   */
  public static long copy(Readable from, Appendable to) throws IOException {
    CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
    long total = 0;
    while (true) {
      int r = from.read(buf);
      if (r == -1) {
        break;
      }
      buf.flip();
      to.append(buf, 0, r);
      total += r;
    }
    return total;
  }
View Full Code Here

Examples of net.sourceforge.chaperon.process.extended.CharBuffer

    super(name);
  }

  public void testCharBuffer()
  {
    CharBuffer buffer = new CharBuffer();

    assertTrue("Test if read is not possible", !buffer.available());

    char[] chars1 = "abcde".toCharArray();
    buffer.push(chars1, 1, 3)// "bcd"

    assertTrue("Test if read is possible", buffer.available());

    assertEquals("Test text", 'b', buffer.read());

    char[] chars2 = "fghij".toCharArray();
    buffer.push(chars2, 2, 3)// "hij"

    assertEquals("Test text", 'c', buffer.read());
    assertEquals("Test text", 'd', buffer.read());
    assertEquals("Test text", 'h', buffer.read());
    assertEquals("Test text", 'i', buffer.read());

    assertTrue("Test if read is possible", buffer.available());

    assertEquals("Test text", 'j', buffer.read());

    assertTrue("Test if read is not possible", !buffer.available());

    try
    {
      buffer.read();
      fail("Test for IllegalStateException");
    }
    catch (IllegalStateException e) {}

    char[] chars3 = "klmno".toCharArray();
    buffer.push(chars3, 0, 2)// "kl"

    assertTrue("Test if read is possible", buffer.available());

    assertEquals("Test text", 'k', buffer.read());
    assertEquals("Test text", 'l', buffer.read());

    try
    {
      buffer.read();
      fail("Test for IllegalStateException");
    }
    catch (IllegalStateException e) {}

    buffer.back();
    assertEquals("Test text", 'l', buffer.peek());
    buffer.back();
    assertEquals("Test text", 'k', buffer.peek());

    assertTrue("Test if read is possible", buffer.available());

    buffer.back();
    assertEquals("Test text", 'j', buffer.peek());
    buffer.back();
    assertEquals("Test text", 'i', buffer.peek());
    buffer.back();
    assertEquals("Test text", 'h', buffer.peek());
    buffer.back();
    assertEquals("Test text", 'd', buffer.peek());
    buffer.back();
    assertEquals("Test text", 'c', buffer.peek());
    buffer.back();
    assertEquals("Test text", 'b', buffer.peek());

    assertTrue("Test if read is possible", buffer.available());

    try
    {
      buffer.back();
      fail("Test for IllegalStateException");
    }
    catch (IllegalStateException e) {}
  }
View Full Code Here

Examples of net.sourceforge.chaperon.process.extended.CharBuffer

    catch (IllegalStateException e) {}
  }

  public void testSimple()
  {
    CharBuffer buffer = new CharBuffer();

    assertTrue("Test if read is not possible", !buffer.available());

    char[] chars1 = "abc".toCharArray();
    buffer.push(chars1, 0, 3);

    assertTrue("Test if read is possible", buffer.available());

    assertEquals("Test text", 'a', buffer.read());

    assertTrue("Test if read is possible", buffer.available());

    assertEquals("Test text", 'b', buffer.read());

    assertTrue("Test if read is possible", buffer.available());

    assertEquals("Test text", 'c', buffer.read());

    assertTrue("Test if read is not possible", !buffer.available());

    // change direction
    buffer.back();
    assertEquals("Test text", 'c', buffer.peek());
  }
View Full Code Here

Examples of net.yacy.kelondro.io.CharBuffer

    }

    public boolean checkSignature() {
        if(this.releaseFile != null) {
            try {
                final CharBuffer signBuffer = new CharBuffer(getSignatureFile());
                final byte[] signByteBuffer = Base64Order.standardCoder.decode(signBuffer.toString().trim());
                final CryptoLib cl = new CryptoLib();
                for(final yacyUpdateLocation updateLocation : latestReleaseLocations) {
                    try {
                        if(cl.verifySignature(updateLocation.getPublicKey(),
                            new FileInputStream(this.releaseFile), signByteBuffer)) {
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.