Package java.nio

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


      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

      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

      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

   * @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

        }
       
        ByteBuffer buf = ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin() + start, end - start);
       
        try {
            CharBuffer cbuf = fromEncoding.decode(buf);
            buf = toEncoding.encode(cbuf);
        } catch (MalformedInputException e) {
        } catch (UnmappableCharacterException e) {
        } catch (CharacterCodingException e) {
            throw getRuntime().newInvalidEncoding("invalid sequence");
View Full Code Here

        }
       
        ByteList bytes = str.convertToString().getByteList();
        ByteBuffer buf = ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin(), bytes.length());
        try {
            CharBuffer cbuf = decoder.decode(buf);
            buf = encoder.encode(cbuf);
        } catch (CharacterCodingException e) {
            throw runtime.newArgumentError("invalid encoding");
        }
        byte[] arr = buf.array();
View Full Code Here

                        "java:comp/env/" + JNDI_NAME);
            } catch (NamingException e) {
                // Ignore - the test checks if the message is sent
                e.printStackTrace(); // for debug purposes if the test fails
            }
            CharBuffer cb = CharBuffer.wrap("" + msg);
            try {
                outbound.writeTextMessage(cb);
            } catch (IOException e) {
                // Ignore - the test checks if the message is sent
            }
View Full Code Here

        = IndexableBinaryStringTools.getEncodedLength(collationKeyBuf);
      if (encodedLength > termBuffer.length) {
        termAtt.resizeTermBuffer(encodedLength);
      }
      termAtt.setTermLength(encodedLength);
      CharBuffer wrappedTermBuffer = CharBuffer.wrap(termAtt.termBuffer());
      IndexableBinaryStringTools.encode(collationKeyBuf, wrappedTermBuffer);
      return true;
    } else {
      return false;
    }
View Full Code Here

        = IndexableBinaryStringTools.getEncodedLength(collationKeyBuf);
      if (encodedLength > termBuffer.length) {
        termAtt.resizeTermBuffer(encodedLength);
      }
      termAtt.setTermLength(encodedLength);
      CharBuffer wrappedTermBuffer = CharBuffer.wrap(termAtt.termBuffer());
      IndexableBinaryStringTools.encode(collationKeyBuf, wrappedTermBuffer);
      return true;
    } else {
      return false;
    }
View Full Code Here

TOP

Related Classes of java.nio.CharBuffer

Copyright © 2018 www.massapicom. 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.