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

Print this page




  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.nio.charset.CharacterCodingException;
  35 import java.nio.charset.MalformedInputException;
  36 import java.nio.charset.UnmappableCharacterException;
  37 
  38 
  39 public class US_ASCII
  40     extends Charset
  41     implements HistoricallyNamedCharset
  42 {
  43 
  44     public US_ASCII() {
  45         super("US-ASCII", StandardCharsets.aliases_US_ASCII);
  46     }
  47 
  48     public String historicalName() {
  49         return "ASCII";
  50     }
  51 
  52     public boolean contains(Charset cs) {
  53         return (cs instanceof US_ASCII);
  54     }
  55 
  56     public CharsetDecoder newDecoder() {
  57         return new Decoder(this);
  58     }
  59 
  60     public CharsetEncoder newEncoder() {
  61         return new Encoder(this);
  62     }
  63 
  64     private static class Decoder extends CharsetDecoder {

  65 
  66         private Decoder(Charset cs) {
  67             super(cs, 1.0f, 1.0f);
  68         }
  69 
  70         private CoderResult decodeArrayLoop(ByteBuffer src,
  71                                             CharBuffer dst)
  72         {
  73             byte[] sa = src.array();
  74             int sp = src.arrayOffset() + src.position();
  75             int sl = src.arrayOffset() + src.limit();
  76             assert (sp <= sl);
  77             sp = (sp <= sl ? sp : sl);
  78             char[] da = dst.array();
  79             int dp = dst.arrayOffset() + dst.position();
  80             int dl = dst.arrayOffset() + dst.limit();
  81             assert (dp <= dl);
  82             dp = (dp <= dl ? dp : dl);
  83 
  84             try {


 114                         mark++;
 115                         continue;
 116                     }
 117                     return CoderResult.malformedForLength(1);
 118                 }
 119                 return CoderResult.UNDERFLOW;
 120             } finally {
 121                 src.position(mark);
 122             }
 123         }
 124 
 125         protected CoderResult decodeLoop(ByteBuffer src,
 126                                          CharBuffer dst)
 127         {
 128             if (src.hasArray() && dst.hasArray())
 129                 return decodeArrayLoop(src, dst);
 130             else
 131                 return decodeBufferLoop(src, dst);
 132         }
 133 



 134     }
 135 
 136     private static class Encoder extends CharsetEncoder {












 137 



 138         private Encoder(Charset cs) {
 139             super(cs, 1.0f, 1.0f);
 140         }
 141 
 142         public boolean canEncode(char c) {
 143             return c < 0x80;
 144         }
 145 
 146         private final Surrogate.Parser sgp = new Surrogate.Parser();


 147 

 148         private CoderResult encodeArrayLoop(CharBuffer src,
 149                                             ByteBuffer dst)
 150         {
 151             char[] sa = src.array();
 152             int sp = src.arrayOffset() + src.position();
 153             int sl = src.arrayOffset() + src.limit();
 154             assert (sp <= sl);
 155             sp = (sp <= sl ? sp : sl);
 156             byte[] da = dst.array();
 157             int dp = dst.arrayOffset() + dst.position();
 158             int dl = dst.arrayOffset() + dst.limit();
 159             assert (dp <= dl);
 160             dp = (dp <= dl ? dp : dl);
 161 
 162             try {
 163                 while (sp < sl) {
 164                     char c = sa[sp];
 165                     if (c < 0x80) {
 166                         if (dp >= dl)
 167                             return CoderResult.OVERFLOW;


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



 216     }
 217 























 218 }


  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 public class US_ASCII
  37     extends Charset
  38     implements HistoricallyNamedCharset
  39 {
  40 
  41     public US_ASCII() {
  42         super("US-ASCII", StandardCharsets.aliases_US_ASCII);
  43     }
  44 
  45     public String historicalName() {
  46         return "ASCII";
  47     }
  48 
  49     public boolean contains(Charset cs) {
  50         return (cs instanceof US_ASCII);
  51     }
  52 
  53     public CharsetDecoder newDecoder() {
  54         return new Decoder(this);
  55     }
  56 
  57     public CharsetEncoder newEncoder() {
  58         return new Encoder(this);
  59     }
  60 
  61     private static class Decoder extends CharsetDecoder
  62                                  implements ArrayDecoder {
  63 
  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 {


 112                         mark++;
 113                         continue;
 114                     }
 115                     return CoderResult.malformedForLength(1);
 116                 }
 117                 return CoderResult.UNDERFLOW;
 118             } finally {
 119                 src.position(mark);
 120             }
 121         }
 122 
 123         protected CoderResult decodeLoop(ByteBuffer src,
 124                                          CharBuffer dst)
 125         {
 126             if (src.hasArray() && dst.hasArray())
 127                 return decodeArrayLoop(src, dst);
 128             else
 129                 return decodeBufferLoop(src, dst);
 130         }
 131 
 132         private char repl = '\uFFFD';
 133         protected void implReplaceWith(String newReplacement) {
 134             repl = newReplacement.charAt(0);
 135         }
 136 
 137         public int decode(byte[] src, int sp, int len, char[] dst) {
 138             int dp = 0;
 139             len = Math.min(len, dst.length);
 140             while (dp < len) {
 141                 byte b = src[sp++];
 142                 if (b >= 0)
 143                     dst[dp++] = (char)b;
 144                 else
 145                     dst[dp++] = repl;
 146             }
 147             return dp;
 148         }
 149     }
 150 
 151     private static class Encoder extends CharsetEncoder
 152                                  implements ArrayEncoder {
 153 
 154         private Encoder(Charset cs) {
 155             super(cs, 1.0f, 1.0f);
 156         }
 157 
 158         public boolean canEncode(char c) {
 159             return c < 0x80;
 160         }
 161 
 162         public boolean isLegalReplacement(byte[] repl) {
 163             return (repl.length == 1 && repl[0] >= 0);
 164         }
 165 
 166         private final Surrogate.Parser sgp = new Surrogate.Parser();
 167         private CoderResult encodeArrayLoop(CharBuffer src,
 168                                             ByteBuffer dst)
 169         {
 170             char[] sa = src.array();
 171             int sp = src.arrayOffset() + src.position();
 172             int sl = src.arrayOffset() + src.limit();
 173             assert (sp <= sl);
 174             sp = (sp <= sl ? sp : sl);
 175             byte[] da = dst.array();
 176             int dp = dst.arrayOffset() + dst.position();
 177             int dl = dst.arrayOffset() + dst.limit();
 178             assert (dp <= dl);
 179             dp = (dp <= dl ? dp : dl);
 180 
 181             try {
 182                 while (sp < sl) {
 183                     char c = sa[sp];
 184                     if (c < 0x80) {
 185                         if (dp >= dl)
 186                             return CoderResult.OVERFLOW;


 215                     }
 216                     if (sgp.parse(c, src) < 0)
 217                         return sgp.error();
 218                     return sgp.unmappableResult();
 219                 }
 220                 return CoderResult.UNDERFLOW;
 221             } finally {
 222                 src.position(mark);
 223             }
 224         }
 225 
 226         protected CoderResult encodeLoop(CharBuffer src,
 227                                          ByteBuffer dst)
 228         {
 229             if (src.hasArray() && dst.hasArray())
 230                 return encodeArrayLoop(src, dst);
 231             else
 232                 return encodeBufferLoop(src, dst);
 233         }
 234 
 235         private byte repl = (byte)'?';
 236         protected void implReplaceWith(byte[] newReplacement) {
 237             repl = newReplacement[0];
 238         }
 239 
 240         public int encode(char[] src, int sp, int len, byte[] dst) {
 241             int dp = 0;
 242             int sl = sp + Math.min(len, dst.length);
 243             while (sp < sl) {
 244                 char c = src[sp++];
 245                 if (c < 0x80) {
 246                     dst[dp++] = (byte)c;
 247                     continue;
 248                 }
 249                 if (Surrogate.isHigh(c) && sp < sl &&
 250                     Surrogate.isLow(src[sp])) {
 251                     if (len > dst.length) {
 252                         sl++;
 253                         len--;
 254                     }
 255                     sp++;
 256                 }
 257                 dst[dp++] = repl;
 258             }
 259             return dp;
 260         }
 261     }
 262 
 263 }