Package java.nio.charset

Examples of java.nio.charset.CharsetEncoder


                IoBuffer buffer = IoBuffer.allocate(1);
                buffer.setAutoExpand(true);

                Charset charset = Charset.forName("UTF-8");

                CharsetEncoder encoder = charset.newEncoder();

                for (int i = 0; i < 5; i++) {
                    try {
                        buffer.putString("\u89d2", encoder);
                        buffer.putPrefixedString("\u89d2", encoder);
View Full Code Here


    public static void putDecoder(CharsetDecoder decoder) { decoders.put(decoder) ; }
   
    /** Allocate a CharsetEncoder, creating as necessary */
    public static CharsetEncoder allocEncoder()
    {
        CharsetEncoder enc = Chars.getEncoder();
        // Blocking finite Pool - does not happen.
        // Plain Pool (sync wrapped) - might - allocate an extra one.
        if ( enc == null )
            enc = Chars.createEncoder() ;
        enc
          .onMalformedInput(CodingErrorAction.REPLACE)
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
          .reset() ;
       
        return enc ;
View Full Code Here

     *            The {@code OutputStream} to write to.
     * @throws IOException
     *             If an error occurs writing the {@code Manifest}.
     */
    static void write(Manifest manifest, OutputStream out) throws IOException {
        CharsetEncoder encoder = ThreadLocalCache.utf8Encoder.get();
        ByteBuffer buffer = ThreadLocalCache.byteBuffer.get();

        String version = manifest.mainAttributes
                .getValue(Attributes.Name.MANIFEST_VERSION);
        if (version != null) {
View Full Code Here

    /** Encode a string into a ByteBuffer : on return position is the end of the encoding */
    public static int toByteBuffer(CharSequence s, ByteBuffer bb)
    {
        //BlockUTF8.fromChars(s, bb) ;
        // To be removed (Dec 2011)
        CharsetEncoder enc = Chars.allocEncoder();
        int x = toByteBuffer(s, bb, enc) ;
        Chars.deallocEncoder(enc) ;
        return x ;
    }
View Full Code Here

    String toString(byte[] ba) {
        return toString(ba, ba.length);
    }

    byte[] getBytes(String s) {
        CharsetEncoder ce = encoder().reset();
        char[] ca = s.toCharArray();
        int len = (int)(ca.length * ce.maxBytesPerChar());
        byte[] ba = new byte[len];
        if (len == 0)
            return ba;
        // UTF-8 only for now. Other ArrayDeocder only handles
        // CodingErrorAction.REPLACE mode.
        if (isUTF8 && ce instanceof ArrayEncoder) {
            int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
            if (blen == -1)    // malformed
                throw new IllegalArgumentException("MALFORMED");
            return Arrays.copyOf(ba, blen);
        }
        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());
        if (bb.position() == ba.length// defensive copy?
            return ba;
        else
View Full Code Here

  public static String convertToHTMLEntities(String s, boolean encodeTags) {
    return convertToHTMLEntities(s, null, encodeTags);
  }
 
  public static String convertToHTMLEntities(String s, String charset, boolean encodeTags) {
    CharsetEncoder ce = charset == null ? null : Charset.forName(charset).newEncoder();
    StringBuffer sb = new StringBuffer(s.length());
   
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      int n = ((int) c) & 0xFFFF;
     
      if (n > 127) {
        if (ce != null && ce.canEncode(c)) {
          sb.append(c);
        } else {
          String ent = NUMBER_TO_ENTITY.getProperty(Integer.toString(c));
         
          if (ent == null) {
View Full Code Here

    public String getCharsetName() {
        return charset.name();
    }

    private CharsetEncoder getEncoder() {
        CharsetEncoder encoder = (CharsetEncoder) getReference(encoderCache);
        if (encoder == null) {
            encoder = charset.newEncoder().onMalformedInput(
                    CodingErrorAction.REPLACE).onUnmappableCharacter(
                    CodingErrorAction.REPLACE);
            setReference(encoderCache, encoder);
View Full Code Here

     *            the char sequence
     * @return the encoded byte buffer
     */
    public ByteBuffer encode(CharSequence sequence) {
        CharBuffer buffer = CharBuffer.wrap(sequence);
        CharsetEncoder encoder = getEncoder();

        int n = 0;
        if (buffer.remaining() > 0) {
            n = (int) (buffer.remaining() * encoder.averageBytesPerChar());
            if (n == 0) {
                n = (int) (buffer.remaining() * encoder.maxBytesPerChar());
            }
        }
        ByteBuffer result = ByteBuffer.allocate(n); // judge result length
        if (n == 0) {
            return result;
        }

        encoder.reset();
        while (true) {
            CoderResult cr = buffer.hasRemaining() ? encoder.encode(buffer,
                    result, true) : encoder.flush(result);
            if (cr.isUnderflow()) {
                break;
            } else if (cr.isOverflow()) {
                n *= 2;
                result.flip();
View Full Code Here

     * @return the encoded byte buffer array
     * @throws IllegalArgumentException
     */
    public ByteBuffer[] encode(CharSequence sequence, int bufferCapcity) {
        CharBuffer buffer = CharBuffer.wrap(sequence);
        CharsetEncoder encoder = getEncoder();
        encoder.reset();

        Collection buffers = new ArrayList();
        while (true) {
            ByteBuffer out = ByteBuffer.allocate(bufferCapcity);
            CoderResult cr = encoder.encode(buffer, out, true);
            if (cr.isUnderflow()) {
                encoder.flush(out);
                out.flip();
                buffers.add(out);
                break;
            }
            if (cr.isOverflow()) {
View Full Code Here

     */
    public static BufferedWriter newBufferedWriter(Path path, Charset cs,
                                                   OpenOption... options)
        throws IOException
    {
        CharsetEncoder encoder = cs.newEncoder();
        Writer writer = new OutputStreamWriter(newOutputStream(path, options), encoder);
        return new BufferedWriter(writer);
    }
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.