InputStream is = DataParser.class.getClassLoader().getResourceAsStream("baidu_cdr.pcap");
Pcap pcap = PcapParser.unpack(is);
is.close();
byte[] t = pcap.getData().get(0).getContent();
byte[] data = Arrays.copyOfRange(t, 42, t.length);
package com.hylanda.pcap;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhouqisheng
*
*/
public class PcapParser {
public static Pcap unpack(InputStream is) throws IOException {
Pcap pcap = null;
byte[] buffer_4 = new byte[4];
byte[] buffer_2 = new byte[2];
pcap = new Pcap();
PcapHeader header = new PcapHeader();
int m = is.read(buffer_4);
if(m != 4){
return null;
}
reverseByteArray(buffer_4);
header.setMagic(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_2);
reverseByteArray(buffer_2);
header.setMagor_version(byteArrayToShort(buffer_2, 0));
m = is.read(buffer_2);
reverseByteArray(buffer_2);
header.setMinor_version(byteArrayToShort(buffer_2, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
header.setTimezone(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
header.setSigflags(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
header.setSnaplen(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
header.setLinktype(byteArrayToInt(buffer_4, 0));
pcap.setHeader(header);
List<PcapData> dataList = new ArrayList<PcapData>();
while (m > 0) {
PcapData data = new PcapData();
m = is.read(buffer_4);
if (m < 0) {
break;
}
reverseByteArray(buffer_4);
data.setTime_s(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
data.setTime_ms(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
data.setpLength(byteArrayToInt(buffer_4, 0));
m = is.read(buffer_4);
reverseByteArray(buffer_4);
data.setLength(byteArrayToInt(buffer_4, 0));
byte[] content = new byte[data.getpLength()];
m = is.read(content);
data.setContent(content);
dataList.add(data);
}
pcap.setData(dataList);
return pcap;
}
private static int byteArrayToInt(byte[] b, int offset) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}
private static short byteArrayToShort(byte[] b, int offset) {
short value = 0;
for (int i = 0; i < 2; i++) {
int shift = (2 - 1 - i) * 8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}
/**
* 反转数组
* @param arr
*/
private static void reverseByteArray(byte[] arr){
byte temp;
int n = arr.length;
for(int i=0; i<n/2; i++){
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
}
}
/**
*
*/
package com.hylanda.pcap;
import java.util.List;
/**
* @author zhouqisheng
*
*/
public class Pcap {
private PcapHeader header;
private List<PcapData> data;
public PcapHeader getHeader() {
return header;
}
public void setHeader(PcapHeader header) {
this.header = header;
}
public List<PcapData> getData() {
return data;
}
public void setData(List<PcapData> data) {
this.d[\J
H]\[\NXX[\J[[\JH\[\HH[\N\YOXX[[^[НZ[\H][НZ[\
N\[
XYK\[
[Y^[\XYN\[
XY\HK\[
\XY\N\[
Z[\HK\[
\Z[\N\[
[Y^OHK\[
\[Y^JN\[
YHK\[
\YN\[
\[HK\[
\\[N\[
[\OHK\[
\[\JN]\[NOO]