trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/InternalFileHandler.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  * Internal Trace File Handler
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine;
       
    20 
       
    21 import java.io.FileNotFoundException;
       
    22 import java.io.IOException;
       
    23 import java.io.RandomAccessFile;
       
    24 import java.nio.channels.FileChannel;
       
    25 
       
    26 /**
       
    27  * Internal Trace File Handler
       
    28  * 
       
    29  */
       
    30 final class InternalFileHandler implements TraceFileHandler {
       
    31 
       
    32 	/**
       
    33 	 * File path
       
    34 	 */
       
    35 	private final String filePath;
       
    36 
       
    37 	/**
       
    38 	 * Trace File
       
    39 	 */
       
    40 	private RandomAccessFile file;
       
    41 
       
    42 	/**
       
    43 	 * Constructor
       
    44 	 * 
       
    45 	 * @param filePath
       
    46 	 *            trace file path
       
    47 	 */
       
    48 	public InternalFileHandler(String filePath) {
       
    49 		this.filePath = filePath;
       
    50 	}
       
    51 
       
    52 	/*
       
    53 	 * (non-Javadoc)
       
    54 	 * 
       
    55 	 * @see com.nokia.traceviewer.engine.TraceFileHandler#clearFile()
       
    56 	 */
       
    57 	public void clearFile() {
       
    58 		if (file != null) {
       
    59 			try {
       
    60 				file.setLength(0);
       
    61 			} catch (IOException e) {
       
    62 				e.printStackTrace();
       
    63 			}
       
    64 		}
       
    65 	}
       
    66 
       
    67 	/*
       
    68 	 * (non-Javadoc)
       
    69 	 * 
       
    70 	 * @see com.nokia.traceviewer.engine.TraceFileHandler#openFile()
       
    71 	 */
       
    72 	public void openFile() {
       
    73 		if (filePath != null) {
       
    74 			// Create new RandomAccessFile
       
    75 			try {
       
    76 				if (getChannel() == null || !getChannel().isOpen()) {
       
    77 					this.file = new RandomAccessFile(filePath, "rw"); //$NON-NLS-1$
       
    78 				}
       
    79 			} catch (FileNotFoundException e) {
       
    80 				e.printStackTrace();
       
    81 			}
       
    82 		}
       
    83 	}
       
    84 
       
    85 	/*
       
    86 	 * (non-Javadoc)
       
    87 	 * 
       
    88 	 * @see com.nokia.traceviewer.engine.TraceFileHandler#closeFile()
       
    89 	 */
       
    90 	public void closeFile() {
       
    91 		if (file != null) {
       
    92 			try {
       
    93 				file.close();
       
    94 			} catch (IOException e) {
       
    95 				e.printStackTrace();
       
    96 			}
       
    97 		}
       
    98 	}
       
    99 
       
   100 	/**
       
   101 	 * Gets file channel
       
   102 	 * 
       
   103 	 * @return file channel
       
   104 	 */
       
   105 	private FileChannel getChannel() {
       
   106 		FileChannel channel = null;
       
   107 		if (file != null) {
       
   108 			channel = file.getChannel();
       
   109 		}
       
   110 		return channel;
       
   111 	}
       
   112 }