alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (AWTCharset.java)

This example Java source code file (AWTCharset.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

awtcharset, bytebuffer, charbuffer, charset, charsetdecoder, charsetencoder, coderresult, encoder, error, nio, string

The AWTCharset.java Java example source code

/*
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.awt;

import java.nio.CharBuffer;
import java.nio.ByteBuffer;
import java.nio.charset.*;


//This class delegates all invokes to the charset "javaCs" if
//its subclasses do not provide their own en/decode solution.

public class AWTCharset extends Charset {
    protected Charset awtCs;
    protected Charset javaCs;

    public AWTCharset(String awtCsName, Charset javaCs) {
        super(awtCsName, null);
        this.javaCs = javaCs;
        this.awtCs = this;
    }

    public boolean contains(Charset cs) {
        if (javaCs == null) return false;
        return javaCs.contains(cs);
    }

    public CharsetEncoder newEncoder() {
        if (javaCs == null)
            throw new Error("Encoder is not supported by this Charset");
        return new Encoder(javaCs.newEncoder());
    }

    public CharsetDecoder newDecoder() {
        if (javaCs == null)
            throw new Error("Decoder is not supported by this Charset");
        return new Decoder(javaCs.newDecoder());
    }

    public class Encoder extends CharsetEncoder {
        protected CharsetEncoder enc;
        protected Encoder () {
            this(javaCs.newEncoder());
        }
        protected Encoder (CharsetEncoder enc) {
            super(awtCs,
                  enc.averageBytesPerChar(),
                  enc.maxBytesPerChar());
            this.enc = enc;
        }
        public boolean canEncode(char c) {
            return enc.canEncode(c);
        }
        public boolean canEncode(CharSequence cs) {
            return enc.canEncode(cs);
        }
        protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
            return enc.encode(src, dst, true);
        }
        protected CoderResult implFlush(ByteBuffer out) {
            return enc.flush(out);
        }
        protected void implReset() {
            enc.reset();
        }
        protected void implReplaceWith(byte[] newReplacement) {
            if (enc != null)
                enc.replaceWith(newReplacement);
        }
        protected void implOnMalformedInput(CodingErrorAction newAction) {
            enc.onMalformedInput(newAction);
        }
        protected void implOnUnmappableCharacter(CodingErrorAction newAction) {
            enc.onUnmappableCharacter(newAction);
        }
        public boolean isLegalReplacement(byte[] repl) {
            return true;
        }
    }

    public class Decoder extends CharsetDecoder {
        protected CharsetDecoder dec;
        private String nr;

        protected Decoder () {
            this(javaCs.newDecoder());
        }

        protected Decoder (CharsetDecoder dec) {
            super(awtCs,
                  dec.averageCharsPerByte(),
                  dec.maxCharsPerByte());
            this.dec = dec;
        }
        protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
            return dec.decode(src, dst, true);
        }
        ByteBuffer fbb = ByteBuffer.allocate(0);
        protected CoderResult implFlush(CharBuffer out) {
            dec.decode(fbb, out, true);
            return dec.flush(out);
        }
        protected void implReset() {
            dec.reset();
        }
        protected void implReplaceWith(String newReplacement) {
            if (dec != null)
                dec.replaceWith(newReplacement);
        }
        protected void implOnMalformedInput(CodingErrorAction newAction) {
            dec.onMalformedInput(newAction);
        }
        protected void implOnUnmappableCharacter(CodingErrorAction newAction) {
            dec.onUnmappableCharacter(newAction);
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java AWTCharset.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.