Package java.nio.charset

Examples of java.nio.charset.CharsetEncoder


        int start = 0;
        int idx = 0;

        int length = s.length();
        CharsetEncoder encoder = null;
        ByteBuffer byteBuffer = null;
        CharBuffer buffer = null;

        for (; idx < length; idx++) {
            char c = s.charAt(idx);

            if (!isLegalURIQueryChar(c)) {
                builder.append(s.substring(start, idx));

                if (encoder == null) {
                    encoder = Charset.forName("UTF-8").newEncoder();
                }
                if (buffer == null) {
                    buffer = CharBuffer.allocate(1);
                    byteBuffer = ByteBuffer.allocate(6); // max bytes size in UTF-8
                } else {
                    byteBuffer.limit(6);
                }

                buffer.put(0, c);

                buffer.rewind();
                byteBuffer.rewind();
                encoder.encode(buffer, byteBuffer, true);

                byteBuffer.flip();

                int limit = byteBuffer.limit();
                for (int i = 0; i < limit; i++) {
View Full Code Here


    /**
     * @see
     * com.alimama.mdrill.utils.zip.ZipEncoding#canEncode(java.lang.String)
     */
    public boolean canEncode(String name) {
        CharsetEncoder enc = this.charset.newEncoder();
        enc.onMalformedInput(CodingErrorAction.REPORT);
        enc.onUnmappableCharacter(CodingErrorAction.REPORT);

        return enc.canEncode(name);
    }
View Full Code Here

    /**
     * @see
     * com.alimama.mdrill.utils.zip.ZipEncoding#encode(java.lang.String)
     */
    public ByteBuffer encode(String name) {
        CharsetEncoder enc = this.charset.newEncoder();

        enc.onMalformedInput(CodingErrorAction.REPORT);
        enc.onUnmappableCharacter(CodingErrorAction.REPORT);

        CharBuffer cb = CharBuffer.wrap(name);
        ByteBuffer out = ByteBuffer.allocate(name.length()
                                             + (name.length() + 1) / 2);

        while (cb.remaining() > 0) {
            CoderResult res = enc.encode(cb, out,true);

            if (res.isUnmappable() || res.isMalformed()) {

                // write the unmappable characters in utf-16
                // pseudo-URL encoding style to ByteBuffer.
                if (res.length() * 6 > out.remaining()) {
                    out = ZipEncodingHelper.growBuffer(out, out.position()
                                                       + res.length() * 6);
                }

                for (int i=0; i<res.length(); ++i) {
                    ZipEncodingHelper.appendSurrogate(out,cb.get());
                }

            } else if (res.isOverflow()) {

                out = ZipEncodingHelper.growBuffer(out, 0);

            } else if (res.isUnderflow()) {

                enc.flush(out);
                break;

            }
        }

View Full Code Here

        this.maxLineLength = maxLineLength;
    }

    public void encode(IoSession session, Object message,
            ProtocolEncoderOutput out) throws Exception {
        CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
        if (encoder == null) {
            encoder = charset.newEncoder();
            session.setAttribute(ENCODER, encoder);
        }
View Full Code Here

        // Enable auto-expand for easier encoding
        buf.setAutoExpand(true);

        try {
            // output all headers except the content length
            CharsetEncoder encoder = Charset.defaultCharset().newEncoder();
            buf.putString("HTTP/1.1 ", encoder);
            buf.putString(String.valueOf(msg.getResponseCode()), encoder);
            switch (msg.getResponseCode()) {
            case HttpResponseMessage.HTTP_STATUS_SUCCESS:
                buf.putString(" OK", encoder);
View Full Code Here

    private static String makeSafe(String s)
    {
        Charset charset = Charset.forName(System.getProperty("file.encoding"));
        if (charset == null)
            throw new IllegalStateException("Default character set is null!");
        CharsetEncoder cEncoder = charset.newEncoder();
        StringBuffer result = new StringBuffer();
        int i;
        for (i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            if (!cEncoder.canEncode(c))
                break;
        }
        for (; i < s.length(); i++)
        {
            char c = s.charAt(i);
            if (cEncoder.canEncode(c))
                result.append(c);
            else
            {
                String hexValue = Integer.toHexString((int) c);
                switch (hexValue.length())
View Full Code Here

    // later on during sorting and iteration.
    //
    entries = list;
    int i, o;

    final CharsetEncoder nameEncoder = state.nameEncoder;
    for (i = 0, o = 0; i < entries.length; i++) {
      final Entry e = entries[i];
      if (e == null)
        continue;
      final String name = e.getName();
View Full Code Here

          }
      }
   
    ByteBuffer appBuffer = ByteBuffer.allocate(serverEngine.getSession().getApplicationBufferSize());
    Charset cs = Charset.forName("US-ASCII");
    CharsetEncoder enc = cs.newEncoder();
    enc.encode(CharBuffer.wrap(TEST_MESSAGE), appBuffer, true);
    appBuffer.flip();
    result = clientEngine.wrap(appBuffer, cnetBuffer);
    if (result.getStatus() != Status.OK)
      throw new SSLException("unexpected status: " + result.getStatus());
    cnetBuffer.flip();
    appBuffer.clear();
    result = serverEngine.unwrap(cnetBuffer, appBuffer);
    if (result.getStatus() != Status.OK)
      throw new SSLException("unexpected status: " + result.getStatus());
    appBuffer.flip();
    String msg = cs.decode(appBuffer).toString();
    if (!msg.equals(TEST_MESSAGE))
      throw new SSLException("message decode failed");

    appBuffer.clear();
    enc.encode(CharBuffer.wrap(msg), appBuffer, true);
    appBuffer.flip();
    result = serverEngine.wrap(appBuffer, snetBuffer);
    if (result.getStatus() != Status.OK)
      throw new SSLException("unexpected status: " + result.getStatus());
    snetBuffer.flip();
View Full Code Here

   {
      try
      {
         CharBuffer cb = CharBuffer.wrap(source.toCharArray());
         Charset cs = Charset.forName(charSetName);
         CharsetEncoder cse = cs.newEncoder();
         ByteBuffer encoded = cse.encode(cb);
         return new String(encoded.array()).trim(); // Trim is very important!!!
      }
      catch (IllegalStateException e)
      {
         return null;
View Full Code Here

            Assert.assertEquals(oldPos, buffer.position());
        }
    }

    public void testPutString() throws Exception {
        CharsetEncoder encoder;
        ByteBuffer buf = ByteBuffer.allocate(16);
        encoder = Charset.forName("ISO-8859-1").newEncoder();

        buf.putString("ABC", encoder);
        Assert.assertEquals(3, buf.position());
View Full Code Here

TOP

Related Classes of java.nio.charset.CharsetEncoder

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.