|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
public interface FileCapture<T extends FilePacket>
File capture extends the basic Capture interface and adds several capabilites which can only be done with files, as opposed to live captures for example. You can modify the contents of the file using FileCapture API and its supporting interface. You also have access to lower level API which works more closely with the structure of the file, such as records. At this lower level some knowledge of the file structure is neccessary and is explained in various API documentation sections. The file structure is still fairely abstracted and you may continue to work with generic records instead of specified ones.
Capture files contain network packet data captured from a network interface and stored in the file. The FileCapture 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.
RecordIterator<BlockRecord> i = fileCapture.getBlockRecordIterator();
if (i.hasNext() == false) {
return;
}
BlockRecord block = i.next();
All capture files utilize a similar format that there is atleast 1 block
record which contains 0 or more data records. Data records are typically
packet records. There can also be other record types which hold various meta
information such as counters and properties.
RecordIterator<BlockRecord> blocks = fileCapture.getBlockRecordIterator();
while (blocks.hasNext() {
BlockRecord block = blocks.next();
RecordIterator<DataRecord> i = block.getRecordIterator();
while (i.hasNext()) {
DataRecord record = i.next();
System.out.printf("Record type=%s", record.getRecordType().toString());
}
}
The reason you aquire the block as an iterator is the certain formats have
more than one block in the file. Most only have 1 though, so if you know that
you are going to be working with a PCAP or SNOOP file, you can simply just
aquire the block record immediately without putting everything in a loop.
Notice that this extra level of inderection with the Block records is only
neccessary when accessing records. The PacketIterator returns and manipulates
packet records no matter which block they physicaly reside. You aquire the
PacketIterator directly from the FileCapture.
try {
PacketIterator<FilePacket> i = fileCapture.getPacketIterator();
while (i.hasNext()) {
FilePacket packet = i.next();
System.out.println(packet.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
or the more compact Java 5 version:
try {
for (FilePacket packet: fileCapture) {
System.out.println(packet.toString());
}
catch (IORuntimeException e) {
e.ioException().printStackTrace();
}
Notice that the second version throws IORuntimeException while the first
throws the plain old IOException if any IO errors are encountered. This is
because the normal java.util.Iterator and java.lang.Iterable interfaces do
not provide a way to allow and applications throw any exceptions besides
runtime exceptions. You still need to surround your loops around an iterator
with try/catch statements in order to catch any IO errors thrown as a
IORuntimeException.
Another way to access packets within a capture file is using the CaptureIndexer interface. The indexer provides a type of a List view of all the packets within the capture file. You use indexes to access a specific packet found within the file. Use the getPacketIndexer method to aquire a reference to an indexer of all the packets within the file.
The indexer only provides a view of the file, its typically not able to load all packets into memory from a file as some capture files can be very large. So the indexer pulls packets in and out of memory as neccessary when requested. For efficiency is goes a lot of caching of packets using SoftReferences. You must keep in mind, that any operation on the packet indexer may result in an IO operation, this has a performance and resource impact.
Both methods of accessing packets and records provide methods for modifying the capture file. Packets and records can be removed, switched, replaced and inserted anywhere within the file. All changes are first stored in memory and at certain points flushed to physical medium which is when the backend file gets physically modified. At anytime you can call on undoAllChanges method which discards all changes that have not been flushed and reverts to the original state of the file. If any changes had already been flushed to the file, those changes are not undone.
| Method Summary | |
|---|---|
void |
abortChanges()
Method which abandons all currently cached and pending changes to the file contents. |
void |
flush()
Changes to file content are cached for efficiency reasons. |
com.slytechs.utils.collection.IOSkippableIterator<? extends BlockRecord> |
getBlockIterator()
Each capture file is organized so that there exists atleast 1 block record, usually at the beginning of the capture file. |
FastIterator |
getFastIterator()
|
java.io.File |
getFile()
Returns the underlying file object that is associated with this FileCapture. |
FormatType |
getFormatType()
Gets the format type of this file. |
long |
getLength()
Returns the length of the entire file including any changes that have been made. |
long |
getPacketCount()
Returns the number of packets within the file. |
long |
getPacketCount(PacketCounterModel model)
Gets the packet count using a different algorithm. |
PacketIndexer<T> |
getPacketIndexer()
Indexer which accesses packets by index. |
PacketIterator<T> |
getPacketIterator()
Packet iterator provides methods for mutation, searches and regular iteration over the entire file based capture. |
RawIndexer |
getRawIndexer()
Raw ByteBuffer based record indexer. |
RawIterator |
getRawIterator()
Gets an iterator that will return raw contents of the records contained in the underlying capture file. |
RawIterator |
getRawIterator(Filter<RecordFilterTarget> filter)
Gets an iterator that will return raw contents of the records contained in the underlying capture file. |
RecordIndexer<? extends Record> |
getRecordIndexer()
Indexer which accesses records by index. |
RecordIterator<? extends Record> |
getRecordIterator()
Iterator which iterates over every record within the file capture. |
RecordIterator<? extends Record> |
getRecordIterator(Filter<RecordFilterTarget> filter)
Iterator which iterates over every record within the file capture. |
com.slytechs.utils.number.Version |
getVersion()
Returns the first file version found. |
boolean |
isEmpty()
Tells if the capture file contains 0 packets. |
boolean |
isOpen()
Checks if the current capture has an open connection to a physical file. |
java.nio.ByteOrder |
order()
Gets the byte order of the underlying capture file. |
| Methods inherited from interface org.jnetstream.capture.Capture |
|---|
getFilter, getType, isMutable, iterator |
| Method Detail |
|---|
void abortChanges()
throws java.io.IOException
Method which abandons all currently cached and pending changes to the file
contents. Initialy when a file is opened, no pending changes are waiting to
be flushed, invoking the #abort method has no effect at this time.
Once changes accumulate they continue to be cached until flushed, they can
be aborted at any time and the file state is reverted back to the last
flush call.
Any flushed changes that resulted in changes in the underlying capture file can not be undone and will remain. Only changes still in memory will be undone.
java.io.IOException - any IO errors
void flush()
throws java.io.IOException
flush in interface java.io.Flushablejava.io.IOException
com.slytechs.utils.collection.IOSkippableIterator<? extends BlockRecord> getBlockIterator()
throws java.io.IOException
java.io.IOExceptionFastIterator getFastIterator()
java.io.File getFile()
FormatType getFormatType()
long getLength()
Returns the length of the entire file including any changes that have been
made. This value may be different from the value returned from
getFile().length() as some changes may still reside in
memory and have not been flushed to physical storage.
However if you call abortChanges() and immediately after the call
getFile().length() == getLength().
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
PacketIndexer<T> getPacketIndexer()
throws java.io.IOException
java.io.IOException - any IO errors
PacketIterator<T> getPacketIterator()
throws java.io.IOException
Packet iterator provides methods for mutation, searches and regular iteration over the entire file based capture. Iterator keeps a current position in form of a cursor. Most of the methods work with the cursor either adjusting its position or using the cursor to learn the exact location where some operation should take place.
There is a special postion at the end of the file, past the last byte of file data. This location has a restriction on which operations may operate at this cursor position. For example you can no remove any elements at this location, but you can add which in effect is an append operation.
There may also be some restrictions on which operations are supported based
on the FileMode using which the file capture was opened. In
FileMode.ReadWrite or any such related modes, all operations
are functional. In FileMode.ReadOnly or any such related
modes, any operations which makes modifications may fail with a
ReadonlyBuffer exception. You can use isMutable
to check if the current file capture is opened in read-write mode.
getPacketIterator in interface Capture<T extends FilePacket>java.io.IOException - Any IO errors while retrieving a packet
RawIndexer getRawIndexer()
throws java.io.IOException
java.io.IOException - any IO errors
RawIterator getRawIterator()
throws java.io.IOException
java.io.IOException - any IO errors
RawIterator getRawIterator(Filter<RecordFilterTarget> filter)
throws java.io.IOException
filter - record filter which will be applied to records during iteration
java.io.IOException - any IO errors
RecordIndexer<? extends Record> getRecordIndexer()
throws java.io.IOException
java.io.IOException - any IO errors
RecordIterator<? extends Record> getRecordIterator()
throws java.io.IOException
java.io.IOException
RecordIterator<? extends Record> getRecordIterator(Filter<RecordFilterTarget> filter)
throws java.io.IOException
filter - a filter that will be applied to match packets during iteration
java.io.IOException
com.slytechs.utils.number.Version getVersion()
throws java.io.IOException
java.io.IOException
boolean isEmpty()
throws java.io.IOException
java.io.IOException - any IO errorsboolean isOpen()
java.nio.ByteOrder order()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||