src/share/classes/sun/nio/cs/ISO_8859_1.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 /*
  27  */
  28 
  29 package sun.nio.cs;
  30 
  31 import java.nio.ByteBuffer;
  32 import java.nio.CharBuffer;
  33 import java.nio.charset.Charset;
  34 import java.nio.charset.CharsetDecoder;
  35 import java.nio.charset.CharsetEncoder;
  36 import java.nio.charset.CoderResult;
  37 import java.nio.charset.CharacterCodingException;
  38 import java.nio.charset.MalformedInputException;
  39 import java.nio.charset.UnmappableCharacterException;
  40 
  41 
  42 class ISO_8859_1
  43     extends Charset
  44     implements HistoricallyNamedCharset
  45 {
  46 
  47     public ISO_8859_1() {
  48         super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1);
  49     }
  50 
  51     public String historicalName() {
  52         return "ISO8859_1";
  53     }
  54 
  55     public boolean contains(Charset cs) {
  56         return ((cs instanceof US_ASCII)
  57                 || (cs instanceof ISO_8859_1));
  58     }
  59 
  60     public CharsetDecoder newDecoder() {
  61         return new Decoder(this);
  62     }
  63 
  64     public CharsetEncoder newEncoder() {
  65         return new Encoder(this);
  66     }
  67 
  68     private static class Decoder extends CharsetDecoder {
  69 
  70         private Decoder(Charset cs) {
  71             super(cs, 1.0f, 1.0f);
  72         }
  73 
  74         private CoderResult decodeArrayLoop(ByteBuffer src,
  75                                             CharBuffer dst)
  76         {
  77             byte[] sa = src.array();
  78             int sp = src.arrayOffset() + src.position();
  79             int sl = src.arrayOffset() + src.limit();
  80             assert (sp <= sl);
  81             sp = (sp <= sl ? sp : sl);
  82             char[] da = dst.array();
  83             int dp = dst.arrayOffset() + dst.position();
  84             int dl = dst.arrayOffset() + dst.limit();
  85             assert (dp <= dl);
  86             dp = (dp <= dl ? dp : dl);
  87 
  88             try {
  89                 while (sp < sl) {


 110                     if (!dst.hasRemaining())
 111                         return CoderResult.OVERFLOW;
 112                     dst.put((char)(b & 0xff));
 113                     mark++;
 114                 }
 115                 return CoderResult.UNDERFLOW;
 116             } finally {
 117                 src.position(mark);
 118             }
 119         }
 120 
 121         protected CoderResult decodeLoop(ByteBuffer src,
 122                                          CharBuffer dst)
 123         {
 124             if (src.hasArray() && dst.hasArray())
 125                 return decodeArrayLoop(src, dst);
 126             else
 127                 return decodeBufferLoop(src, dst);
 128         }
 129 







 130     }

 131 
 132     private static class Encoder extends CharsetEncoder {
 133 
 134         private Encoder(Charset cs) {
 135             super(cs, 1.0f, 1.0f);
 136         }
 137 
 138         public boolean canEncode(char c) {
 139             return c <= '\u00FF';
 140         }
 141 




 142         private final Surrogate.Parser sgp = new Surrogate.Parser();
 143 
 144         private CoderResult encodeArrayLoop(CharBuffer src,
 145                                             ByteBuffer dst)
 146         {
 147             char[] sa = src.array();
 148             int sp = src.arrayOffset() + src.position();
 149             int sl = src.arrayOffset() + src.limit();
 150             assert (sp <= sl);
 151             sp = (sp <= sl ? sp : sl);
 152             byte[] da = dst.array();
 153             int dp = dst.arrayOffset() + dst.position();
 154             int dl = dst.arrayOffset() + dst.limit();
 155             assert (dp <= dl);
 156             dp = (dp <= dl ? dp : dl);
 157             try {
 158                 while (sp < sl) {
 159                     char c = sa[sp];
 160                     if (c <= '\u00FF') {
 161                         if (dp >= dl)


 191                     }
 192                     if (sgp.parse(c, src) < 0)
 193                         return sgp.error();
 194                     return sgp.unmappableResult();
 195                 }
 196                 return CoderResult.UNDERFLOW;
 197             } finally {
 198                 src.position(mark);
 199             }
 200         }
 201 
 202         protected CoderResult encodeLoop(CharBuffer src,
 203                                          ByteBuffer dst)
 204         {
 205             if (src.hasArray() && dst.hasArray())
 206                 return encodeArrayLoop(src, dst);
 207             else
 208                 return encodeBufferLoop(src, dst);
 209         }
 210 



 211     }























 212 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 



  26 package sun.nio.cs;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.nio.CharBuffer;
  30 import java.nio.charset.Charset;
  31 import java.nio.charset.CharsetDecoder;
  32 import java.nio.charset.CharsetEncoder;
  33 import java.nio.charset.CoderResult;
  34 import java.util.Arrays;


  35 

  36 class ISO_8859_1
  37     extends Charset
  38     implements HistoricallyNamedCharset
  39 {
  40 
  41     public ISO_8859_1() {
  42         super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1);
  43     }
  44 
  45     public String historicalName() {
  46         return "ISO8859_1";
  47     }
  48 
  49     public boolean contains(Charset cs) {
  50         return ((cs instanceof US_ASCII)
  51                 || (cs instanceof ISO_8859_1));
  52     }
  53 
  54     public CharsetDecoder newDecoder() {
  55         return new Decoder(this);
  56     }
  57 
  58     public CharsetEncoder newEncoder() {
  59         return new Encoder(this);
  60     }
  61 
  62     private static class Decoder extends CharsetDecoder
  63                                  implements ArrayDecoder {
  64         private Decoder(Charset cs) {
  65             super(cs, 1.0f, 1.0f);
  66         }
  67 
  68         private CoderResult decodeArrayLoop(ByteBuffer src,
  69                                             CharBuffer dst)
  70         {
  71             byte[] sa = src.array();
  72             int sp = src.arrayOffset() + src.position();
  73             int sl = src.arrayOffset() + src.limit();
  74             assert (sp <= sl);
  75             sp = (sp <= sl ? sp : sl);
  76             char[] da = dst.array();
  77             int dp = dst.arrayOffset() + dst.position();
  78             int dl = dst.arrayOffset() + dst.limit();
  79             assert (dp <= dl);
  80             dp = (dp <= dl ? dp : dl);
  81 
  82             try {
  83                 while (sp < sl) {


 104                     if (!dst.hasRemaining())
 105                         return CoderResult.OVERFLOW;
 106                     dst.put((char)(b & 0xff));
 107                     mark++;
 108                 }
 109                 return CoderResult.UNDERFLOW;
 110             } finally {
 111                 src.position(mark);
 112             }
 113         }
 114 
 115         protected CoderResult decodeLoop(ByteBuffer src,
 116                                          CharBuffer dst)
 117         {
 118             if (src.hasArray() && dst.hasArray())
 119                 return decodeArrayLoop(src, dst);
 120             else
 121                 return decodeBufferLoop(src, dst);
 122         }
 123 
 124         public int decode(byte[] src, int sp, int len, char[] dst) {
 125             if (len > dst.length)
 126                 len = dst.length;
 127             int dp = 0;
 128             while (dp < len)
 129                 dst[dp++] = (char)(src[sp++] & 0xff);
 130             return dp;
 131         }
 132     }
 133 
 134     private static class Encoder extends CharsetEncoder
 135                                  implements ArrayEncoder {
 136         private Encoder(Charset cs) {
 137             super(cs, 1.0f, 1.0f);
 138         }
 139 
 140         public boolean canEncode(char c) {
 141             return c <= '\u00FF';
 142         }
 143 
 144         public boolean isLegalReplacement(byte[] repl) {
 145             return (repl.length == 1);  // we accept any byte value
 146         }
 147 
 148         private final Surrogate.Parser sgp = new Surrogate.Parser();
 149 
 150         private CoderResult encodeArrayLoop(CharBuffer src,
 151                                             ByteBuffer dst)
 152         {
 153             char[] sa = src.array();
 154             int sp = src.arrayOffset() + src.position();
 155             int sl = src.arrayOffset() + src.limit();
 156             assert (sp <= sl);
 157             sp = (sp <= sl ? sp : sl);
 158             byte[] da = dst.array();
 159             int dp = dst.arrayOffset() + dst.position();
 160             int dl = dst.arrayOffset() + dst.limit();
 161             assert (dp <= dl);
 162             dp = (dp <= dl ? dp : dl);
 163             try {
 164                 while (sp < sl) {
 165                     char c = sa[sp];
 166                     if (c <= '\u00FF') {
 167                         if (dp >= dl)


 197                     }
 198                     if (sgp.parse(c, src) < 0)
 199                         return sgp.error();
 200                     return sgp.unmappableResult();
 201                 }
 202                 return CoderResult.UNDERFLOW;
 203             } finally {
 204                 src.position(mark);
 205             }
 206         }
 207 
 208         protected CoderResult encodeLoop(CharBuffer src,
 209                                          ByteBuffer dst)
 210         {
 211             if (src.hasArray() && dst.hasArray())
 212                 return encodeArrayLoop(src, dst);
 213             else
 214                 return encodeBufferLoop(src, dst);
 215         }
 216 
 217         private byte repl = (byte)'?';
 218         protected void implReplaceWith(byte[] newReplacement) {
 219             repl = newReplacement[0];
 220         }
 221 
 222         public int encode(char[] src, int sp, int len, byte[] dst) {
 223             int dp = 0;
 224             int sl = sp + Math.min(len, dst.length);
 225             while (sp < sl) {
 226                 char c = src[sp++];
 227                 if (c <= '\u00FF') {
 228                     dst[dp++] = (byte)c;
 229                     continue;
 230                 }
 231                 if (Surrogate.isHigh(c) && sp < sl &&
 232                     Surrogate.isLow(src[sp])) {
 233                     if (len > dst.length) {
 234                         sl++;
 235                         len--;
 236                     }
 237                     sp++;
 238                 }
 239                 dst[dp++] = repl;
 240             }
 241             return dp;
 242         }
 243     }
 244 }