trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/InternalFileHandler.java
author Jussi Ryoma <ext-jussi.s.ryoma@nokia.com>
Tue, 24 Aug 2010 14:01:48 +0300
changeset 16 72f198be1c1d
parent 11 5b9d4d8641ce
permissions -rw-r--r--
Crash Analyser Carbide Extension 1.4.0

/*
 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 * Internal Trace File Handler
 *
 */
package com.nokia.traceviewer.engine;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * Internal Trace File Handler
 * 
 */
final class InternalFileHandler implements TraceFileHandler {

	/**
	 * File path
	 */
	private final String filePath;

	/**
	 * Trace File
	 */
	private RandomAccessFile file;

	/**
	 * Constructor
	 * 
	 * @param filePath
	 *            trace file path
	 */
	public InternalFileHandler(String filePath) {
		this.filePath = filePath;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nokia.traceviewer.engine.TraceFileHandler#clearFile()
	 */
	public void clearFile() {
		if (file != null) {
			try {
				file.setLength(0);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nokia.traceviewer.engine.TraceFileHandler#openFile()
	 */
	public void openFile() {
		if (filePath != null) {
			// Create new RandomAccessFile
			try {
				if (getChannel() == null || !getChannel().isOpen()) {
					this.file = new RandomAccessFile(filePath, "rw"); //$NON-NLS-1$
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nokia.traceviewer.engine.TraceFileHandler#closeFile()
	 */
	public void closeFile() {
		if (file != null) {
			try {
				file.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Gets file channel
	 * 
	 * @return file channel
	 */
	private FileChannel getChannel() {
		FileChannel channel = null;
		if (file != null) {
			channel = file.getChannel();
		}
		return channel;
	}
}