ctf: clean up CTFStreamOutputWriter.java
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 30 Apr 2015 00:59:07 +0000 (20:59 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 30 Apr 2015 14:55:58 +0000 (10:55 -0400)
Merge if statements
Preserve exceptions
Throw exception on deletion failure as the trace would be unreadable

Change-Id: I77f23e5bca82be26b831a38ea5add385586e20f5
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/46831
Reviewed-by: Hudson CI
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/trace/CTFStreamOutputWriter.java

index 9452daf9642d34f85b39b96bf64eec917096b4ff..3e05b4c62bf366d6cf156ef781231e1a75d0a240 100644 (file)
@@ -43,10 +43,14 @@ public class CTFStreamOutputWriter {
     // Attributes
     // ------------------------------------------------------------------------
     // Stream input when copying stream from an input
-    // It is @Nullable for future implementations that doesn't use an input stream
-    @Nullable private final CTFStreamInput fStreamInput;
-    @NonNull private final CTFStreamPacketOutputWriter fStreamPacketOutputWriter;
-    @NonNull private final File fOutFile;
+    // It is @Nullable for future implementations that doesn't use an input
+    // stream
+    @Nullable
+    private final CTFStreamInput fStreamInput;
+    @NonNull
+    private final CTFStreamPacketOutputWriter fStreamPacketOutputWriter;
+    @NonNull
+    private final File fOutFile;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -60,7 +64,7 @@ public class CTFStreamOutputWriter {
      * @param file
      *            The output trace directory.
      * @throws CTFException
-     *            If a reading or writing error occurs
+     *             If a reading or writing error occurs
      */
     public CTFStreamOutputWriter(@NonNull CTFStreamInput streamInput, @NonNull File file) throws CTFException {
         fStreamInput = streamInput;
@@ -70,7 +74,7 @@ public class CTFStreamOutputWriter {
         try {
             fOutFile = checkNotNull(Files.createFile(outFilePath).toFile());
         } catch (IOException e) {
-            throw new CTFIOException("Output file can't be created: " + outFilePath); //$NON-NLS-1$
+            throw new CTFIOException("Output file can't be created: " + outFilePath, e); //$NON-NLS-1$
         }
 
         fStreamPacketOutputWriter = new CTFStreamPacketOutputWriter();
@@ -79,8 +83,7 @@ public class CTFStreamOutputWriter {
     /**
      * Copies packets from the relevant input this input stream to a
      * corresponding output stream based on a given time range. The following
-     * condition has to be met so that a packet is written to the output
-     * stream:
+     * condition has to be met so that a packet is written to the output stream:
      *
      * startTime <= packet.getTimestampBegin() <= endTime
      *
@@ -89,7 +92,7 @@ public class CTFStreamOutputWriter {
      * @param endTime
      *            the end time for packets to be written
      * @throws CTFException
-     *            if a reading or writing error occurs
+     *             if a reading or writing error occurs
      * @since 1.0
      */
     public void copyPackets(long startTime, long endTime) throws CTFException {
@@ -112,11 +115,11 @@ public class CTFStreamOutputWriter {
                 }
             }
 
-            // If no packet was written delete the channel file
-            if (count == 0) {
-               if (fOutFile.exists()) {
-                  fOutFile.delete();
-               }
+            if (count == 0 && fOutFile.exists()) {
+                boolean deleteResult = fOutFile.delete();
+                if (!deleteResult) {
+                    throw new CTFIOException("Could not delete " + fOutFile.getAbsolutePath()); //$NON-NLS-1$
+                }
             }
         } catch (IOException e) {
             throw new CTFIOException("Error copying packets: " + e.toString(), e); //$NON-NLS-1$
This page took 0.027643 seconds and 5 git commands to generate.