1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.trace
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.io
.IOException
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
26 * A CTF trace reader. Reads the events of a trace.
28 * @author Bernd Hufmann
31 public class CTFTraceWriter
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
38 * The trace to read from.
40 @Nullable private final CTFTrace fInTrace
;
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * Constructs a TraceReader to read a trace.
50 * The trace to read from.
51 * @throws CTFException
54 public CTFTraceWriter(@NonNull CTFTrace trace
) throws CTFException
{
56 try (CTFTraceReader fTraceReader
= new CTFTraceReader(fInTrace
)) {
57 fTraceReader
.populateIndex();
62 * Copies packets from the relevant input to the output trace based
63 * on a given time range. The following condition has to be met so that
64 * a packet is written to the output trace:
66 * startTime <= packet.getTimestampBegin() <= endTime
69 * start time of packets to be included in output trace
71 * end time of packets to be included in the output trace
73 * the path of the new trace to be written
74 * @throws CTFException
75 * If a reading or writing error occurs
77 public void copyPackets(long startTime
, long endTime
, String newTracePath
) throws CTFException
{
78 CTFTrace trace
= fInTrace
;
80 long adjustedStart
= startTime
- trace
.getClock().getClockOffset();
81 long adjustedEnd
= endTime
- trace
.getClock().getClockOffset();
82 File out
= new File(newTracePath
);
84 throw new CTFIOException("Trace segment cannot be created since trace already exists: " + newTracePath
); //$NON-NLS-1$
87 // create new directory
88 boolean isSuccess
= out
.mkdir();
90 throw new CTFIOException("Creating trace directory failed: " + newTracePath
); //$NON-NLS-1$
94 Metadata metadata
= new Metadata(fInTrace
);
97 } catch (IOException e
) {
98 throw new CTFIOException("Error copying metadata: " + e
.toString(), e
); //$NON-NLS-1$
102 for (CTFStream stream
: trace
.getStreams()) {
103 Set
<CTFStreamInput
> inputs
= stream
.getStreamInputs();
104 for (CTFStreamInput s
: inputs
) {
105 CTFStreamOutputWriter streamOutputwriter
= new CTFStreamOutputWriter(checkNotNull(s
), out
);
106 streamOutputwriter
.copyPackets(adjustedStart
, adjustedEnd
);