📄 clogreader.java
字号:
return bytesRead; } /** * reads only RAW records */ private boolean getRawEvents (String filename, RecordHandler mainTool) { int n = CONST.CLOG_BLOCK_SIZE, retval; InputStream in; if (!aplt) { if ((in = getFileIn (filename)) == null) return false; } else if ((in = getUrlIn (filename)) == null) return false; BufferedInputStream inFile = new BufferedInputStream (in, n); firsttime = 0; numEvt2 = 0; rawevtno = 0; for (retval = readRaw (inFile, mainTool); retval == n; retval = readRaw (inFile, mainTool)) { int a = (int)((numEvt2 * 100) / numEvt1); updateStatus (a); updateStatField (a); if (Thread.interrupted () || cancel) {close (inFile, filename); return false;} Thread.yield (); } try {inFile.close ();} catch (IOException y) { new ErrorDiag (null, "logfile:" + filename + " cannot be closed." + " Proceeding anyway."); } if (retval != 0) return false; return true; } private int readRaw (BufferedInputStream inFile, RecordHandler mainTool) { int n = CONST.CLOG_BLOCK_SIZE, bytesRead = n; byte[] block = new byte [n]; try {(new DataInputStream (inFile)).readFully (block);} catch (EOFException x) {return 0;} catch (IOException x) { new ErrorDiag (null, "IOException occured while reading logfile."); return -1; } DataInputStream in = new DataInputStream (new ByteArrayInputStream (block)); int rtype; CLOG_HEADER hdr = new CLOG_HEADER (); CLOG_RAW raw = new CLOG_RAW (); rtype = CONST.CLOG_UNDEF; while (rtype != CONST.CLOG_ENDBLOCK && rtype != CONST.CLOG_ENDLOG) { if (swap) hdr.readLitEnd (in); else hdr.readBigEnd (in); rtype = hdr.rectype; numEvt2++; try { switch (rtype){ case CONST.CLOG_RAWEVENT: if (rawevtno++ == 0) firsttime = hdr.timestamp; if (swap) raw.readLitEnd (in); else raw.readBigEnd (in); hdr.timestamp = hdr.timestamp - firsttime; mainTool.HandleRaw (new RECORD (hdr, raw)); break; case CONST.CLOG_STATEDEF: in.skipBytes (CLOG_STATE.getSize ()); break; case CONST.CLOG_SRCLOC: in.skipBytes (CLOG_SRC.getSize ()); break; case CONST.CLOG_COMMEVENT: in.skipBytes (CLOG_COMM.getSize ()); break; case CONST.CLOG_MSGEVENT: in.skipBytes (CLOG_MSG.getSize ()); break; case CONST.CLOG_COLLEVENT: in.skipBytes (CLOG_COLL.getSize ()); break; case CONST.CLOG_EVENTDEF: in.skipBytes (CLOG_EVENT.getSize ()); break; case CONST.CLOG_ENDBLOCK: break; case CONST.CLOG_ENDLOG: break; default: new ErrorDiag (null, "readRaw() : Unrecognized record type = " + rtype); return -1; } } catch (IOException x) { new ErrorDiag (null, "IOException occured while reading logfile"); return -1; } } return bytesRead; } /** * reads both RAW and CLOG_STATE records and assumes that CLOG_STATE * records will occur before RAW. * If this method is used the progress bar will not show progress relative to * the size of the file since there is no way of knowing the number of events * in the file during a single pass */ private boolean getData (String filename, RecordHandler mainTool) { int n = CONST.CLOG_BLOCK_SIZE, retval; InputStream in; if (!aplt) { if ((in = getFileIn (filename)) == null) return false; } else if ((in = getUrlIn (filename)) == null) return false; BufferedInputStream inFile = new BufferedInputStream (in, n); firsttime = 0; rawevtno = 0; numEvt1 = 0; for (retval = readBlock (inFile, mainTool); retval == n; retval = readBlock (inFile, mainTool)) { updateStatus (numEvt1 % 100); if (Thread.interrupted () || cancel) {close (inFile, filename); return false;} Thread.yield (); } try {inFile.close ();} catch (IOException y) { new ErrorDiag (null, "logfile:" + filename + " cannot be closed." + " Proceeding anyway."); } return true; } private int readBlock (BufferedInputStream inFile, RecordHandler mainTool) { int n = CONST.CLOG_BLOCK_SIZE, bytesRead = 0; byte[] block = new byte [n]; try {(new DataInputStream (inFile)).readFully (block);} catch (EOFException x) {return 0;} catch (IOException x) { new ErrorDiag (null, "IOException occured while reading logfile."); return -1; } DataInputStream in = new DataInputStream (new ByteArrayInputStream (block)); int rtype; CLOG_HEADER hdr = new CLOG_HEADER (); CLOG_RAW raw = new CLOG_RAW (); rtype = CONST.CLOG_UNDEF; while (rtype != CONST.CLOG_ENDBLOCK && rtype != CONST.CLOG_ENDLOG) { if (swap) hdr.readLitEnd (in); else hdr.readBigEnd (in); rtype = hdr.rectype; numEvt1++; try { switch (rtype){ case CONST.CLOG_RAWEVENT: if (rawevtno++ == 0) firsttime = hdr.timestamp; if (swap) raw.readLitEnd (in); else raw.readBigEnd (in); hdr.timestamp = hdr.timestamp - firsttime; mainTool.HandleRaw (new RECORD (hdr, raw)); break; case CONST.CLOG_STATEDEF: CLOG_STATE state = new CLOG_STATE(); if (swap) state.readLitEnd (in); else state.readBigEnd (in); mainTool.HandleStateDef (state); break; case CONST.CLOG_SRCLOC: in.skipBytes (CLOG_SRC.getSize ()); break; case CONST.CLOG_COMMEVENT: in.skipBytes (CLOG_COMM.getSize ()); break; case CONST.CLOG_MSGEVENT: in.skipBytes (CLOG_MSG.getSize ()); break; case CONST.CLOG_COLLEVENT: in.skipBytes (CLOG_COLL.getSize ()); break; case CONST.CLOG_EVENTDEF: in.skipBytes (CLOG_EVENT.getSize ()); break; case CONST.CLOG_ENDBLOCK: break; case CONST.CLOG_ENDLOG: break; default: new ErrorDiag (null, "readBlock() : Unrecognized record type = " + rtype); return -1; } }catch (IOException x) { new ErrorDiag (null, "IOException occured while reading logfile."); return -1; } } return bytesRead; } private InputStream getFileIn (String name) { InputStream in = null; try {in = new FileInputStream (name);} catch (FileNotFoundException e) { new ErrorDiag (null, "file: " + name + " not found."); return null; } return in; } private InputStream getUrlIn (String name) { URL url = null; InputStream in = null; try {url = new URL (name);} catch (MalformedURLException e) { new ErrorDiag (null, "Bad URL:" + url); return null; } try {in = url.openStream ();} catch (IOException e) { new ErrorDiag (null, "IO Error:" + e.getMessage ()); return null; } return in; } /** * When the worker needs to update the GUI we do so by queuing * a Runnable for the event dispatching thread with * SwingUtilities.invokeLater(). In this case we're just * changing the progress bars value. */ private void updateStatus(final int i) { Runnable doSetProgressBarValue = new Runnable() { public void run() { progressBar.setValue(i); } }; SwingUtilities.invokeLater(doSetProgressBarValue); } /** * In this case we're just updating status field. */ private void updateStatField (final int i) { Runnable setField = new Runnable() { public void run() { statusField.setText ("Making second pass over file: " + i + "% done"); } }; SwingUtilities.invokeLater(setField); } /** * This is an action listener, called by the "Cancel" button */ private ActionListener interruptListener = new ActionListener() { public void actionPerformed(ActionEvent event) {cancel = true;} }; /** * Close InputStream */ private void close (InputStream in, String filename) { try {in.close ();} catch (IOException x) { new ErrorDiag (null, "logfile:" + filename + " cannot be closed"); } } protected void finalize () throws Throwable {super.finalize ();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -