Home | History | Annotate | Download | only in charset
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /**
      4  *******************************************************************************
      5  * Copyright (C) 2006-2011, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 
     10 package com.ibm.icu.charset;
     11 
     12 import java.nio.ByteBuffer;
     13 import java.nio.CharBuffer;
     14 import java.nio.charset.CharsetDecoder;
     15 import java.nio.charset.CharsetEncoder;
     16 import java.nio.charset.CoderResult;
     17 
     18 import com.ibm.icu.text.UnicodeSet;
     19 
     20 class Charset88591 extends CharsetASCII {
     21     public Charset88591(String icuCanonicalName, String javaCanonicalName, String[] aliases) {
     22         super(icuCanonicalName, javaCanonicalName, aliases);
     23     }
     24 
     25     class CharsetDecoder88591 extends CharsetDecoderASCII {
     26         public CharsetDecoder88591(CharsetICU cs) {
     27             super(cs);
     28         }
     29 
     30         @Override
     31         protected CoderResult decodeLoopCoreOptimized(ByteBuffer source, CharBuffer target,
     32                 byte[] sourceArray, char[] targetArray, int oldSource, int offset, int limit) {
     33 
     34             /*
     35              * perform 88591 conversion from the source array to the target array. no range check is
     36              * necessary.
     37              */
     38             for (int i = oldSource; i < limit; i++)
     39                 targetArray[i + offset] = (char) (sourceArray[i] & 0xff);
     40 
     41             return null;
     42         }
     43 
     44         @Override
     45         protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
     46             byte ch;
     47             /*
     48              * perform 88591 conversion from the source buffer to the target buffer. no range check
     49              * is necessary.
     50              */
     51             while (source.hasRemaining()) {
     52                 ch = source.get();
     53                 if (target.hasRemaining()) {
     54                     target.put((char) (ch & 0xff));
     55                 } else {
     56                     return CoderResult.OVERFLOW;
     57                 }
     58             }
     59 
     60             return CoderResult.UNDERFLOW;
     61         }
     62     }
     63 
     64     class CharsetEncoder88591 extends CharsetEncoderASCII {
     65         public CharsetEncoder88591(CharsetICU cs) {
     66             super(cs);
     67         }
     68 
     69         @Override
     70         protected final CoderResult encodeLoopCoreOptimized(CharBuffer source, ByteBuffer target,
     71                 char[] sourceArray, byte[] targetArray, int oldSource, int offset, int limit,
     72                 boolean flush) {
     73             int i, ch = 0;
     74 
     75             /*
     76              * perform 88591 conversion from the source array to the target array, making sure each
     77              * char in the source is within the correct range
     78              */
     79             for (i = oldSource; i < limit; i++) {
     80                 ch = sourceArray[i];
     81                 if ((ch & 0xff00) == 0) {
     82                     targetArray[i + offset] = (byte) ch;
     83                 } else {
     84                     break;
     85                 }
     86             }
     87 
     88             /*
     89              * if some byte was not in the correct range, we need to deal with this byte by calling
     90              * encodeMalformedOrUnmappable and move the source and target positions to reflect the
     91              * early termination of the loop
     92              */
     93             if ((ch & 0xff00) != 0) {
     94                 source.position((i + 1) - source.arrayOffset());
     95                 target.position(i + offset);
     96                 return encodeMalformedOrUnmappable(source, ch, flush);
     97             } else
     98                 return null;
     99         }
    100 
    101         @Override
    102         protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
    103             int ch;
    104 
    105             /*
    106              * perform 88591 conversion from the source buffer to the target buffer, making sure
    107              * each char in the source is within the correct range
    108              */
    109 
    110             while (source.hasRemaining()) {
    111                 ch = source.get();
    112                 if ((ch & 0xff00) == 0) {
    113                     if (target.hasRemaining()) {
    114                         target.put((byte) ch);
    115                     } else {
    116                         return CoderResult.OVERFLOW;
    117                     }
    118                 } else {
    119                     /*
    120                      * if we reach here, it's because a character was not in the correct range, and we need
    121                      * to deal with this by calling encodeMalformedOrUnmappable.
    122                      */
    123                     return encodeMalformedOrUnmappable(source, ch, flush);
    124                 }
    125             }
    126 
    127             return CoderResult.UNDERFLOW;
    128         }
    129 
    130     }
    131 
    132     @Override
    133     public CharsetDecoder newDecoder() {
    134         return new CharsetDecoder88591(this);
    135     }
    136 
    137     @Override
    138     public CharsetEncoder newEncoder() {
    139         return new CharsetEncoder88591(this);
    140     }
    141 
    142     @Override
    143     void getUnicodeSetImpl( UnicodeSet setFillIn, int which){
    144         setFillIn.add(0,0xff);
    145      }
    146 }
    147