Contribute native CTF Parser (bug370499)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.trace;
14
15 import java.util.ListIterator;
16
17 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
18 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
19
20 /**
21 * <b><u>StreamInputReader</u></b>
22 * <p>
23 * Reads the events of a trace file.
24 */
25 public class StreamInputReader {
26
27 // ------------------------------------------------------------------------
28 // Attributes
29 // ------------------------------------------------------------------------
30
31 /**
32 * The StreamInput we are reading.
33 */
34 private final StreamInput streamInput;
35
36 /**
37 * The packet reader used to read packets from this trace file.
38 */
39 private final StreamInputPacketReader packetReader;
40
41 /**
42 * Iterator on the packet index
43 */
44 private ListIterator<StreamInputPacketIndexEntry> packetIndexIt;
45
46 /**
47 * Reference to the current event of this trace file (iow, the last on that
48 * was read, the next one to be returned)
49 */
50 private EventDefinition currentEvent = null;
51
52 private int name;
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
58 /**
59 * Constructs a StreamInputReader that reads a StreamInput.
60 *
61 * @param streamInput
62 * The StreamInput to read.
63 */
64 public StreamInputReader(StreamInput streamInput) {
65 this.streamInput = streamInput;
66 this.packetReader = new StreamInputPacketReader(this);
67
68 /*
69 * Get the iterator on the packet index.
70 */
71 this.packetIndexIt = streamInput.getIndex().listIterator();
72
73 /*
74 * Make first packet the current one.
75 */
76 goToNextPacket();
77 }
78
79 // ------------------------------------------------------------------------
80 // Getters/Setters/Predicates
81 // ------------------------------------------------------------------------
82
83 public EventDefinition getCurrentEvent() {
84 return this.currentEvent;
85 }
86
87 public StructDefinition getCurrentPacketContext() {
88 return this.packetReader.getStreamPacketContextDef();
89 }
90
91 public StreamInput getStreamInput() {
92 return this.streamInput;
93 }
94
95 public int getName() {
96 return this.name;
97 }
98
99 public void setName(int name) {
100 this.name = name;
101 }
102
103 public int getCPU() {
104 return this.packetReader.getCPU();
105 }
106
107 // ------------------------------------------------------------------------
108 // Operations
109 // ------------------------------------------------------------------------
110 /**
111 * Reads the next event in the current event variable.
112 *
113 * @return If an event has been successfully read.
114 */
115 public boolean readNextEvent() {
116 /*
117 * Change packet if needed
118 */
119 if (!this.packetReader.hasMoreEvents()) {
120 goToNextPacket();
121 }
122
123 /*
124 * If an event is available, read it.
125 */
126 if (this.packetReader.hasMoreEvents()) {
127 try {
128 this.setCurrentEvent(this.packetReader.readNextEvent());
129 } catch (CTFReaderException e) {
130 /* Some problem happened, we'll assume there is no more events */
131 return false;
132 }
133 return true;
134 }
135 this.setCurrentEvent(null);
136 return false;
137 }
138
139 /**
140 * Change the current packet of the packet reader to the next one.
141 */
142 private void goToNextPacket() {
143 if (this.packetIndexIt.hasNext()) {
144 this.packetReader.setCurrentPacket(this.packetIndexIt.next());
145 } else {
146 this.packetReader.setCurrentPacket(null);
147 }
148 }
149
150 /**
151 * Changes the location of the trace file reader so that the current event
152 * is the first event with a timestamp greater than the given timestamp.
153 *
154 * @param timestamp
155 * The timestamp to seek to.
156 */
157 public void seek(long timestamp) {
158 /*
159 * Search in the index for the packet to search in.
160 */
161 this.packetIndexIt = this.streamInput.getIndex().search(timestamp);
162
163 /*
164 * Switch to this packet.
165 */
166 goToNextPacket();
167
168 /*
169 * Advance until A. we reached the end of the trace file (which means
170 * the given timestamp is after the last event), or B. we found the
171 * first event with a timestamp greater than the given timestamp.
172 */
173 readNextEvent();
174 boolean done = (this.getCurrentEvent() == null);
175 while (!done && (this.getCurrentEvent().timestamp < timestamp)) {
176 readNextEvent();
177 done = (this.getCurrentEvent() == null);
178 }
179 }
180
181 public void goToLastEvent() throws CTFReaderException {
182 /*
183 * Search in the index for the packet to search in.
184 */
185 this.packetIndexIt = this.streamInput.getIndex().search(Long.MAX_VALUE);
186 /*
187 * Go until the end of that packet
188 */
189 while (this.packetReader.hasMoreEvents()) {
190 this.packetReader.readNextEvent();
191 }
192 }
193
194 public void setCurrentEvent(EventDefinition currentEvent) {
195 this.currentEvent = currentEvent;
196 }
197
198 }
This page took 0.037256 seconds and 6 git commands to generate.