*/
@Test
public void testGetCurrentPacketContext() {
- ICompositeDefinition result = fixture.getCurrentEvent().getPacketContext();
+ EventDefinition currentEvent = fixture.getCurrentEvent();
+ assertNotNull(currentEvent);
+ ICompositeDefinition result = currentEvent.getPacketContext();
assertNotNull(result);
}
public void testGoToLastEvent2() throws CTFException {
long timestamp = -1;
while (fixture.readNextEvent().equals(CTFResponse.OK)) {
- timestamp = fixture.getCurrentEvent().getTimestamp();
+ EventDefinition currentEvent = fixture.getCurrentEvent();
+ assertNotNull(currentEvent);
+ timestamp = currentEvent.getTimestamp();
}
long endTimestamp = goToEnd();
assertEquals(0, timestamp - endTimestamp);
private long goToEnd() throws CTFException {
fixture.goToLastEvent();
- return fixture.getCurrentEvent().getTimestamp();
+ EventDefinition currentEvent = fixture.getCurrentEvent();
+ assertNotNull(currentEvent);
+ return currentEvent.getTimestamp();
}
/**
package org.eclipse.tracecompass.ctf.core.trace;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
import java.io.File;
import java.io.IOException;
-import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
* @author Simon Marchi
* @since 2.0
*/
+@NonNullByDefault
public class CTFStreamInputReader implements AutoCloseable {
// ------------------------------------------------------------------------
/**
* The StreamInput we are reading.
*/
- private final @NonNull File fFile;
+ private final File fFile;
- private final @NonNull CTFStreamInput fStreamInput;
+ private final CTFStreamInput fStreamInput;
- private final FileChannel fFileChannel;
+ private final @Nullable FileChannel fFileChannel;
/**
* The packet reader used to read packets from this trace file.
* Reference to the current event of this trace file (iow, the last on that
* was read, the next one to be returned)
*/
- private EventDefinition fCurrentEvent = null;
+ private @Nullable EventDefinition fCurrentEvent = null;
private int fId;
* If the file cannot be opened
*/
public CTFStreamInputReader(CTFStreamInput streamInput) throws CTFException {
- if (streamInput == null) {
- throw new IllegalArgumentException("stream cannot be null"); //$NON-NLS-1$
- }
fStreamInput = streamInput;
fFile = fStreamInput.getFile();
try {
if (fFileChannel != null) {
fFileChannel.close();
}
- if (fPacketReader != null) {
- fPacketReader.close();
- }
+ fPacketReader.close();
}
// ------------------------------------------------------------------------
* @return the current event in the stream, null if the stream is
* finished/empty/malformed
*/
- public EventDefinition getCurrentEvent() {
+ public @Nullable EventDefinition getCurrentEvent() {
return fCurrentEvent;
}
- /**
- * Gets the byte order for a trace
- *
- * @return the trace byte order
- */
- public ByteOrder getByteOrder() {
- return fStreamInput.getStream().getTrace().getByteOrder();
- }
-
/**
* Gets the name of the stream (it's an id and a number)
*
* @return Unmodifiable set with the event definitions
*/
public Iterable<IEventDeclaration> getEventDeclarations() {
- return ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations());
+ return checkNotNull(ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations()));
}
/**
*
* @return the event context declaration of the stream
*/
- public StructDeclaration getStreamEventContextDecl() {
+ public @Nullable StructDeclaration getStreamEventContextDecl() {
return getStreamInput().getStream().getEventContextDecl();
}
* Change packet if needed
*/
if (!fPacketReader.hasMoreEvents()) {
- final ICTFPacketDescriptor prevPacket = fPacketReader
- .getCurrentPacket();
+ final ICTFPacketDescriptor prevPacket = fPacketReader.getCurrentPacket();
if (prevPacket != null || fLive) {
goToNextPacket();
}
* timestamp.
*/
readNextEvent();
- boolean done = (this.getCurrentEvent() == null);
- while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
+ EventDefinition currentEvent = getCurrentEvent();
+ while (currentEvent != null && (currentEvent.getTimestamp() < timestamp)) {
readNextEvent();
- done = (this.getCurrentEvent() == null);
+ currentEvent = getCurrentEvent();
offset++;
}
return offset;
* if an error occurs
*/
private void gotoPacket(long timestamp) throws CTFException {
- fPacketIndex = fStreamInput.getIndex().search(timestamp)
- .previousIndex();
+ fPacketIndex = fStreamInput.getIndex().search(timestamp).previousIndex();
/*
* Switch to this packet.
*/
* @param currentEvent
* the event to set
*/
- public void setCurrentEvent(EventDefinition currentEvent) {
+ public void setCurrentEvent(@Nullable EventDefinition currentEvent) {
fCurrentEvent = currentEvent;
}
return fPacketIndex;
}
- private ICTFPacketDescriptor getPacket() {
+ private @Nullable ICTFPacketDescriptor getPacket() {
+ if (getPacketIndex() >= fStreamInput.getIndex().size()) {
+ return null;
+ }
return fStreamInput.getIndex().getElement(getPacketIndex());
}
*
* @return the file channel
*/
+ @Nullable
FileChannel getFc() {
return fFileChannel;
}
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
@Override
public String toString() {
// this helps debugging
- return fId + ' ' + fCurrentEvent.toString();
+ return fId + ' ' + NonNullUtils.nullToEmptyString(fCurrentEvent);
}
}
package org.eclipse.tracecompass.ctf.core.trace;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
/**
* Vector of all the trace file readers.
*/
- private final List<CTFStreamInputReader> fStreamInputReaders =
- Collections.synchronizedList(new ArrayList<CTFStreamInputReader>());
+ private final List<CTFStreamInputReader> fStreamInputReaders = Collections.synchronizedList(new ArrayList<CTFStreamInputReader>());
/**
* Priority queue to order the trace file readers by timestamp.
*/
fStartTime = 0;
if (hasMoreEvents()) {
- fStartTime = getTopStream().getCurrentEvent().getTimestamp();
- setEndTime(fStartTime);
+ EventDefinition currentEvent = getTopStream().getCurrentEvent();
+ if (currentEvent != null) {
+ fStartTime = currentEvent.getTimestamp();
+ setEndTime(fStartTime);
+ }
}
}
/*
* Create a reader and add it to the group.
*/
- fStreamInputReaders.add(new CTFStreamInputReader(streamInput));
+ fStreamInputReaders.add(new CTFStreamInputReader(checkNotNull(streamInput)));
}
}
/*
* Create a reader.
*/
- CTFStreamInputReader streamInputReader = new CTFStreamInputReader(
- streamInput);
+ CTFStreamInputReader streamInputReader = new CTFStreamInputReader(checkNotNull(streamInput));
/*
* Add it to the group.
* Add it back in the queue.
*/
fPrio.add(top);
- final long topEnd = fTrace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
+ /*
+ * We're in OK, there's a guaranteed top#getCurrentEvent() unless another
+ * thread does something bad.
+ */
+ EventDefinition currentEvent = checkNotNull(top.getCurrentEvent());
+ final long topEnd = fTrace.timestampCyclesToNanos(currentEvent.getTimestamp());
setEndTime(Math.max(topEnd, getEndTime()));
fEventCountPerTraceFile[top.getName()]++;
-
- if (top.getCurrentEvent() != null) {
- fEndTime = Math.max(top.getCurrentEvent().getTimestamp(),
- fEndTime);
- }
+ fEndTime = Math.max(currentEvent.getTimestamp(), fEndTime);
break;
}
case WAIT: {
/*******************************************************************************
- * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2015 Ericsson, Ecole Polytechnique de Montreal and others
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
package org.eclipse.tracecompass.internal.ctf.core.trace;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
import java.io.Serializable;
import java.util.Comparator;
*/
@Override
public int compare(CTFStreamInputReader a, CTFStreamInputReader b) {
- EventDefinition event_a = a.getCurrentEvent();
- EventDefinition event_b = b.getCurrentEvent();
+ EventDefinition eventA = checkNotNull(a.getCurrentEvent());
+ EventDefinition eventB = checkNotNull(b.getCurrentEvent());
- long ta = event_a.getTimestamp();
- long tb = event_b.getTimestamp();
+ long ta = eventA.getTimestamp();
+ long tb = eventB.getTimestamp();
return Utils.unsignedCompare(ta, tb);
}