|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface FileCapture
Immutable interface to an open capture file. Use the factory
method CaptureFactory.openFile(java.io.File) to get an instance of this interface.
Capture files contain network packet data captured from a network interface and stored in the file. The CaptureFile interface provides an abstraction to the possible formats for capture files.
FileCapture extends the standard Capture interface and adds several methods
that provide file related information. Use the #hasNext and #next methods
to iterate over packets within the file or call on getRecords(java.lang.Class method to iterate
over raw block records within the file. Block record (also called file header) contains
a set of data records which contain captured packet data. The reason you have to iterate
over block records, is that certain formats contain multiple block records while others
can only contain one. getRecords(java.lang.Class method is generic and you can request a more specific
subclass of the block record. For example if you know you are working with a PCAP file,
you can check using getFileType() method, you can request to retrieve
the PCAP block records, and there should only be one for PCAP files.
RecordIterator<PcapBlockRecord> fileHeader = fileCapture.getRecords(PcapBlockRecord.class); // Do something with the file header like get its magic number, timezone or accuracyAnother thing you can do once you have a block record is iterate over the block records child data records which contain packet data, as follows:
RecordIterator<PcapBlockRecord> fileHeaders = fileCapture.getRecords(PcapBlockRecord.class);
if (fileHeader.hasNext() == false) return; // Empty file
RecordIterator<PcapPacketRecord> rawPackets = fileHeaders.next().getRecords(PcapPacketRecord.class):
while (rawPackets.hasNext()) {
PcapPacketRecord packet = rawPackets.next();
// Do something with the raw packet record like extract its seconds or micros fields
// Or more importantly access is data buffer using DataRecord.getDataBuffer()
// PcapPacketRecord extends the DataRecord interface
}
As you can see that various levels of access to file content is provided depending on
what is needed. The simplest is using the top level IOIterator methods (extended
by Capture interface) and simply iterate over the file, bypassing all of the complexity
of its structure. When needed though, you can access any file formats exact structure. This
level of access requires somewhat higher knowledge about file format, although that is also
abstracted quiet a bit as well.
Note this is an immutable interface which does not allow any modification to the file.
If you need to modify the file, you can use the Capture.isCaptureMutable() method to
check if this FileCapture can be made mutable and call on Capture.getMutableCapture()
to aquire a reference to MutableFileCapture which provides methods for modifying
the underlying data store. All stream based FileCaptures are immutable and can not be
turned into mutable versions as input streams are immutable.
| Method Summary | ||
|---|---|---|
|
asType(java.lang.Class<C> c)
Converts the generic capture file into a more specific type. |
|
void |
close()
Closes the capture session and underlying storage if it was opened. |
|
FileType |
getFileType()
Returns file type of the currently open file. |
|
java.nio.ByteOrder |
getHeaderByteOrder()
|
|
byte[] |
getMagicPattern()
Returns the Magic number or pattern that is used to uniquely identify the file type of the capture file. |
|
long |
getPacketCount()
Returns the number of packets within the file. |
|
long |
getPacketCount(PacketCounterModel model)
Gets the packet count using a different algorithm. |
|
|
getRecords(java.lang.Class<E> c)
|
|
Version |
getVersion()
Returns the first file version found. |
|
FilePacket |
newPacket()
Creates an empty packet record that is appropriate for the current file type. |
|
| Methods inherited from interface jnetpcap.capture.Capture |
|---|
getFilter, getMutableCapture, getRemoteSession, isCaptureMutable, isCaptureRemote, setFilter |
| Methods inherited from interface jnetpcap.capture.FileIterator |
|---|
getPosition |
| Method Detail |
|---|
void close()
throws java.io.IOException
Capture
close in interface Capture<FilePacket>close in interface java.io.Closeablejava.io.IOException - any IO errorsjava.nio.ByteOrder getHeaderByteOrder()
FileType getFileType()
byte[] getMagicPattern()
throws java.io.IOException
java.io.IOException - any IO errors
long getPacketCount()
throws java.io.IOException
Returns the number of packets within the file. This only includes
records that hold packet data and not any additional meta data records.
The method uses the default PacketCounter. If estimated packet counter
is acceptable you can use one of of several other PacketCounterModels to
calculate estimated packet count using the
getPacketCount(PacketCounterModel)
method.
The default PacketCounterModel is file type specific. The model at minimum
returns an accurate count of packet records within the capture file, but no
guarrantees about performance can be made and the performance will vary from
format to format. You can use the more explicit getPacketCount(PacketCounterModel)
method to counter packets.
java.io.IOException - any io errors
long getPacketCount(PacketCounterModel model)
throws java.io.IOException
model - model/algorithm to use to count packets
java.io.IOException - any io exceptions<E extends BlockRecord> RecordIterator<E> getRecords(java.lang.Class<E> c)
Version getVersion()
FilePacket newPacket()
throws java.io.IOException
Creates an empty packet record that is appropriate for the current file type. The record has not been written to file, does contain the appropriate record header; no packet buffer has been defined. So you must set a packet buffer.
java.io.IOException
<C extends FileCapture> C asType(java.lang.Class<C> c)
throws CaptureFormatException
Converts the generic capture file into a more specific type. The type
Here is an example that opens up a PCAP file and then aquires a reference to actual PCAPFile interface.
CaptureFile file = CaptureFactory.openFile(new File("somefile.pcap"));
PCAPFile pcap = file.asType(PCAPFile.class);
E - c -
CaptureFormatException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||