Java tutorial
/* * 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 org.alan.graph.binaryparsermaven.util; import au.com.bytecode.opencsv.CSVWriter; import java.awt.Point; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import org.alan.graph.binaryparsermaven.App; import static org.alan.graph.binaryparsermaven.App.cw; import org.alan.graph.binaryparsermaven.dao.Points; import org.alan.graph.binaryparsermaven.inter.SchoolInterface; import org.apache.commons.io.IOUtils; /** * * @author alan */ public class ParseZNOFiles { public Date newDat; public void run(String dir, Date dat, SchoolInterface s) throws Exception { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(0); cal.set(2013, 1, 1); newDat = cal.getTime(); for (File f : new File(dir).listFiles()) { System.out.println(f.getName()); parseFile(f, dir, s, dat); } } private void parseFile(File f, String dir, SchoolInterface s, Date dat) throws Exception { try { //rs /* FileWriter fileWriter = new FileWriter(dir + f.getName() + ".csv"); CSVWriter cw = new CSVWriter(fileWriter); */ //rs int schools = 1; FileInputStream fis = new FileInputStream(f); byte[] arr = IOUtils.toByteArray(fis); byte[] split = new byte[] { 0x00, 0x01, 0x0a, 0x00, 0x00, 0x00 }; int rend = 0; int start = 0; int end = 0; byte[] data; int i1 = 0; boolean flag = true; while (flag) { schools++; //System.out.println(schools); start = indexOf(arr, split, end); end = indexOf(arr, split, start + split.length); rend = indexOf(arr, split, end + split.length - 1); if (end < start) { end = arr.length - 1; flag = false; } if (Math.abs(rend - end) <= 6 && rend > 0) { end = rend; } data = new byte[(end - start) + 1]; i1 = 0; for (int i = start; i < end; i++) { data[i1++] = arr[i]; } String[] parsedData = parseData(data, split.length, dat); /* if (s == null) { cw.writeNext(parsedData); } else {*/ Points p = new Points(); p.setValueType(f.getName()); p.setKoatyy(parsedData[0]); p.setParentKoatyy(parsedData[1]); CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); utf8Decoder.onMalformedInput(CodingErrorAction.IGNORE); utf8Decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); ByteBuffer bytes = ByteBuffer.wrap(parsedData[2].getBytes()); CharBuffer parsed = utf8Decoder.decode(bytes); String repl = String.valueOf(parsed.array()); repl = repl.replaceAll("\\x00", ""); p.setSchoolName(repl); p.setSchoolType(Integer.valueOf(parsedData[3])); p.setValue1(Double.valueOf(parsedData[4])); p.setValue2(Double.valueOf(parsedData[5])); p.setValue3(Double.valueOf(parsedData[6])); p.setValue4(Double.valueOf(parsedData[7])); p.setValue5(Double.valueOf(parsedData[8])); p.setValue6(Double.valueOf(parsedData[9])); p.setValue7(Double.valueOf(parsedData[10])); p.setValue8(Double.valueOf(parsedData[11])); p.setValue9(Double.valueOf(parsedData[12])); p.setValue10(Double.valueOf(parsedData[13])); p.setPupils(Integer.valueOf(parsedData[14])); p.setDat(dat); s.save(p); //} } // System.out.println(data); fis.close(); //csv /* fileWriter.close(); cw.close(); */ } catch (IOException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); } } private String[] parseData(byte[] data, int spiltLen, Date dat) throws UnsupportedEncodingException, Exception { String[] csv = new String[15]; //Koatyy byte[] innerSplit = new byte[] { 0x01, 0x0a, 0x00, 0x00, 0x00 }; int startK = spiltLen; int endK = indexOf(data, innerSplit, startK); byte[] k = new byte[endK - startK]; int k1 = 0; for (int i = startK; i < endK; i++) { k[k1++] = data[i]; //System.out.println(Integer.toHexString(arr[i])); } k1 = 0; String koatyy = new String(k, "ASCII"); //System.out.println(koatyy); csv[0] = koatyy; //Parent byte[] innerSplitP = new byte[] { 0x00, 0x00, 0x00 }; int startP = endK + spiltLen - 1; int endP = indexOf(data, innerSplitP, startP); byte[] p = new byte[endP - startP - 2]; // ignore unknow extenstions int p1 = 0; for (int i = startP; i < endP - 2; i++) { p[p1++] = data[i]; //System.out.println(Integer.toHexString(arr[i])); } p1 = 0; String parent = new String(p, "ASCII"); //System.out.println(parent); csv[1] = parent; //School name byte[] innerSplitS = new byte[] { 0x00, 0x00, 0x00 }; int startS = endP + 1; //innerSplitP.length; int endS = indexOf(data, innerSplitS, startS); byte[] s = new byte[endS - startS]; int s1 = 0; for (int i = startS; i < endS; i++) { s[s1++] = data[i]; //System.out.println(Integer.toHexString(arr[i])); } s1 = 0; String school = new String(s, "windows-1251"); //System.out.println(school); csv[2] = school; //School type, values and users int g = 0; //global index byte[] innerSplitV = new byte[] { 0x00, 0x01 }; /*if (dat.before(newDat)) { innerSplitV = new byte[]{0x00, 0x01, 0x20, 0x04}; }*/ int startV = endS - 2; int endV = indexOf(data, innerSplitV, startV + 2); while (endV > 0) { byte[] v = new byte[endV - startV]; int v1 = 0; for (int i = startV; i < endV; i++) { v[v1++] = data[i]; //System.out.println(Integer.toHexString(arr[i])); } try { switch (g) { case 0: { Integer type = Integer.valueOf(v[0]); /*System.out.print("Type is: "); System.out.println(type);*/ csv[3] = type.toString(); break; } case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: { switch (v[0]) { case 0x06: { byte[] num = new byte[3]; int lo = Integer.valueOf(v[1]); num[0] = v[2]; num[1] = v[3]; num[2] = v[4]; String valueInter = String.format("%02X", num[0]); valueInter += String.format("%02X", num[1]); valueInter += String.format("%02X", num[2]); valueInter = valueInter.substring(0, valueInter.length() - lo) + "." + valueInter.substring(valueInter.length() - lo, valueInter.length()); Double floatValue = Double.parseDouble(valueInter); /*System.out.print("Percent of" + g + " is: "); System.out.println(floatValue);*/ csv[3 + g] = floatValue.toString(); break; } case 0x0a: { Integer value = 0; /*System.out.print("Percent of" + g + " is: "); System.out.println(value);*/ csv[3 + g] = value.toString(); break; } case 0x02: { int lo = Integer.valueOf(v[1]); String valueInter = String.format("%02X", v[2]); valueInter = valueInter.substring(0, valueInter.length() - lo) + "." + valueInter.substring(valueInter.length() - lo, valueInter.length()); Double floatValue = Double.parseDouble(valueInter); /*System.out.print("Percent of" + g + " is: "); System.out.println(valueU + "." + valueL);*/ csv[3 + g] = String.valueOf(floatValue); break; } case 0x04: { int lo = Integer.valueOf(v[1]); String valueInter = String.format("%02X", v[2]); valueInter += String.format("%02X", v[3]); valueInter = valueInter.substring(0, valueInter.length() - lo) + "." + valueInter.substring(valueInter.length() - lo, valueInter.length()); Double floatValue = Double.parseDouble(valueInter); /*System.out.print("Percent of" + g + " is: "); System.out.println(valueU + "." + valueL);*/ csv[3 + g] = String.valueOf(floatValue); break; } case 0x20: { //if value os 01 error in find function fix it if (v.length < 16) { endV = indexOf(data, innerSplitV, endV + 2); v = new byte[endV - startV]; v1 = 0; for (int i = startV; i < endV; i++) { v[v1++] = data[i]; } } String valueInter = String.format("%02X", v[15]) + "." + String.format("%02X", v[16]) + String.format("%02X", v[17]); Double floatValue = Double.parseDouble(valueInter); /*System.out.print("Percent of" + g + " is: "); System.out.println(valueU + "." + valueL);*/ csv[3 + g] = String.valueOf(floatValue); break; } default: { System.out.println(school); System.out.print("Unknown type "); System.out.println(Integer.toHexString(v[0])); } } } /* default: { System.out.print("Unknown index "); System.out.println(g); break; }*/ } if (g == 10) { ///Integer pupils = Integer.valueOf(data[endV + 1]); String pupils = String.format("%02X", data[endV + 2]); //CHANGE TO HEX TO DEC /*System.out.print("Pupils are: "); System.out.println(pupils);*/ csv[3 + g + 1] = String.valueOf(Integer.parseInt(pupils, 16)); } startV = endV + innerSplitV.length; endV = indexOf(data, innerSplitV, startV + 2); g++; } catch (Exception e) { System.out.println(Arrays.toString(csv)); System.out.println(Arrays.toString(v)); throw new Exception(e); } } g = 0; return csv; } /** * Finds the first occurrence of the pattern in the text. */ private int indexOf(byte[] data, byte[] pattern, int offset) { int[] failure = computeFailure(pattern); int j = 0; if (data.length == 0) { return -1; } for (int i = offset; i < data.length; i++) { while (j > 0 && pattern[j] != data[i]) { j = failure[j - 1]; } if (pattern[j] == data[i]) { j++; } if (j == pattern.length) { return i - pattern.length + 1; } } return -1; } /** * Computes the failure function using a boot-strapping process, where the * pattern is matched against itself. */ private int[] computeFailure(byte[] pattern) { int[] failure = new int[pattern.length]; int j = 0; for (int i = 1; i < pattern.length; i++) { while (j > 0 && pattern[j] != pattern[i]) { j = failure[j - 1]; } if (pattern[j] == pattern[i]) { j++; } failure[i] = j; } return failure; } } ///e7EGmdGDc8nGDxLX