import java.io.*; import java.util.*; import java.lang.Long; import java.nio.channels.FileChannel; public class TargheReaderExt implements DataReaderTarghe { private File file; private ArrayList keys; private Map data_pool; private Map occurencies; public TargheReaderExt(File file){ this.file = file; File temp = null; data_pool = new HashMap(); occurencies = new HashMap(); keys = new ArrayList(); if(file.isDirectory()) { java.io.FilenameFilter select = new FileListFilter(null, "txt"); File sources[] = file.listFiles(select); FileOutputStream outf = null; FileInputStream inpf = null; try { temp = File.createTempFile("Classifier_save", null, new File("./")); outf = new FileOutputStream(temp, true); } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel out = outf.getChannel(); for(int i = 0; i < sources.length; i++) { try { inpf = new FileInputStream(sources[i]); } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel in = inpf.getChannel(); try { long bytesWritten = 0L; long bytesCount = in.size(); while (bytesWritten < bytesCount) { bytesWritten += in.transferTo(bytesWritten, bytesCount - bytesWritten, out); } inpf.close(); } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } } file = temp; } else if(!file.isFile()) { System.err.println(file + " : nome file non esistente..."); System.exit(1); } else { String name = file.getName(); if(!name.endsWith(".txt")) { System.err.println(file.getName() + " : formato file non valido..."); System.exit(1); } } } public void Load(){ try{ String s; FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while ((s = br.readLine()) != null) { s = s.trim(); if (s.indexOf("/")!=-1 || s.indexOf("#")!=-1 ){ continue; } Long out; LongBoxExt in; Long tmplong[] = new Long[4]; // the 128bit TrainingSet input is composed by four 32bit number String tmpstring[] = new String[4]; StringTokenizer st = new StringTokenizer(s); for(int i=0; i < 4; i++) { tmplong[i] = new Long(st.nextToken()); tmpstring[i] = Long.toBinaryString(tmplong[i].longValue()); } long tmps[] = {Long.parseLong(tmpstring[0]+tmpstring[1]),Long.parseLong(tmpstring[2]+tmpstring[3])}; in = new LongBoxExt(tmps); out = new Long(st.nextToken()); keys.add(in); data_pool.put(in,out); Long tmp = (Long) occurencies.get(out); if (tmp == null) occurencies.put(out,new Long(1)); else occurencies.put(out, new Long(tmp.longValue() + 1)); } } catch (FileNotFoundException e){ e.printStackTrace(System.err); System.exit(-1); } catch (IOException e){ e.printStackTrace(System.err); System.exit(-1); } } public long getOccurencies(Long key){ return ((Long) occurencies.get(key)).longValue(); } public Map getMap(){ return data_pool; } public ArrayList getInputObjects(){ return keys; } public boolean isExt(){ return true; } }