9452daf9642d34f85b39b96bf64eec917096b4ff
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFStreamOutputWriter.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.trace;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.File;
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;
26
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;
32
33 /**
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.
36 *
37 * @author Bernd Hufmann
38 * @since 1.0
39 */
40 public class CTFStreamOutputWriter {
41
42 // ------------------------------------------------------------------------
43 // Attributes
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;
50
51 // ------------------------------------------------------------------------
52 // Constructors
53 // ------------------------------------------------------------------------
54
55 /**
56 * Constructs a StreamInput.
57 *
58 * @param streamInput
59 * The stream to which this StreamInput belongs to.
60 * @param file
61 * The output trace directory.
62 * @throws CTFException
63 * If a reading or writing error occurs
64 */
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);
69
70 try {
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$
74 }
75
76 fStreamPacketOutputWriter = new CTFStreamPacketOutputWriter();
77 }
78
79 /**
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
83 * stream:
84 *
85 * startTime <= packet.getTimestampBegin() <= endTime
86 *
87 * @param startTime
88 * the start time for packets to be written
89 * @param endTime
90 * the end time for packets to be written
91 * @throws CTFException
92 * if a reading or writing error occurs
93 * @since 1.0
94 */
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$
99 }
100
101 try (FileChannel fc = checkNotNull(FileChannel.open(fOutFile.toPath(), StandardOpenOption.WRITE))) {
102 StreamInputPacketIndex index = streamInput.getIndex();
103 int count = 0;
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);
110 count++;
111 }
112 }
113 }
114
115 // If no packet was written delete the channel file
116 if (count == 0) {
117 if (fOutFile.exists()) {
118 fOutFile.delete();
119 }
120 }
121 } catch (IOException e) {
122 throw new CTFIOException("Error copying packets: " + e.toString(), e); //$NON-NLS-1$
123 }
124 }
125
126 /**
127 * Get the stream file to write.
128 *
129 * @return the stream file to write
130 */
131 public File getOutFile() {
132 return fOutFile;
133 }
134
135 }
This page took 0.03704 seconds and 4 git commands to generate.