Search in sources :

Example 1 with SpannableStringBuilder

use of jp.sblo.pandora.jota.text.SpannableStringBuilder in project Jota-Text-Editor-old by jiro-aqua.

the class Main method onFileLoaded.

@Override
public void onFileLoaded(SpannableStringBuilder result, String filename, String charset, int linebreak, int offset) {
    mTask = null;
    if (result != null) {
        mInstanceState.filename = filename;
        mInstanceState.charset = charset;
        mInstanceState.linebreak = linebreak;
        mSearchResult = null;
        SpannableStringBuilder ss = result;
        mEditor.setText(ss);
        mEditor.setChanged(false);
        SharedPreferences sp = getSharedPreferences(SettingsActivity.PREF_HISTORY, MODE_PRIVATE);
        String sel = sp.getString(filename, "-1,-1");
        if (offset != -1) {
            try {
                mEditor.setSelection(offset);
                mEditor.centerCursor();
            } catch (Exception e) {
                offset = -1;
            }
        }
        if (offset == -1) {
            int selStart = -1;
            int selEnd = -1;
            if (sel != null) {
                String[] sels = sel.split(",");
                if (sels.length >= 2) {
                    try {
                        selStart = Integer.parseInt(sels[0]);
                        selEnd = Integer.parseInt(sels[1]);
                        if (selStart >= 0 && selEnd >= 0) {
                            int len = mEditor.length();
                            if (selStart >= len) {
                                selStart = len - 1;
                            }
                            if (selEnd >= len) {
                                selEnd = len - 1;
                            }
                            mEditor.setSelection(selStart, selEnd);
                            mEditor.centerCursor();
                        }
                    } catch (Exception e) {
                        selStart = -1;
                        selEnd = -1;
                    }
                }
            }
        }
        mLine = -1;
        saveHistory();
        if (mBootSettings.viewerMode) {
            mEditor.showIme(false);
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) SpannableStringBuilder(jp.sblo.pandora.jota.text.SpannableStringBuilder) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 2 with SpannableStringBuilder

use of jp.sblo.pandora.jota.text.SpannableStringBuilder in project Jota-Text-Editor-old by jiro-aqua.

the class TextLoadTask method openFile.

protected SpannableStringBuilder openFile(InputStream input, String encode) {
    SpannableStringBuilder result = new SpannableStringBuilder();
    InputStream is;
    try {
        mCharset = "utf-8";
        mLinebreak = LineBreak.LF;
        is = new BufferedInputStream(input, 65536);
        is.mark(65536);
        // preread leading 64KB
        int nread;
        byte[] buff = new byte[64 * 1024];
        nread = is.read(buff);
        if (nread <= 0) {
            if (encode.length() != 0) {
                mCharset = encode;
            }
            return new SpannableStringBuilder("");
        }
        // Detect charset
        UniversalDetector detector;
        if (encode == null || encode.length() == 0) {
            try {
                detector = new UniversalDetector();
                detector.handleData(buff, 0, nread);
                detector.dataEnd();
                encode = detector.getCharset();
                detector.destroy();
            } catch (DetectorException e1) {
            }
        }
        is.reset();
        // detect linbreak code
        if (encode == null || encode.length() == 0) {
            encode = "utf-8";
        }
        Charset charset = Charset.forName(encode);
        byte[] cr = new byte[] { '\r' };
        byte[] lf = new byte[] { '\n' };
        if (charset != null) {
            ByteBuffer bb;
            bb = charset.encode("\r");
            cr = new byte[bb.limit()];
            bb.get(cr);
            bb = charset.encode("\n");
            lf = new byte[bb.limit()];
            bb.get(lf);
        }
        int linebreak = LineBreak.LF;
        if (cr.length == 1) {
            for (int i = 0; i < nread - 1; i++) {
                if (buff[i] == lf[0]) {
                    linebreak = LineBreak.LF;
                    break;
                } else if (buff[i] == cr[0]) {
                    if (buff[i + 1] == lf[0]) {
                        linebreak = LineBreak.CRLF;
                    } else {
                        linebreak = LineBreak.CR;
                    }
                    break;
                }
            }
        } else {
            // cr.length == 2 // we dont think in the case cr.length>2
            for (int i = 0; i < nread - 2; i += 2) {
                if (buff[i] == lf[0] && buff[i + 1] == lf[1]) {
                    linebreak = LineBreak.LF;
                    break;
                } else if (buff[i] == cr[0] && buff[i + 1] == cr[1]) {
                    if (buff[i + 2] == lf[0] && buff[i + 3] == lf[1]) {
                        linebreak = LineBreak.CRLF;
                    } else {
                        linebreak = LineBreak.CR;
                    }
                    break;
                }
            }
        }
        //            if ( encode != null ){
        //                Log.e( TAG , "CharSet="+encode+"Linebreak=" + new String[]{"CR","LF","CRLF"}[linebreak]);
        //            }else{
        //                Log.e( TAG , "CharSet="+"--"+"Linebreak=" + new String[]{"CR","LF","CRLF"}[linebreak]);
        //            }
        mCharset = encode;
        mLinebreak = linebreak;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(is, encode), 8192 * 2);
            int line = 0;
            String text;
            while ((text = br.readLine()) != null) {
                // remove BOM
                if (line == 0) {
                    if (text.length() > 0 && text.charAt(0) == 0xfeff) {
                        text = text.substring(1);
                    }
                }
                line++;
                if (line == mLine) {
                    mLineToChar = result.length();
                }
                result.append(text);
                result.append('\n');
            }
            br.close();
            is.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) DetectorException(org.mozilla.universalchardet.UniversalDetector.DetectorException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) UniversalDetector(org.mozilla.universalchardet.UniversalDetector) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) SpannableStringBuilder(jp.sblo.pandora.jota.text.SpannableStringBuilder)

Aggregations

SpannableStringBuilder (jp.sblo.pandora.jota.text.SpannableStringBuilder)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 SharedPreferences (android.content.SharedPreferences)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ByteBuffer (java.nio.ByteBuffer)1 Charset (java.nio.charset.Charset)1 UniversalDetector (org.mozilla.universalchardet.UniversalDetector)1 DetectorException (org.mozilla.universalchardet.UniversalDetector.DetectorException)1