package org.eclipse.linuxtools.ctf.core.trace;
-import java.util.ListIterator;
+import java.nio.ByteOrder;
import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
+import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
/**
* <b><u>StreamInputReader</u></b>
/**
* Iterator on the packet index
*/
- private ListIterator<StreamInputPacketIndexEntry> packetIndexIt;
-
+ private int packetIndex;
/**
* Reference to the current event of this trace file (iow, the last on that
private int name;
+ private CTFTraceReader parent;
+
+ private final long prevIndex;
+
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
public StreamInputReader(StreamInput streamInput) {
this.streamInput = streamInput;
this.packetReader = new StreamInputPacketReader(this);
-
/*
* Get the iterator on the packet index.
*/
- this.packetIndexIt = streamInput.getIndex().listIterator();
+ this.packetIndex = 0;
+ this.prevIndex = 0;
/*
* Make first packet the current one.
*/
return this.packetReader.getStreamPacketContextDef();
}
- public StreamInput getStreamInput() {
- return this.streamInput;
+ public ByteOrder getByteOrder() {
+ return streamInput.getStream().getTrace().getByteOrder();
}
public int getName() {
return this.packetReader.getCPU();
}
+ public String getFilename() {
+ return streamInput.getFilename();
+ }
+
+ /*
+ * for internal use only
+ */
+ StreamInput getStreamInput() {
+ return streamInput;
+ }
+
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
* @return If an event has been successfully read.
*/
public boolean readNextEvent() {
+
/*
* Change packet if needed
*/
if (!this.packetReader.hasMoreEvents()) {
- goToNextPacket();
+ final StreamInputPacketIndexEntry prevPacket = this.packetReader
+ .getCurrentPacket();
+ if (prevPacket != null) {
+ goToNextPacket();
+ final StreamInputPacketIndexEntry currentPacket = this.packetReader
+ .getCurrentPacket();
+ }
}
/*
try {
this.setCurrentEvent(this.packetReader.readNextEvent());
} catch (CTFReaderException e) {
- /* Some problem happened, we'll assume there is no more events */
+ /*
+ * Some problem happened, we'll assume that there are no more
+ * events
+ */
return false;
}
return true;
* Change the current packet of the packet reader to the next one.
*/
private void goToNextPacket() {
- if (getPacketIndexIt().hasNext()) {
- this.packetReader.setCurrentPacket(getPacketIndexIt().next());
+ packetIndex++;
+ if (getPacketSize() >= (packetIndex + 1)) {
+ this.packetReader.setCurrentPacket(getPacket());
} else {
- this.packetReader.setCurrentPacket(null);
+ try {
+ if (this.streamInput.addPacketHeaderIndex()) {
+ packetIndex = getPacketSize() - 1;
+ this.packetReader.setCurrentPacket(getPacket());
+
+ } else {
+ this.packetReader.setCurrentPacket(null);
+ }
+
+ } catch (CTFReaderException e) {
+ this.packetReader.setCurrentPacket(null);
+ }
}
}
+ /**
+ * @return
+ */
+ private int getPacketSize() {
+ return streamInput.getIndex().getEntries().size();
+ }
+
/**
* Changes the location of the trace file reader so that the current event
* is the first event with a timestamp greater than the given timestamp.
* The timestamp to seek to.
*/
public long seek(long timestamp) {
- long offset = 0 ;
+ long offset = 0;
/*
* Search in the index for the packet to search in.
*/
- this.packetIndexIt = this.streamInput.getIndex().search(timestamp);
+ this.packetIndex = this.streamInput.getIndex().search(timestamp)
+ .previousIndex();
/*
* Switch to this packet.
*/
goToNextPacket();
+ /*
+ * index up to the desired timestamp.
+ */
+ while ((this.packetReader.getCurrentPacket() != null)
+ && (this.packetReader.getCurrentPacket().getTimestampEnd() < timestamp)) {
+ try {
+ this.streamInput.addPacketHeaderIndex();
+ goToNextPacket();
+ } catch (CTFReaderException e) {
+ // do nothing here
+ }
+ }
+
/*
* Advance until A. we reached the end of the trace file (which means
* the given timestamp is after the last event), or B. we found the
*/
readNextEvent();
boolean done = (this.getCurrentEvent() == null);
- while (!done && (this.getCurrentEvent().timestamp < timestamp)) {
+ while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
readNextEvent();
done = (this.getCurrentEvent() == null);
- offset++ ;
+ offset++;
}
return offset;
}
-
- public long seekBeforeIndex(long index)
- {
+ public void goToLastEvent() {
/*
* Search in the index for the packet to search in.
*/
- this.packetIndexIt = this.streamInput.getIndex().searchIndex(index);
+ final int len = this.streamInput.getIndex().getEntries().size();
+
+ StreamInputPacketIndexEntry entry = null;
/*
- * Switch to this packet.
+ * Go to beginning of trace.
*/
- goToNextPacket();
+ seek(0);
/*
- * get the current index
+ * if the trace is empty.
*/
- return this.packetReader.getCurrentPacket().indexBegin;
-
- }
-
- public void goToLastEvent() throws CTFReaderException {
+ if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) {
+ /*
+ * This means the trace is empty. abort.
+ */
+ return;
+ }
/*
- * Search in the index for the packet to search in.
+ * Go to the last packet that contains events.
*/
- int len = this.streamInput.getIndex().getEntries().size();
- StreamInputPacketIndexEntry entry = this.streamInput.getIndex().getEntries().get(len-1);
- seek(entry.timestampEnd - 1 );
+ for (int pos = len - 1; pos > 0; pos--) {
+ packetIndex = pos;
+ this.packetReader.setCurrentPacket(getPacket());
+ if (this.packetReader.hasMoreEvents()) {
+ break;
+ }
+ }
+
/*
* Go until the end of that packet
*/
- while (this.packetReader.hasMoreEvents()) {
- this.packetReader.readNextEvent();
+ EventDefinition prevEvent = null;
+ while (this.currentEvent != null) {
+ prevEvent = this.currentEvent;
+ this.readNextEvent();
}
+ /*
+ * Go back to the previous event
+ */
+ this.setCurrentEvent(prevEvent);
+ }
+
+ /**
+ * @return the parent
+ */
+ public CTFTraceReader getParent() {
+ return parent;
+ }
+
+ /**
+ * @param parent
+ * the parent to set
+ */
+ public void setParent(CTFTraceReader parent) {
+ this.parent = parent;
}
public void setCurrentEvent(EventDefinition currentEvent) {
/**
* @return the packetIndexIt
*/
- private ListIterator<StreamInputPacketIndexEntry> getPacketIndexIt() {
- return packetIndexIt;
+ private int getPacketIndex() {
+ return packetIndex;
+ }
+
+ private StreamInputPacketIndexEntry getPacket() {
+ return streamInput.getIndex().getEntries().get(getPacketIndex());
}
/**
return packetReader;
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = (prime * result) + name;
+ result = (prime * result)
+ + ((streamInput == null) ? 0 : streamInput.hashCode());
+ return result;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof StreamInputReader)) {
+ return false;
+ }
+ StreamInputReader other = (StreamInputReader) obj;
+ if (name != other.name) {
+ return false;
+ }
+ if (streamInput == null) {
+ if (other.streamInput != null) {
+ return false;
+ }
+ } else if (!streamInput.equals(other.streamInput)) {
+ return false;
+ }
+ return true;
+ }
+
}