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 stream
47 @Nullable private final CTFStreamInput fStreamInput
;
48 @NonNull private final CTFStreamPacketOutputWriter fStreamPacketOutputWriter
;
49 @NonNull private final File fOutFile
;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Constructs a StreamInput.
59 * The stream to which this StreamInput belongs to.
61 * The output trace directory.
62 * @throws CTFException
63 * If a reading or writing error occurs
65 public CTFStreamOutputWriter(@NonNull CTFStreamInput streamInput
, @NonNull File file
) throws CTFException
{
66 fStreamInput
= streamInput
;
67 String inFileName
= streamInput
.getFile().getName();
68 Path outFilePath
= FileSystems
.getDefault().getPath(file
.getAbsolutePath(), inFileName
);
71 fOutFile
= checkNotNull(Files
.createFile(outFilePath
).toFile());
72 } catch (IOException e
) {
73 throw new CTFIOException("Output file can't be created: " + outFilePath
); //$NON-NLS-1$
76 fStreamPacketOutputWriter
= new CTFStreamPacketOutputWriter();
80 * Copies packets from the relevant input this input stream to a
81 * corresponding output stream based on a given time range. The following
82 * condition has to be met so that a packet is written to the output
85 * startTime <= packet.getTimestampBegin() <= endTime
88 * the start time for packets to be written
90 * the end time for packets to be written
91 * @throws CTFException
92 * if a reading or writing error occurs
95 public void copyPackets(long startTime
, long endTime
) throws CTFException
{
96 CTFStreamInput streamInput
= fStreamInput
;
97 if (streamInput
== null) {
98 throw new CTFIOException("StreamInput is null. Can't copy packets"); //$NON-NLS-1$
101 try (FileChannel fc
= checkNotNull(FileChannel
.open(fOutFile
.toPath(), StandardOpenOption
.WRITE
))) {
102 StreamInputPacketIndex index
= streamInput
.getIndex();
104 try (FileChannel source
= FileChannel
.open(streamInput
.getFile().toPath(), StandardOpenOption
.READ
)) {
105 for (int i
= 0; i
< index
.size(); i
++) {
106 ICTFPacketDescriptor entry
= index
.getElement(i
);
107 if ((entry
.getTimestampBegin() >= startTime
) && (entry
.getTimestampBegin() <= endTime
)) {
108 ByteBuffer buffer
= SafeMappedByteBuffer
.map(source
, MapMode
.READ_ONLY
, entry
.getOffsetBytes(), entry
.getPacketSizeBits() / Byte
.SIZE
);
109 fStreamPacketOutputWriter
.writePacket(buffer
, fc
);
115 // If no packet was written delete the channel file
117 if (fOutFile
.exists()) {
121 } catch (IOException e
) {
122 throw new CTFIOException("Error copying packets: " + e
.toString(), e
); //$NON-NLS-1$
127 * Get the stream file to write.
129 * @return the stream file to write
131 public File
getOutFile() {