src/share/classes/sun/nio/cs/ext/JISAutoDetect.java

Print this page




 100 
 101     public final static boolean canBeEUCKana(int mask1, int mask2) {
 102         return ((mask1 & EUCJP_KANA1_MASK) != 0)
 103             && ((mask2 & EUCJP_KANA2_MASK) != 0);
 104     }
 105 
 106     // A heuristic algorithm for guessing if EUC-decoded text really
 107     // might be Japanese text.  Better heuristics are possible...
 108     private static boolean looksLikeJapanese(CharBuffer cb) {
 109         int hiragana = 0;       // Fullwidth Hiragana
 110         int katakana = 0;       // Halfwidth Katakana
 111         while (cb.hasRemaining()) {
 112             char c = cb.get();
 113             if (0x3040 <= c && c <= 0x309f && ++hiragana > 1) return true;
 114             if (0xff65 <= c && c <= 0xff9f && ++katakana > 1) return true;
 115         }
 116         return false;
 117     }
 118 
 119     private static class Decoder extends CharsetDecoder {


 120 
 121         private final static String SJISName = getSJISName();
 122         private final static String EUCJPName = getEUCJPName();
 123         private DelegatableDecoder detectedDecoder = null;
 124 
 125         public Decoder(Charset cs) {
 126             super(cs, 0.5f, 1.0f);
 127         }
 128 
 129         private static boolean isPlainASCII(byte b) {
 130             return b >= 0 && b != 0x1b;
 131         }
 132 
 133         private static void copyLeadingASCII(ByteBuffer src, CharBuffer dst) {
 134             int start = src.position();
 135             int limit = start + Math.min(src.remaining(), dst.remaining());
 136             int p;
 137             byte b;
 138             for (p = start; p < limit && isPlainASCII(b = src.get(p)); p++)
 139                 dst.put((char)(b & 0xff));


 222             if (detectedDecoder != null)
 223                 return detectedDecoder.implFlush(out);
 224             else
 225                 return super.implFlush(out);
 226         }
 227 
 228         public boolean isAutoDetecting() {
 229             return true;
 230         }
 231 
 232         public boolean isCharsetDetected() {
 233             return detectedDecoder != null;
 234         }
 235 
 236         public Charset detectedCharset() {
 237             if (detectedDecoder == null)
 238                 throw new IllegalStateException("charset not yet detected");
 239             return ((CharsetDecoder) detectedDecoder).charset();
 240         }
 241 
 242         private static final String osName = AccessController.doPrivileged(
 243             (PrivilegedAction<String>) () -> System.getProperty("os.name"));
 244 
 245         /**
 246          * Returned Shift_JIS Charset name is OS dependent
 247          */
 248         private static String getSJISName() {
 249             if (osName.equals("Solaris") || osName.equals("SunOS"))
 250                 return("PCK");
 251             else if (osName.startsWith("Windows"))
 252                 return("windows-31J");
 253             else
 254                 return("Shift_JIS");
 255         }
 256 
 257         /**
 258          * Returned EUC-JP Charset name is OS dependent
 259          */
 260 
 261         private static String getEUCJPName() {
 262             if (osName.equals("Solaris") || osName.equals("SunOS"))
 263                 return("x-eucjp-open");




 100 
 101     public final static boolean canBeEUCKana(int mask1, int mask2) {
 102         return ((mask1 & EUCJP_KANA1_MASK) != 0)
 103             && ((mask2 & EUCJP_KANA2_MASK) != 0);
 104     }
 105 
 106     // A heuristic algorithm for guessing if EUC-decoded text really
 107     // might be Japanese text.  Better heuristics are possible...
 108     private static boolean looksLikeJapanese(CharBuffer cb) {
 109         int hiragana = 0;       // Fullwidth Hiragana
 110         int katakana = 0;       // Halfwidth Katakana
 111         while (cb.hasRemaining()) {
 112             char c = cb.get();
 113             if (0x3040 <= c && c <= 0x309f && ++hiragana > 1) return true;
 114             if (0xff65 <= c && c <= 0xff9f && ++katakana > 1) return true;
 115         }
 116         return false;
 117     }
 118 
 119     private static class Decoder extends CharsetDecoder {
 120         private final static String osName = AccessController.doPrivileged(
 121             (PrivilegedAction<String>) () -> System.getProperty("os.name"));
 122 
 123         private final static String SJISName = getSJISName();
 124         private final static String EUCJPName = getEUCJPName();
 125         private DelegatableDecoder detectedDecoder = null;
 126 
 127         public Decoder(Charset cs) {
 128             super(cs, 0.5f, 1.0f);
 129         }
 130 
 131         private static boolean isPlainASCII(byte b) {
 132             return b >= 0 && b != 0x1b;
 133         }
 134 
 135         private static void copyLeadingASCII(ByteBuffer src, CharBuffer dst) {
 136             int start = src.position();
 137             int limit = start + Math.min(src.remaining(), dst.remaining());
 138             int p;
 139             byte b;
 140             for (p = start; p < limit && isPlainASCII(b = src.get(p)); p++)
 141                 dst.put((char)(b & 0xff));


 224             if (detectedDecoder != null)
 225                 return detectedDecoder.implFlush(out);
 226             else
 227                 return super.implFlush(out);
 228         }
 229 
 230         public boolean isAutoDetecting() {
 231             return true;
 232         }
 233 
 234         public boolean isCharsetDetected() {
 235             return detectedDecoder != null;
 236         }
 237 
 238         public Charset detectedCharset() {
 239             if (detectedDecoder == null)
 240                 throw new IllegalStateException("charset not yet detected");
 241             return ((CharsetDecoder) detectedDecoder).charset();
 242         }
 243 


 244 
 245         /**
 246          * Returned Shift_JIS Charset name is OS dependent
 247          */
 248         private static String getSJISName() {
 249             if (osName.equals("Solaris") || osName.equals("SunOS"))
 250                 return("PCK");
 251             else if (osName.startsWith("Windows"))
 252                 return("windows-31J");
 253             else
 254                 return("Shift_JIS");
 255         }
 256 
 257         /**
 258          * Returned EUC-JP Charset name is OS dependent
 259          */
 260 
 261         private static String getEUCJPName() {
 262             if (osName.equals("Solaris") || osName.equals("SunOS"))
 263                 return("x-eucjp-open");