jdk/src/share/classes/sun/nio/cs/ISO_8859_1.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2294 4259115772f7
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.CharacterCodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.MalformedInputException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.charset.UnmappableCharacterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class ISO_8859_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public ISO_8859_1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return "ISO8859_1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return ((cs instanceof US_ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                || (cs instanceof ISO_8859_1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return new Encoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static class Decoder extends CharsetDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        private Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    byte b = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    if (dp >= dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    da[dp++] = (char)(b & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    byte b = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    dst.put((char)(b & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static class Encoder extends CharsetEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        private Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return c <= '\u00FF';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    if (c <= '\u00FF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        if (dp >= dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        da[dp++] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    if (sgp.parse(c, sa, sp, sl) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    char c = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    if (c <= '\u00FF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        dst.put((byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    if (sgp.parse(c, src) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}