Reading anscii to utf...
if(b1<=127){}//U+007F
else if(b1<=2047){}//U+07FF
else if(b1<=65535){}//U+FFFF
else if(b1<=1114111){}//U+10FFFF
else{} //???
https://en.wikipedia.org/wiki/UTF-8
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package asciutfnotepadsolution;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
/**
*
* @author jimak
*/
public class AsciUtfNotepadSolution extends JPanel implements ActionListener,ChangeListener{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new AsciUtfNotepadSolution();
}
static Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
public JFrame njfr(JComponent jc){
JFrame out=new JFrame(jc.getClass().getSimpleName());
out.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
out.setBounds(scr.width/2,0,scr.width/2,scr.height/4);
out.getContentPane().add(jc);
out.setVisible(true);
return out;
}
JTabbedPane jtp=new JTabbedPane();
JPanel jpLeft=new JPanel(new BorderLayout());
JPanel jpRight=new JPanel(new BorderLayout());
JSplitPane jspl=new JSplitPane(0,jpLeft,jpRight);
JFrame jfr=null;
Ints2 ints=new Ints2();
JLabel jlUnicode=new JLabel("Unicode char : ");
JPanel jpUnicode=new JPanel(new BorderLayout());
JLabel jlMaps=new JLabel("possibly written as");
JPanel jpMaps=new JPanel(new BorderLayout());
JPanel jpUnicodeMaps=new JPanel(new GridLayout(2,1));
JTextArea jtaManual=new JTextArea("ÅÐÉÊÏÕÑÉÊÇ ÈÁÍÁÔÏÕ\nÊÕÑÉÁ ÈÁÍÁÔÏÕ");
JFileChooser jfch=new JFileChooser();
JButton jbTryDecodeFile=new JButton("decode from File");
JButton jbTryDecodeFromTextArea=new JButton("decodeFrom TextArea");
JPanel sp=new JPanel(new BorderLayout());
public AsciUtfNotepadSolution(){
super(new BorderLayout());
jpRight.add(BorderLayout.CENTER,jtp);
jspl.setResizeWeight(0.1);
add(BorderLayout.CENTER,jspl);
jpUnicode.add(BorderLayout.WEST,jlUnicode);
jpUnicode.add(BorderLayout.CENTER,ints.jspun);
jpUnicodeMaps.add(jpUnicode);
jpMaps.add(BorderLayout.WEST,jlMaps);
jpMaps.add(BorderLayout.CENTER,ints.jspwr);
jpUnicodeMaps.add(jpMaps);
sp.add(BorderLayout.WEST,jbTryDecodeFromTextArea);
sp.add(BorderLayout.EAST,jbTryDecodeFile);
jbTryDecodeFromTextArea.setEnabled(false);
jtaManual.setEnabled(false);
jpLeft.add(BorderLayout.WEST,new JScrollPane(jtaManual));
jpLeft.add(BorderLayout.CENTER,jpUnicodeMaps);
jpLeft.add(BorderLayout.SOUTH,sp);
jfr=njfr(this);
jfr.setTitle(""+Charset.defaultCharset()+"..");
jfr.setBounds(50,50,scr.width-100,scr.height-100);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbTryDecodeFile.addActionListener(this);
jbTryDecodeFromTextArea.addActionListener(this);
ints.jspun.addChangeListener(this);
ints.jspwr.addChangeListener(this);
}
static Vector createColumns(){
Vector out=new Vector();
out.add("index");
out.add("view");
out.add("unicode");
out.add("<127");
out.add("result");
out.add("repr");
out.add("reads(b1)");
out.add("bin_b1");
out.add("b2");
out.add("bin_b2");
out.add("b3");
out.add("bin_b3");
out.add("b4");
out.add("bin_b4");
return out;
}
void newTxtDecodingTaskFinished(Rows rows){
System.out.println("done creating swing");
rows.jtbl=new Jtl(rows,createColumns());
rows.jscr=new JScrollPane(rows.jtbl);
rows.jscr.getVerticalScrollBar().setUnitIncrement(100);
rows.jp.add(BorderLayout.CENTER,rows.jscr);
rows.jp.add(BorderLayout.NORTH,rows.jpn);
String tabname="from JTA";
if(rows.src!=null){
tabname=""+rows.src.getName();
}
jtp.addTab(tabname, rows.jspl);
rows.jta.setText(rows.sbuf.toString());
rows.jta.setCaretPosition(0);
}
public static final int []lastCodePoint={127,2047,65535,1114111};
void newTxtDecodeingTask(Rows rows){
if(rows.input!=null){
System.out.println("Decoding bytes vector length="+rows.input.size());
System.out.println("creating multi vectors for");
System.out.println("charOut,i,(<-)byteRead-b1, b1Bin, b2, b2Bin, b3, b3Bin, b4, b4Bin");
int i=0,l=rows.input.size();
int b1,b2,b3,b4;//bytes
int i1,i2,i3,i4;//trancated bytes
int unicodeFinally=0;
char unicode=0;
while(i<l){
Vector v=new Vector();
v.add(i+":i");
v.add("");//view[1]
v.add("");//unicodefinal[2]
v.add(0);//<127[3]
v.add("");//complex resuylt[4]
v.add("");//repr[5]
b1=rows.input.get(i);
v.add(b1);
v.add(Integer.toBinaryString(b1));
rows.add(v);
++i;
}
System.out.println("finished heavy job now omputing job(2nd pass)");
i=0;
int idif=1;
while(i<l){
Vector v=(Vector)rows.get(i);
b1=rows.input.get(i);
if(b1<=127){
v.setElementAt(b1, 3);//result
unicodeFinally=b1;
idif=1;
}
else if(b1<=2047){
if(i+1<l){
b2=rows.input.get(i+1);
v.add(b2);
v.add(Integer.toBinaryString(b2));
i1=b1&31;
i2=b2&63;
i1<<=6;
i1+=i2;
v.setElementAt(i1, 4);//result
unicodeFinally=rows.un+(i1-rows.wr);
String s1=Integer.toBinaryString(b1);
String s2=Integer.toBinaryString(b2);
String s="";
String st="";
try{
s+=s1.substring(0,s1.length()-5);
s+="(";
s+=s1.substring(s1.length()-5,s1.length());
s+=")+";
s+=s2.substring(0,s2.length()-6);
s+="(";
s+=s2.substring(s2.length()-6,s2.length());
s+=")";
v.setElementAt(s, 5);
}catch(Exception e){v.setElementAt("errSTr", 3);}
}
else{v.add("err_2");}
idif=2;
}
else if(b1<=65535){
idif=3;
}
else if(b1<=1114111){
idif=4;
}
else{
v.add("err_1");
idif=1;
}
unicode=(char)unicodeFinally;
v.setElementAt(unicodeFinally,2);
v.setElementAt(unicode,1);
rows.sbuf.append(unicode);
i+=idif;
}
newTxtDecodingTaskFinished(rows);
}
}
void newTxtDecodingTask(String text){
if(text!=null){
int l=text.length();
if(l>0){
Rows rows=new Rows(null,ints.un(),ints.wr());
System.out.println("textArea.len()="+l);
int i=0;
while(i<l){
int ch=(int)text.charAt(i);
int i1=ch%256;
int i0=ch>>8;
rows.input.add(i0);
rows.input.add(i1);
++i;
}
newTxtDecodeingTask(rows);
}else{JOptionPane.showMessageDialog(this, "You must have some text in the up left TextArea to decode it");}
}
}
void newTxtDecodingTask(File f){
if(f!=null){
System.out.println("Task:"+f);
jfr.setTitle("Reading :"+f.getName());
Rows rows=new Rows(f,ints.un(),ints.wr());
IOException iof=null;
boolean end=false;
int ri;
FileInputStream fin=null;
try{
fin=new FileInputStream(f);
ri=fin.read();
while(ri>-1){
rows.input.add(ri);
ri=fin.read();
}
end=true;
}catch(IOException io){iof=io;}
finally{
try{fin.close();System.out.println("closed");}catch(Exception e){}
}
if(iof!=null){
if(!end){
JOptionPane.showMessageDialog(this, iof);
}
}
else{
System.out.println("ok...we have read "+rows.input.size()+" bytes");
System.out.println(f.getName()+" has length = "+f.length());
jfr.setTitle(rows.input.size()+" bytes read ok from "+f.getName());
newTxtDecodeingTask(rows);
}
}
}
void jcUnicodeSetted(int unicodepoint){
jlUnicode.setText("Unicode char: "+((char)unicodepoint)+" = "+unicodepoint);
}
@Override
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
if(src==jbTryDecodeFile){
int answer=jfch.showOpenDialog(this);
if(answer==JFileChooser.APPROVE_OPTION){
newTxtDecodingTask(jfch.getSelectedFile());
}
}
else if(src==jbTryDecodeFromTextArea){
newTxtDecodingTask(jtaManual.getText());
}
}
@Override
public void stateChanged(ChangeEvent e) {
}
}
class Rows extends Vector<Vector> implements ChangeListener{
File src;
Vector<Integer>input;
int un,wr;
StringBuffer sbuf=new StringBuffer();
Ints2 unwr=new Ints2();
JPanel jpn=new JPanel(new BorderLayout());
Jtl jtbl=null;
JScrollPane jscr=null;
JPanel jp=new JPanel(new BorderLayout());
JTextArea jta=new JTextArea();
JScrollPane jscrjta=new JScrollPane(jta);
JSplitPane jspl=new JSplitPane(1,jp,jscrjta);
public Rows(File f,int unicodePoint,int writtenAs){
src=f;
input=new Vector();
un=unicodePoint;
wr=writtenAs;
jspl.setResizeWeight(0.8);
unwr.jspun.setValue(un);
unwr.jspwr.setValue(wr);
jpn.add(BorderLayout.WEST,unwr);
unwr.jspun.addChangeListener(this);
unwr.jspwr.addChangeListener(this);
}
void recompute(){
if(jtbl!=null){
//if(newPossibleWr>=0){
System.out.println("recomputing for un="+un+" , wr="+wr);
DefaultTableModel dtm=(DefaultTableModel)jtbl.getModel();
int i=0,l=size();
int unicodeFinally;
sbuf.delete(0, sbuf.length());
char unicode;
while(i<l){
Vector v=(Vector)get(i);
Object o2=v.get(4);
if(o2 instanceof Integer){
unicodeFinally=(Integer)o2;
unicodeFinally+=un;
unicodeFinally-=wr;
}
else{
unicodeFinally=(Integer)v.get(3);
}
unicode=(char)unicodeFinally;
sbuf.append(unicode);
v.setElementAt(unicodeFinally, 2);
v.setElementAt(unicode, 1);
++i;
}
jtbl.repaint();
jta.setText(sbuf.toString());
//}
}
}
@Override
public void stateChanged(ChangeEvent e) {
un=unwr.un();
wr=unwr.wr();
recompute();
}
}
class Jtl extends JTable{
public Jtl(Vector rowDate,Vector columnData){
DefaultTableModel ndtm=new DefaultTableModel(rowDate, columnData);
setModel(ndtm);
}
public Jtl(Vector rowsWithVectorsOrNot){
super();
if(rowsWithVectorsOrNot==null){
rowsWithVectorsOrNot=new Vector();
}
int i=0,l=rowsWithVectorsOrNot.size(), maxCols=0,vCols;
while(i<l){
Object ov=rowsWithVectorsOrNot.get(i);
Vector v=null;
if(ov!=null && (ov instanceof Vector)){
v=(Vector)ov;
}
else{
v=new Vector();
rowsWithVectorsOrNot.insertElementAt(v, i);
rowsWithVectorsOrNot.remove(i+1);
v.add(ov);
}
vCols=v.size();
if(vCols>maxCols){
maxCols=vCols;
}
++i;
}
System.out.println("maxCols found : "+maxCols+"...creating default columns");
Vector cs=new Vector();
i=0;
while(i<maxCols){
cs.add("c"+i);
++i;
}
DefaultTableModel ndtm=new DefaultTableModel(rowsWithVectorsOrNot, cs);
setModel(ndtm);
}
}
class Ints2 extends JPanel{
JSpinner jspun=new JSpinner(new SpinnerNumberModel(913,0,65555,1));
JSpinner jspwr=new JSpinner(new SpinnerNumberModel(193,0,65555,1));
Ints2(){
super(new BorderLayout());
add(BorderLayout.WEST,jspun);
add(BorderLayout.EAST,jspwr);
}
public int un(){
return (Integer)jspun.getValue();
}
public int wr(){
return (Integer)jspwr.getValue();
}
}
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου