?? logreader.java
字號:
import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.util.Vector;
public class LogReader {
private File filename;//要處理的文件
private LogDealer logdealer;//負責輸出數據.
public File getFilename() {
return filename;
}
public void setFilename(File filename) {
this.filename = filename;
}
public LogDealer getLogdealer() {
return logdealer;
}
public void setLogdealer(LogDealer logdealer) {
this.logdealer = logdealer;
}
/**
* 把日志文件映射成內存緩沖
* @param File f日志文件
* @return MappedByteBuffer 內存映射緩沖。
* @throws 考慮異常
* */
private MappedByteBuffer mappedFile2Buffer(File f) throws Exception{
FileInputStream fis=new FileInputStream(f);
FileChannel fc=fis.getChannel();
return fc.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
}
/**
* 讀取所有登錄日志,并按登入與登出分類放入數據結構
* @param MappedByteBuffer buffer 日志文件的內存緩沖
* Vector<LogRecord> logins 日志的登入數據
* Vector<LogRecord> logouts 日志的登出數據
* @return void
* @throws 考慮異常
* */
private void readLog(MappedByteBuffer buffer,
Vector<LogRecord> logins,Vector<LogRecord> logouts){
int len=buffer.capacity()/372;//記錄條數
for(int i=0;i<len;i++){//循環讀取數據
buffer.position(i*372);
byte[] busername=new byte[32];
buffer.get(busername);//讀取用戶名
String username=new String(busername).trim();
if(username.startsWith(".")){continue;}
LogRecord log=new LogRecord();
log.setUsername(username);
buffer.get(new byte[4]);//跳4字節
byte[] bdevice=new byte[32];//讀取設備類型
buffer.get(bdevice);
log.setDevice(new String(bdevice).trim());
log.setPid(buffer.getInt());//進程ID
short type=buffer.getShort();//登錄類型
buffer.get(new byte[6]);//跳6個字節
log.setVisittime(buffer.getInt());//登錄時間
buffer.get(new byte[30]);//跳30字節
byte[] buserip=new byte[257];//客戶IP
buffer.get(buserip);
log.setUserip(new String(buserip).trim());
//分類存放到數據結構.
if(type==7){/*登入*/ logins.add(log);}
if(type==8){/*登出*/ logouts.add(log);}
}
buffer.clear();
buffer=null;
}
/**
* 把登錄數據按一個登入一個登出匹配成一條完整的登錄過程記錄。
* @param Vector<LogRecord> logins 日志的登入數據
* Vector<LogRecord> logouts 日志的登出數據
* @return Vector<MatchedRecord> 所有完整的登入/登出記錄
* @throws 考慮異常
* */
private Vector<MatchedRecord> match(Vector<LogRecord> logins,Vector<LogRecord> logouts){
Vector<MatchedRecord> re=new Vector<MatchedRecord>();/** 循環匹配查找.找到:就得到匹配數據* 找不到:就表示在線**/
int num=logins.size();
NEXT:for(int i=0;i<num;i++){
LogRecord login=logins.get(0);
MatchedRecord matched=new MatchedRecord();
matched.setUsername(login.getUsername());matched.setDevice(login.getDevice());
matched.setUserip(login.getUserip());matched.setLogintime(login.getVisittime());
for(int j=0;j<logouts.size();j++){
LogRecord logout=logouts.get(j);
if(login.equals(logout)){//匹配
matched.setLogouttime(logout.getVisittime());//匹配成功,則生成一條登錄數據
long duration=logout.getVisittime()-login.getVisittime();
int days=(int)(duration/(24*60*60));
duration=(int)(duration-days*(24*60*60));
int hours=(int)(duration/(60*60));
duration=(int)(duration-hours*(60*60));
int minutes=(int)(duration/60);
String sduration="";
if(days>0){sduration+="<+"+days+">";}
DecimalFormat format=new DecimalFormat("00");
sduration+=format.format(hours)+":"+format.format(minutes);
matched.setDuration(sduration);
logouts.remove(logout);/*刪除該登出數據*/logins.remove(login);//刪除該登入數據
re.add(matched);continue NEXT;}}
matched.setLogouttime(0);//沒有匹配成功,表示在線。
matched.setDuration("仍然在登錄狀態");
logins.remove(login);
re.add(matched);}
return re;
}
/**
* 得到所有的登錄歷史數據
* @param 無
* @return void
* @throws 考慮異常
* */
public void collect()throws Exception{
MappedByteBuffer buffer=mappedFile2Buffer(filename);
Vector<LogRecord> logins=new Vector<LogRecord>();
Vector<LogRecord> logouts=new Vector<LogRecord>();
readLog(buffer, logins, logouts);
Vector<MatchedRecord> matched=match(logins,logouts);
logdealer.deal(matched);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -