Page MenuHomec4science

Reader.java
No OneTemporary

File Metadata

Created
Tue, Jan 7, 00:33

Reader.java

package io.fam;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import crypto.paillier.KeyGenerator;
import database.DBconnector_client;
import database.DBconnector_server;
public class Reader implements Runnable {
private String path;
private File input_f;
private BufferedReader br;
private DBconnector_client conn_mu;
private DBconnector_server conn_s;
private KeyGenerator kg;
private JTextPane tp;
private StyledDocument doc;
public Reader(String fam_file_path, JTextPane txp){
path = fam_file_path;
conn_mu = new DBconnector_client();
conn_s = new DBconnector_server();
tp = txp;
doc = tp.getStyledDocument();
}
@Override
public void run() {
try{
String[] pathCheck = path.split("\\.");
if(!pathCheck[pathCheck.length-1].equalsIgnoreCase("fam")) {
throw new IOException("Wrong Format for FAM File!");
}
SimpleAttributeSet attribute = new SimpleAttributeSet();
StyleConstants.setForeground(attribute, Color.BLACK);
try {
doc.insertString(doc.getLength(), "FAM file parsing, please wait..." + "\n\n", attribute);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
long start = System.currentTimeMillis();
input_f = new File(path);
br = new BufferedReader(new FileReader(input_f));
String newline;
while((newline = br.readLine()) != null){
String[] tokens = newline.split(" ");
int pat_id;
try{
pat_id = Integer.parseInt(tokens[0]);
}
catch(Exception e){
String[] subtokens = tokens[0].split("_");
pat_id = Integer.parseInt(subtokens[1]);
}
conn_mu.insertPatient(pat_id, null, null, null, new Date(System.currentTimeMillis()));
kg = new KeyGenerator();
kg.generateKeys();
String pbk = kg.getN().toString()+";"+kg.getG().toString()+";"+kg.getH();
String prk_mu = kg.getX1().toString();
String prk_s = kg.getX2().toString();
conn_s.insertKey(pat_id, pbk, true);
conn_s.insertKey(pat_id, prk_s, false);
conn_mu.insertKey(pat_id, pbk, true);
conn_mu.insertKey(pat_id, prk_mu, false);
}
long end = System.currentTimeMillis();
try {
doc.insertString(doc.getLength(), "FAM file parsing and crypto keys generation completed!" + "\n\n", attribute);
doc.insertString(doc.getLength(), (end-start) + " ms\n", attribute);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
SimpleAttributeSet error = new SimpleAttributeSet();
StyleConstants.setForeground(error, Color.RED);
try {
doc.insertString(doc.getLength(), e.getMessage() + "\n", error);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
SimpleAttributeSet error = new SimpleAttributeSet();
StyleConstants.setForeground(error, Color.RED);
try {
doc.insertString(doc.getLength(), "No Connection with the Server" + "\n", error);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
}

Event Timeline