1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
15 import java
.util
.ListIterator
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
19 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
22 * <b><u>StreamInputReader</u></b>
24 * Reads the events of a trace file.
26 public class StreamInputReader
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
33 * The StreamInput we are reading.
35 private final StreamInput streamInput
;
38 * The packet reader used to read packets from this trace file.
40 private final StreamInputPacketReader packetReader
;
43 * Iterator on the packet index
45 private ListIterator
<StreamInputPacketIndexEntry
> packetIndexIt
;
49 * Reference to the current event of this trace file (iow, the last on that
50 * was read, the next one to be returned)
52 private EventDefinition currentEvent
= null;
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 * Constructs a StreamInputReader that reads a StreamInput.
64 * The StreamInput to read.
66 public StreamInputReader(StreamInput streamInput
) {
67 this.streamInput
= streamInput
;
68 this.packetReader
= new StreamInputPacketReader(this);
71 * Get the iterator on the packet index.
73 this.packetIndexIt
= streamInput
.getIndex().listIterator();
76 * Make first packet the current one.
81 // ------------------------------------------------------------------------
82 // Getters/Setters/Predicates
83 // ------------------------------------------------------------------------
85 public EventDefinition
getCurrentEvent() {
86 return this.currentEvent
;
89 public StructDefinition
getCurrentPacketContext() {
90 return this.packetReader
.getStreamPacketContextDef();
93 public StreamInput
getStreamInput() {
94 return this.streamInput
;
97 public int getName() {
101 public void setName(int name
) {
105 public int getCPU() {
106 return this.packetReader
.getCPU();
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
113 * Reads the next event in the current event variable.
115 * @return If an event has been successfully read.
117 public boolean readNextEvent() {
119 * Change packet if needed
121 if (!this.packetReader
.hasMoreEvents()) {
126 * If an event is available, read it.
128 if (this.packetReader
.hasMoreEvents()) {
130 this.setCurrentEvent(this.packetReader
.readNextEvent());
131 } catch (CTFReaderException e
) {
132 /* Some problem happened, we'll assume there is no more events */
137 this.setCurrentEvent(null);
142 * Change the current packet of the packet reader to the next one.
144 private void goToNextPacket() {
145 if (getPacketIndexIt().hasNext()) {
146 StreamInputPacketIndexEntry nextPacket
= getPacketIndexIt().next();
147 this.packetReader
.setCurrentPacket(nextPacket
);
149 this.packetReader
.setCurrentPacket(null);
154 * Changes the location of the trace file reader so that the current event
155 * is the first event with a timestamp greater than the given timestamp.
158 * The timestamp to seek to.
160 public long seek(long timestamp
) {
163 * Search in the index for the packet to search in.
165 this.packetIndexIt
= this.streamInput
.getIndex().search(timestamp
);
168 * Switch to this packet.
173 * Advance until A. we reached the end of the trace file (which means
174 * the given timestamp is after the last event), or B. we found the
175 * first event with a timestamp greater than the given timestamp.
178 boolean done
= (this.getCurrentEvent() == null);
179 while (!done
&& (this.getCurrentEvent().timestamp
< timestamp
)) {
181 done
= (this.getCurrentEvent() == null);
188 public long seekIndex(long index
) throws CTFReaderException
191 * Search in the index for the packet to search in.
193 this.packetIndexIt
= this.streamInput
.getIndex().searchIndex(index
);
195 * Switch to this packet.
199 * Read the first packet
203 * get the current index
205 if(this.packetReader
.getCurrentPacket() == null){
206 throw new CTFReaderException("Current packet null in index seek, did you index your trace yet?");
208 return this.packetReader
.getCurrentPacket().getIndexBegin();
212 public void goToLastEvent() throws CTFReaderException
{
214 * Search in the index for the packet to search in.
216 int len
= this.streamInput
.getIndex().getEntries().size();
217 StreamInputPacketIndexEntry entry
= this.streamInput
.getIndex().getEntries().get(len
-1);
218 seek(entry
.getTimestampEnd() - 1 );
220 * Go until the end of that packet
222 while (this.packetReader
.hasMoreEvents()) {
223 this.packetReader
.readNextEvent();
227 public void setCurrentEvent(EventDefinition currentEvent
) {
228 this.currentEvent
= currentEvent
;
232 * @return the packetIndexIt
234 private ListIterator
<StreamInputPacketIndexEntry
> getPacketIndexIt() {
235 return packetIndexIt
;
239 * @return the packetReader
241 public StreamInputPacketReader
getPacketReader() {