import java.io.File;
import java.io.FilenameFilter;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
fixture.setTimestampEnd(1L);
}
+ @NonNull
private static File createFile() {
File path = new File(testTrace.getPath());
- return path.listFiles(new FilenameFilter() {
+ final File[] listFiles = path.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.contains("hann")) {
}
return false;
}
- })[0];
+ });
+ assertNotNull(listFiles);
+ final File returnFile = listFiles[0];
+ assertNotNull(returnFile);
+ return returnFile;
}
/**
import java.io.IOException;
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
}
@After
- public void tearDown() throws IOException{
+ public void tearDown() throws IOException {
fInput.close();
}
+ @NonNull
private static File createFile() {
File path = new File(testTrace.getPath());
- return path.listFiles(new FilenameFilter() {
+ final File[] listFiles = path.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.contains("hann")) {
}
return false;
}
- })[0];
+ });
+ assertNotNull(listFiles);
+ final File returnFile = listFiles[0];
+ assertNotNull(returnFile);
+ return returnFile;
}
/**
package org.eclipse.linuxtools.ctf.core.trace;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.StandardOpenOption;
import java.util.UUID;
import org.eclipse.jdt.annotation.NonNull;
*
* @since 3.0
*/
+// TODO: remove AutoCloseable
public class CTFStreamInput implements IDefinitionScope, AutoCloseable {
// ------------------------------------------------------------------------
*/
private final CTFStream fStream;
- /**
- * FileChannel to the trace file
- */
- private final FileChannel fFileChannel;
-
/**
* Information on the file (used for debugging)
*/
+ @NonNull
private final File fFile;
/**
*/
private long fLostSoFar = 0;
- /**
- * File input stream, the parent input file
- */
- private final FileInputStream fFileInputStream;
-
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
* The stream to which this StreamInput belongs to.
* @param file
* Information about the trace file (for debugging purposes).
- * @throws CTFReaderException
- * The file must exist
*/
- public CTFStreamInput(CTFStream stream, File file) throws CTFReaderException {
+ public CTFStreamInput(CTFStream stream, @NonNull File file) {
fStream = stream;
fFile = file;
- try {
- fFileInputStream = new FileInputStream(file);
- } catch (FileNotFoundException e) {
- throw new CTFReaderException(e);
- }
-
- fFileChannel = fFileInputStream.getChannel();
fIndex = new StreamInputPacketIndex();
}
@Override
public void close() throws IOException {
- fFileChannel.close();
- fFileInputStream.close();
}
// ------------------------------------------------------------------------
StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry(
currentPos);
- createPacketIndexEntry(fileSize, currentPos, packetIndex,
- fTracePacketHeaderDecl, fStreamPacketContextDecl);
+ createPacketIndexEntry(fileSize, currentPos, packetIndex);
fIndex.addEntry(packetIndex);
return true;
}
return fFile.length();
}
- private long createPacketIndexEntry(long fileSizeBytes,
- long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
- StructDeclaration tracePacketHeaderDecl,
- StructDeclaration streamPacketContextDecl)
+ private long createPacketIndexEntry(long fileSizeBytes, long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex)
throws CTFReaderException {
- /*
- * create a packet bit buffer to read the packet header
- */
- BitBuffer bitBuffer = new BitBuffer(createPacketBitBuffer(fileSizeBytes, packetOffsetBytes, packetIndex));
- bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());
- /*
- * Read the trace packet header if it exists.
- */
- if (tracePacketHeaderDecl != null) {
- parseTracePacketHeader(tracePacketHeaderDecl, bitBuffer);
- }
-
- /*
- * Read the stream packet context if it exists.
- */
- if (streamPacketContextDecl != null) {
- parsePacketContext(fileSizeBytes, streamPacketContextDecl,
- bitBuffer, packetIndex);
- } else {
- setPacketContextNull(fileSizeBytes, packetIndex);
- }
+ long pos = readPacketHeader(fileSizeBytes, packetOffsetBytes, packetIndex);
/* Basic validation */
if (packetIndex.getContentSizeBits() > packetIndex.getPacketSizeBits()) {
/*
* Offset in the file, in bits
*/
- packetIndex.setDataOffsetBits(bitBuffer.position());
+ packetIndex.setDataOffsetBits(pos);
/*
* Update the counting packet offset
+ ((packetIndex.getPacketSizeBits() + 7) / 8);
}
- @NonNull
- ByteBuffer getByteBufferAt(long position, long size) throws CTFReaderException, IOException {
- MappedByteBuffer map = fFileChannel.map(MapMode.READ_ONLY, position, size);
- if (map == null) {
- throw new CTFReaderException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$
- }
- return map;
- }
-
- @NonNull
- private ByteBuffer createPacketBitBuffer(long fileSizeBytes,
+ private long readPacketHeader(long fileSizeBytes,
long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex) throws CTFReaderException {
+ long position = -1;
/*
* Initial size, it should map at least the packet header + context
* size.
/*
* Map the packet.
*/
- try {
- return getByteBufferAt(packetOffsetBytes, mapSize);
+ try (FileChannel fc = FileChannel.open(fFile.toPath(), StandardOpenOption.READ)) {
+ MappedByteBuffer map = fc.map(MapMode.READ_ONLY, packetOffsetBytes, mapSize);
+ if (map == null) {
+ throw new CTFReaderException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$
+ }
+ /*
+ * create a packet bit buffer to read the packet header
+ */
+ BitBuffer bitBuffer = new BitBuffer(map);
+ bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());
+ /*
+ * Read the trace packet header if it exists.
+ */
+ if (fTracePacketHeaderDecl != null) {
+ parseTracePacketHeader(fTracePacketHeaderDecl, bitBuffer);
+ }
+
+ /*
+ * Read the stream packet context if it exists.
+ */
+ if (fStreamPacketContextDecl != null) {
+ parsePacketContext(fileSizeBytes, fStreamPacketContextDecl,
+ bitBuffer, packetIndex);
+ } else {
+ setPacketContextNull(fileSizeBytes, packetIndex);
+ }
+
+ position = bitBuffer.position();
} catch (IOException e) {
throw new CTFReaderException(e);
}
+ return position;
}
private void parseTracePacketHeader(StructDeclaration tracePacketHeaderDecl,
}
}
+ /**
+ * Gets the wrapped file
+ *
+ * @return the file
+ */
+ @NonNull
+ File getFile() {
+ return fFile;
+ }
+
private static void setPacketContextNull(long fileSizeBytes,
StreamInputPacketIndexEntry packetIndex) {
/*
public int hashCode() {
final int prime = 31;
int result = 1;
- result = (prime * result) + ((fFile == null) ? 0 : fFile.hashCode());
+ result = (prime * result) + fFile.hashCode();
return result;
}
return false;
}
CTFStreamInput other = (CTFStreamInput) obj;
- if (fFile == null) {
- if (other.fFile != null) {
- return false;
- }
- } else if (!fFile.equals(other.fFile)) {
+ if (!fFile.equals(other.fFile)) {
return false;
}
return true;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel.MapMode;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
// Operations
// ------------------------------------------------------------------------
+ @NonNull
+ private ByteBuffer getByteBufferAt(long position, long size) throws CTFReaderException, IOException {
+ MappedByteBuffer map = fStreamInputReader.getFc().map(MapMode.READ_ONLY, position, size);
+ if (map == null) {
+ throw new CTFReaderException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$
+ }
+ return map;
+ }
/**
* Changes the current packet to the given one.
*
*/
ByteBuffer bb = null;
try {
- bb = fStreamInputReader.getStreamInput().getByteBufferAt(
+ bb = getByteBufferAt(
fCurrentPacket.getOffsetBytes(),
(fCurrentPacket.getPacketSizeBits() + 7) / 8);
} catch (IOException e) {
package org.eclipse.linuxtools.ctf.core.trace;
+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.linuxtools.ctf.core.event.EventDefinition;
import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
+import org.eclipse.linuxtools.internal.ctf.core.Activator;
import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
import com.google.common.collect.ImmutableList;
/**
* The StreamInput we are reading.
*/
+ private final @NonNull File fFile;
+
private final @NonNull CTFStreamInput fStreamInput;
+ private final FileChannel fFileChannel;
+
/**
* The packet reader used to read packets from this trace file.
*/
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
-
/**
* Constructs a StreamInputReader that reads a StreamInput.
*
* @param streamInput
* The StreamInput to read.
* @throws CTFReaderException
- * if an error occurs
+ * If the file cannot be opened
*/
public CTFStreamInputReader(CTFStreamInput streamInput) throws CTFReaderException {
if (streamInput == null) {
- throw new IllegalArgumentException("streamInput cannot be null"); //$NON-NLS-1$
+ throw new IllegalArgumentException("stream cannot be null"); //$NON-NLS-1$
}
fStreamInput = streamInput;
+ fFile = fStreamInput.getFile();
+ try {
+ fFileChannel = FileChannel.open(fFile.toPath(), StandardOpenOption.READ);
+ } catch (IOException e) {
+ throw new CTFReaderException(e);
+ }
fPacketReader = new CTFStreamInputPacketReader(this);
/*
* Get the iterator on the packet index.
}
/**
- * Dispose the StreamInputReader
+ * Dispose the StreamInputReader, closes the file channel and its packet
+ * reader
+ *
+ * @throws IOException
+ * If an I/O error occurs
*/
@Override
- public void close() {
+ public void close() throws IOException {
+ fFileChannel.close();
fPacketReader.close();
}
goToNextPacket();
} catch (CTFReaderException e) {
// do nothing here
+ Activator.log(e.getMessage());
}
}
if (fPacketReader.getCurrentPacket() == null) {
return fStreamInput.getIndex().getEntries().get(getPacketIndex());
}
+ /**
+ * Get the file channel wrapped by this reader
+ *
+ * @return the file channel
+ */
+ FileChannel getFc() {
+ return fFileChannel;
+ }
+
/**
* @return the packetReader
*/
int result = 1;
result = (prime * result) + fId;
result = (prime * result)
- + fStreamInput.hashCode();
+ + fFile.hashCode();
return result;
}
if (fId != other.fId) {
return false;
}
- return fStreamInput.equals(other.fStreamInput);
+ return fFile.equals(other.fFile);
}
@Override
// this helps debugging
return fId + ' ' + fCurrentEvent.toString();
}
+
}
return getStream(streamId).getEventDeclaration((int) id);
}
-
/**
* Get an event by it's ID
*
return (fPath != null) ? fPath.getPath() : ""; //$NON-NLS-1$
}
-
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
FileChannel fc = fis.getChannel()) {
/* Map one memory page of 4 kiB */
byteBuffer = fc.map(MapMode.READ_ONLY, 0, (int) Math.min(fc.size(), 4096L));
- if( byteBuffer == null){
+ if (byteBuffer == null) {
throw new IllegalStateException("Failed to allocate memory"); //$NON-NLS-1$
}
} catch (IOException e) {
/**
* Gets the current first packet start time
+ *
* @return the current start time
* @since 3.0
*/
/**
* Gets the current last packet end time
+ *
* @return the current end time
* @since 3.0
*/
* The file must exist
* @since 3.0
*/
+ // TODO: remove suppress warning
+ @SuppressWarnings("resource")
public void addStream(long id, File streamFile) throws CTFReaderException {
CTFStream stream = null;
+ final File file = streamFile;
+ if (file == null) {
+ throw new CTFReaderException("cannot create a stream with no file"); //$NON-NLS-1$
+ }
if (fStreams.containsKey(id)) {
stream = fStreams.get(id);
} else {
stream = new CTFStream(this);
fStreams.put(id, stream);
}
- stream.addInput(new CTFStreamInput(stream, streamFile));
+ stream.addInput(new CTFStreamInput(stream, file));
}
}
package org.eclipse.linuxtools.ctf.core.trace;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public void close() {
for (CTFStreamInputReader reader : fStreamInputReaders) {
if (reader != null) {
- reader.close();
+ try {
+ reader.close();
+ } catch (IOException e) {
+ Activator.logError(e.getMessage(), e);
+ }
}
}
fStreamInputReaders.clear();