Package java.nio.charset

Examples of java.nio.charset.CharsetEncoder.encode()


            encoder.onMalformedInput(CodingErrorAction.REPLACE);
            encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
            InputStream stream;
            try {
                byte[] bytes;
                ByteBuffer byteBuffer = encoder.encode(CharBuffer
                        .wrap(document.get()));
                if (byteBuffer.hasArray())
                    bytes = byteBuffer.array();
                else {
                    bytes = new byte[byteBuffer.limit()];
View Full Code Here


   * @param char_encoding is the character encoding
   */
  public IOBuffer(String str, String char_encoding)
    throws CharacterCodingException {
    CharsetEncoder encoder = Charset.forName(char_encoding).newEncoder();
    read_buf_ = encoder.encode(CharBuffer.wrap(str));
  }

  // Replaces unmappable characters when encoding with non-UTF-8 charsets.
  private static final String BAD_INPUT_REPLACEMENT = "?";

View Full Code Here

   * @param char_encoding is the character encoding
   */
  public IOBuffer(String str, String char_encoding)
    throws CharacterCodingException {
    CharsetEncoder encoder = Charset.forName(char_encoding).newEncoder();
    read_buf_ = encoder.encode(CharBuffer.wrap(str));
  }

  // Replaces unmappable characters when encoding with non-UTF-8 charsets.
  private static final String BAD_INPUT_REPLACEMENT = "?";

View Full Code Here

            ByteBuffer buf = ByteBuffer.wrap(str.getUnsafeBytes(), str.begin(), str.length());

            try {
                CharBuffer cbuf = decoder.decode(buf);
                encoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction.IGNORE);
                buf = encoder.encode(cbuf);
            } catch (CharacterCodingException e) {
                throw context.getRuntime().newArgumentError("invalid encoding");
            }
            byte[] arr = buf.array();
            ByteList r = new ByteList(arr, 0, buf.limit());
View Full Code Here

      CharBuffer cbuf = CharBuffer.allocate(1);
      ByteBuffer bbuf = ByteBuffer.allocate(1);

      cbuf.put("\u00a5");
      cbuf.position(0);
      enc.encode(cbuf, bbuf, true);
      if(bbuf.get(0) == '\\') {
        requiresEscapingEncoder = true;
      } else {
        cbuf.clear();
        bbuf.clear();
View Full Code Here

        cbuf.clear();
        bbuf.clear();
       
        cbuf.put("\u20a9");
        cbuf.position(0);
        enc.encode(cbuf, bbuf, true);
        if(bbuf.get(0) == '\\') {
          requiresEscapingEncoder = true;
        }
      }
    } catch(java.nio.charset.UnsupportedCharsetException ucex) {
View Full Code Here

        Charset to = lookupCharsetFor(runtime, toEncoding, fromEncoding.toString(), toEncoding.toString());
        CharsetEncoder encoder = getCharsetEncoder(context, runtime, to, opts);
        ByteBuffer fromBytes = ByteBuffer.wrap(value.getUnsafeBytes(), value.begin(), value.length());
       
        try {
            ByteBuffer toBytes = encoder.encode(from.decode(fromBytes));

            // CharsetEncoder#encode guarantees a newly-allocated buffer, so no need to copy.
            return new ByteList(toBytes.array(), toBytes.arrayOffset(),
                    toBytes.limit() - toBytes.arrayOffset(), toEncoding, false);
        } catch (CharacterCodingException e) {
View Full Code Here

   
    Charset charset = Charset.forName("ISO-8859-1");
      CharsetDecoder decoder = charset.newDecoder();
      CharsetEncoder encoder = charset.newEncoder();     
      try {
          ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(s));
          CharBuffer cbuf = decoder.decode(bbuf);
          return cbuf.toString();
      } catch (CharacterCodingException e) {
        s = s.replaceAll("[^A-Za-z0-9_+-]", "_");
        try {
View Full Code Here

            return safeTrim(ba, blen, cs, isTrusted);
        } else {
            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);
                if (!cr.isUnderflow())
                    cr.throwException();
View Full Code Here

        byte[] ba = new byte[len];
        if (len == 0)
            return ba;
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca);
        CoderResult cr = ce.encode(cb, bb, true);
        if (!cr.isUnderflow())
            throw new IllegalArgumentException(cr.toString());
        cr = ce.flush(bb);
        if (!cr.isUnderflow())
            throw new IllegalArgumentException(cr.toString());
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.