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
;
21 * <b><u>StreamInputReader</u></b>
23 * Reads the events of a trace file.
25 public class StreamInputReader
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
32 * The StreamInput we are reading.
34 private final StreamInput streamInput
;
37 * The packet reader used to read packets from this trace file.
39 private final StreamInputPacketReader packetReader
;
42 * Iterator on the packet index
44 private ListIterator
<StreamInputPacketIndexEntry
> packetIndexIt
;
47 * Reference to the current event of this trace file (iow, the last on that
48 * was read, the next one to be returned)
50 private EventDefinition currentEvent
= null;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * Constructs a StreamInputReader that reads a StreamInput.
62 * The StreamInput to read.
64 public StreamInputReader(StreamInput streamInput
) {
65 this.streamInput
= streamInput
;
66 this.packetReader
= new StreamInputPacketReader(this);
69 * Get the iterator on the packet index.
71 this.packetIndexIt
= streamInput
.getIndex().listIterator();
74 * Make first packet the current one.
79 // ------------------------------------------------------------------------
80 // Getters/Setters/Predicates
81 // ------------------------------------------------------------------------
83 public EventDefinition
getCurrentEvent() {
84 return this.currentEvent
;
87 public StructDefinition
getCurrentPacketContext() {
88 return this.packetReader
.getStreamPacketContextDef();
91 public StreamInput
getStreamInput() {
92 return this.streamInput
;
95 public int getName() {
99 public void setName(int name
) {
103 public int getCPU() {
104 return this.packetReader
.getCPU();
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
111 * Reads the next event in the current event variable.
113 * @return If an event has been successfully read.
115 public boolean readNextEvent() {
117 * Change packet if needed
119 if (!this.packetReader
.hasMoreEvents()) {
124 * If an event is available, read it.
126 if (this.packetReader
.hasMoreEvents()) {
128 this.setCurrentEvent(this.packetReader
.readNextEvent());
129 } catch (CTFReaderException e
) {
130 /* Some problem happened, we'll assume there is no more events */
135 this.setCurrentEvent(null);
140 * Change the current packet of the packet reader to the next one.
142 private void goToNextPacket() {
143 if (this.packetIndexIt
.hasNext()) {
144 this.packetReader
.setCurrentPacket(this.packetIndexIt
.next());
146 this.packetReader
.setCurrentPacket(null);
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.
155 * The timestamp to seek to.
157 public void seek(long timestamp
) {
159 * Search in the index for the packet to search in.
161 this.packetIndexIt
= this.streamInput
.getIndex().search(timestamp
);
164 * Switch to this packet.
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.
174 boolean done
= (this.getCurrentEvent() == null);
175 while (!done
&& (this.getCurrentEvent().timestamp
< timestamp
)) {
177 done
= (this.getCurrentEvent() == null);
181 public void goToLastEvent() throws CTFReaderException
{
183 * Search in the index for the packet to search in.
185 this.packetIndexIt
= this.streamInput
.getIndex().search(Long
.MAX_VALUE
);
187 * Go until the end of that packet
189 while (this.packetReader
.hasMoreEvents()) {
190 this.packetReader
.readNextEvent();
194 public void setCurrentEvent(EventDefinition currentEvent
) {
195 this.currentEvent
= currentEvent
;