001    // Copyright 2006-2007 Regents of the University of California.  May be used 
002    // under the terms of the revised BSD license.  See LICENSING for details.
003    package org.joe_e.charset;
004    
005    import java.nio.charset.Charset;
006    import java.nio.ByteBuffer;
007    import java.io.InputStream;
008    import java.io.InputStreamReader;
009    import java.io.OutputStream;
010    import java.io.OutputStreamWriter;
011    import java.io.Reader;
012    import java.io.Writer;
013    
014    /**
015     * ASCII I/O.
016     */
017    public final class ASCII {
018        private static final Charset charset = Charset.forName("US-ASCII");
019        
020        private ASCII() {}
021    
022        /**
023         * Encodes a string in US-ASCII.
024         * @param text  The text to encode.
025         * @return The ASCII bytes.
026         */
027        static public byte[] encode(final String text) {
028            final ByteBuffer bytes = charset.encode(text);
029            final int len = bytes.limit();
030            final byte[] v = bytes.array();
031            if (len == v.length) { return v; }
032            final byte[] r = new byte[len];
033            System.arraycopy(v, bytes.arrayOffset(), r, 0, len);
034            return r;
035        }
036        
037        /**
038         * Decodes a US-ASCII string. Each byte not corresponding to a US-ASCII
039         * character decodes to the Unicode replacement character U+FFFD.  This
040         * method is equivalent to <code>decode(buffer, 0, buffer.length)</code>.
041         * @param buffer    the ASCII-encoded string to decode
042         * @return The corresponding string
043         * @throws java.lang.IndexOutOfBoundsException
044         */
045        static public String decode(byte[] buffer) {
046            return decode(buffer, 0, buffer.length);
047        }
048        
049        /**
050         * Decodes a US-ASCII string. Each byte not corresponding to a US-ASCII
051         * character decodes to the Unicode replacement character U+FFFD.
052         * @param buffer    the ASCII-encoded string to decode
053         * @param off       where to start decoding
054         * @param len       how many bytes to decode
055         * @return  the corresponding string
056         * @throws java.lang.IndexOutOfBoundsException
057         */
058        static public String decode(byte[] buffer, int off, int len) {
059            return charset.decode(ByteBuffer.wrap(buffer, off, len)).toString();
060        }
061            
062        /**
063         * Constructs an ASCII reader.
064         * @param in    The binary input stream
065         * @return  the ASCII character reader.
066         */
067        static public Reader input(final InputStream in) {
068            return new InputStreamReader(in, charset);
069        }
070    
071        /**
072         * Constructs an ASCII writer.
073         * @param out the output stream.
074         */
075        static public Writer output(final OutputStream out) {
076            return new OutputStreamWriter(out, charset);
077        }
078    }