ctf: clean up CTFStreamOutputWriter.java
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFStreamOutputWriter.java
CommitLineData
51173fa1
BH
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
13package org.eclipse.tracecompass.ctf.core.trace;
14
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17import java.io.File;
18import java.io.IOException;
19import java.nio.ByteBuffer;
20import java.nio.channels.FileChannel;
21import java.nio.channels.FileChannel.MapMode;
22import java.nio.file.FileSystems;
23import java.nio.file.Files;
24import java.nio.file.Path;
25import java.nio.file.StandardOpenOption;
26
27import org.eclipse.jdt.annotation.NonNull;
28import org.eclipse.jdt.annotation.Nullable;
29import org.eclipse.tracecompass.ctf.core.CTFException;
30import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer;
31import 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 */
40public class CTFStreamOutputWriter {
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45 // Stream input when copying stream from an input
4b86b7b3
MK
46 // It is @Nullable for future implementations that doesn't use an input
47 // stream
48 @Nullable
49 private final CTFStreamInput fStreamInput;
50 @NonNull
51 private final CTFStreamPacketOutputWriter fStreamPacketOutputWriter;
52 @NonNull
53 private final File fOutFile;
51173fa1
BH
54
55 // ------------------------------------------------------------------------
56 // Constructors
57 // ------------------------------------------------------------------------
58
59 /**
60 * Constructs a StreamInput.
61 *
62 * @param streamInput
63 * The stream to which this StreamInput belongs to.
64 * @param file
65 * The output trace directory.
66 * @throws CTFException
4b86b7b3 67 * If a reading or writing error occurs
51173fa1
BH
68 */
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);
73
74 try {
75 fOutFile = checkNotNull(Files.createFile(outFilePath).toFile());
76 } catch (IOException e) {
4b86b7b3 77 throw new CTFIOException("Output file can't be created: " + outFilePath, e); //$NON-NLS-1$
51173fa1
BH
78 }
79
80 fStreamPacketOutputWriter = new CTFStreamPacketOutputWriter();
81 }
82
83 /**
84 * Copies packets from the relevant input this input stream to a
85 * corresponding output stream based on a given time range. The following
4b86b7b3 86 * condition has to be met so that a packet is written to the output stream:
51173fa1
BH
87 *
88 * startTime <= packet.getTimestampBegin() <= endTime
89 *
90 * @param startTime
91 * the start time for packets to be written
92 * @param endTime
93 * the end time for packets to be written
94 * @throws CTFException
4b86b7b3 95 * if a reading or writing error occurs
51173fa1
BH
96 * @since 1.0
97 */
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$
102 }
103
104 try (FileChannel fc = checkNotNull(FileChannel.open(fOutFile.toPath(), StandardOpenOption.WRITE))) {
105 StreamInputPacketIndex index = streamInput.getIndex();
106 int count = 0;
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);
113 count++;
114 }
115 }
116 }
117
4b86b7b3
MK
118 if (count == 0 && fOutFile.exists()) {
119 boolean deleteResult = fOutFile.delete();
120 if (!deleteResult) {
121 throw new CTFIOException("Could not delete " + fOutFile.getAbsolutePath()); //$NON-NLS-1$
122 }
51173fa1
BH
123 }
124 } catch (IOException e) {
125 throw new CTFIOException("Error copying packets: " + e.toString(), e); //$NON-NLS-1$
126 }
127 }
128
129 /**
130 * Get the stream file to write.
131 *
132 * @return the stream file to write
133 */
134 public File getOutFile() {
135 return fOutFile;
136 }
137
138}
This page took 0.031235 seconds and 5 git commands to generate.