FileFactory.java
上传用户:mytimer
上传日期:2019-10-15
资源大小:1979k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import java.io.InputStream;
  2. public class FileFactory {
  3. public String st, ngoaiLe;
  4. public FileFactory(String string) {
  5.             st = string;
  6. }
  7.         public final String readUnicodeFileUTF8() {
  8.             String ndEbook = null;
  9.             StringBuffer sb = new StringBuffer(256);
  10.             try {
  11.                int[] surrogatePair = new int[2];
  12.                InputStream is = this.getClass().getResourceAsStream(st);
  13.                int val = 0;
  14.                int unicharCount = 0;
  15.                while ((val = readNextCharFromStreamUTF8(is))!=-1) {
  16.                    unicharCount++;
  17.                    if (val <= 0xFFFF) {
  18.                        // if first value is the Byte Order Mark (BOM), do not add
  19.                        if (! (unicharCount == 1 && val == 0xFEFF)) {
  20.                            sb.append((char)val);
  21.                        }
  22.                    } else {
  23.                        supplementCodePointToSurrogatePair(val, surrogatePair);
  24.                        sb.append((char)surrogatePair[0]);
  25.                        sb.append((char)surrogatePair[1]);
  26.                    }
  27.                }
  28.                ndEbook = new String(sb);
  29.                is.close();
  30.            } catch (Exception e) {
  31.     //        e.printStackTrace();
  32.                ndEbook = "Kh鬾g ??c ???c file n鄖!";
  33.            }
  34.            return ndEbook;
  35.    }
  36.    public final static int readNextCharFromStreamUTF8(InputStream is) {
  37.        int c = -1;
  38.        if (is==null) return c;
  39.        boolean complete = false;
  40.        try {
  41.            int byteVal;
  42.            int expecting=0;
  43.            int composedVal=0;
  44.            while (!complete && (byteVal = is.read()) != -1) {
  45.                if (expecting > 0 && (byteVal & 0xC0) == 0x80) {  /* 10xxxxxx */
  46.                    expecting--;
  47.                    composedVal = composedVal | ((byteVal & 0x3F) << (expecting*6));
  48.                    if (expecting == 0) {
  49.                        c = composedVal;
  50.                        complete = true;
  51.                        //System.out.println("appending: U+" + Integer.toHexString(composedVal) );
  52.                    }
  53.                } else {
  54.                    composedVal = 0;
  55.                    expecting = 0;
  56.                    if ((byteVal & 0x80) == 0) {    /* 0xxxxxxx */
  57.                        // one byte character, no extending byte expected
  58.                        c = byteVal;
  59.                        complete = true;
  60.                        //System.out.println("appending: U+" + Integer.toHexString(byteVal) );
  61.                    } else if ((byteVal & 0xE0) == 0xC0) {   /* 110xxxxx */
  62.                        expecting = 1;  // expecting 1 extending byte
  63.                        composedVal = ((byteVal & 0x1F) << 6);
  64.                    } else if ((byteVal & 0xF0) == 0xE0) {   /* 1110xxxx */
  65.                        expecting = 2;  // expecting 2 extending bytes
  66.                        composedVal = ((byteVal & 0x0F) << 12);
  67.                    } else if ((byteVal & 0xF8) == 0xF0) {   /* 11110xxx */
  68.                        expecting = 3;  // expecting 3 extending bytes
  69.                        composedVal = ((byteVal & 0x07) << 18);
  70.                    } else {
  71.                        // non conformant utf-8, ignore or catch error
  72.                    }
  73.                }
  74.            }
  75.        } catch (Exception e) {
  76.            System.out.println(e.toString());
  77.        }
  78.        return c;
  79.    }
  80.    public final static void supplementCodePointToSurrogatePair(int codePoint, int[] surrogatePair) {
  81.        int high4 = ((codePoint >> 16) & 0x1F) - 1;
  82.        int mid6 = ((codePoint >> 10) & 0x3F);
  83.        int low10 = codePoint & 0x3FF;
  84.        surrogatePair[0] = (0xD800 | (high4 << 6) | (mid6));
  85.        surrogatePair[1] = (0xDC00 | (low10));
  86.    }
  87. ////////////////////
  88. }