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
;
19 import java
.nio
.ByteBuffer
;
20 import java
.nio
.channels
.FileChannel
;
21 import java
.nio
.channels
.FileChannel
.MapMode
;
22 import java
.nio
.file
.FileSystems
;
23 import java
.nio
.file
.Files
;
24 import java
.nio
.file
.Path
;
25 import java
.nio
.file
.StandardOpenOption
;
27 import org
.eclipse
.jdt
.annotation
.NonNull
;
28 import org
.eclipse
.jdt
.annotation
.Nullable
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
30 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.SafeMappedByteBuffer
;
31 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
.StreamInputPacketIndex
;
34 * A CTF Stream output writer. Reads the packets of a given CTFStreamInput and
35 * writes packets that are within a given time range to output stream file.
37 * @author Bernd Hufmann
40 public class CTFStreamOutputWriter
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
45 // Stream input when copying stream from an input
46 // It is @Nullable for future implementations that doesn't use an input
49 private final CTFStreamInput fStreamInput
;
51 private final CTFStreamPacketOutputWriter fStreamPacketOutputWriter
;
53 private final File fOutFile
;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * Constructs a StreamInput.
63 * The stream to which this StreamInput belongs to.
65 * The output trace directory.
66 * @throws CTFException
67 * If a reading or writing error occurs
69 public CTFStreamOutputWriter(@NonNull CTFStreamInput streamInput
, @NonNull File file
) throws CTFException
{
70 fStreamInput
= streamInput
;
71 String inFileName
= streamInput
.getFile().getName();
72 Path outFilePath
= FileSystems
.getDefault().getPath(file
.getAbsolutePath(), inFileName
);
75 fOutFile
= checkNotNull(Files
.createFile(outFilePath
).toFile());
76 } catch (IOException e
) {
77 throw new CTFIOException("Output file can't be created: " + outFilePath
, e
); //$NON-NLS-1$
80 fStreamPacketOutputWriter
= new CTFStreamPacketOutputWriter();
84 * Copies packets from the relevant input this input stream to a
85 * corresponding output stream based on a given time range. The following
86 * condition has to be met so that a packet is written to the output stream:
88 * startTime <= packet.getTimestampBegin() <= endTime
91 * the start time for packets to be written
93 * the end time for packets to be written
94 * @throws CTFException
95 * if a reading or writing error occurs
98 public void copyPackets(long startTime
, long endTime
) throws CTFException
{
99 CTFStreamInput streamInput
= fStreamInput
;
100 if (streamInput
== null) {
101 throw new CTFIOException("StreamInput is null. Can't copy packets"); //$NON-NLS-1$
104 try (FileChannel fc
= checkNotNull(FileChannel
.open(fOutFile
.toPath(), StandardOpenOption
.WRITE
))) {
105 StreamInputPacketIndex index
= streamInput
.getIndex();
107 try (FileChannel source
= FileChannel
.open(streamInput
.getFile().toPath(), StandardOpenOption
.READ
)) {
108 for (int i
= 0; i
< index
.size(); i
++) {
109 ICTFPacketDescriptor entry
= index
.getElement(i
);
110 if ((entry
.getTimestampBegin() >= startTime
) && (entry
.getTimestampBegin() <= endTime
)) {
111 ByteBuffer buffer
= SafeMappedByteBuffer
.map(source
, MapMode
.READ_ONLY
, entry
.getOffsetBytes(), entry
.getPacketSizeBits() / Byte
.SIZE
);
112 fStreamPacketOutputWriter
.writePacket(buffer
, fc
);
118 if (count
== 0 && fOutFile
.exists()) {
119 boolean deleteResult
= fOutFile
.delete();
121 throw new CTFIOException("Could not delete " + fOutFile
.getAbsolutePath()); //$NON-NLS-1$
124 } catch (IOException e
) {
125 throw new CTFIOException("Error copying packets: " + e
.toString(), e
); //$NON-NLS-1$
130 * Get the stream file to write.
132 * @return the stream file to write
134 public File
getOutFile() {