1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made 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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.trace
;
15 import java
.io
.FileNotFoundException
;
16 import java
.io
.IOException
;
17 import java
.io
.RandomAccessFile
;
18 import java
.util
.concurrent
.locks
.ReentrantLock
;
20 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
22 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.parser
.ITmfEventParser
;
26 * <b><u>TmfTraceStub</u></b>
28 * Dummy test trace. Use in conjunction with TmfEventParserStub.
30 @SuppressWarnings("nls")
31 public class TmfTraceStub
extends TmfTrace
<TmfEvent
> {
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
38 private RandomAccessFile fTrace
;
40 // The associated event parser
41 private ITmfEventParser fParser
;
43 // The synchronization lock
44 private ReentrantLock fLock
= new ReentrantLock();
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
52 * @throws FileNotFoundException
54 public TmfTraceStub(String filename
) throws FileNotFoundException
{
55 this(filename
, DEFAULT_INDEX_PAGE_SIZE
, false);
61 * @throws FileNotFoundException
63 public TmfTraceStub(String filename
, int cacheSize
) throws FileNotFoundException
{
64 this(filename
, cacheSize
, false);
69 * @param waitForCompletion
70 * @throws FileNotFoundException
72 public TmfTraceStub(String filename
, boolean waitForCompletion
) throws FileNotFoundException
{
73 this(filename
, DEFAULT_INDEX_PAGE_SIZE
, waitForCompletion
);
79 * @param waitForCompletion
80 * @throws FileNotFoundException
82 public TmfTraceStub(String filename
, int cacheSize
, boolean waitForCompletion
) throws FileNotFoundException
{
83 super(null, TmfEvent
.class, filename
, cacheSize
, false);
84 fTrace
= new RandomAccessFile(filename
, "r");
85 fParser
= new TmfEventParserStub();
92 * @param waitForCompletion
94 * @throws FileNotFoundException
96 public TmfTraceStub(String filename
, int cacheSize
, boolean waitForCompletion
, ITmfEventParser parser
) throws FileNotFoundException
{
97 super(filename
, TmfEvent
.class, filename
, cacheSize
, false);
98 fTrace
= new RandomAccessFile(filename
, "r");
105 public TmfTraceStub
clone() {
106 TmfTraceStub clone
= null;
108 clone
= (TmfTraceStub
) super.clone();
109 clone
.fTrace
= new RandomAccessFile(getPath(), "r");
110 clone
.fParser
= new TmfEventParserStub();
111 } catch (CloneNotSupportedException e
) {
112 } catch (FileNotFoundException e
) {
118 public ITmfTrace
copy() {
119 ITmfTrace returnedValue
= null;
120 returnedValue
= clone();
121 return returnedValue
;
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 public RandomAccessFile
getStream() {
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
137 @SuppressWarnings("unchecked")
138 public TmfContext
seekLocation(ITmfLocation location
) {
141 if (fTrace
!= null) {
142 // Position the trace at the requested location and
143 // returns the corresponding context
146 if (location
!= null) {
147 loc
= ((TmfLocation
<Long
>) location
).getLocation();
148 rank
= ITmfContext
.UNKNOWN_RANK
;
150 if (loc
!= fTrace
.getFilePointer()) {
153 TmfContext context
= new TmfContext(getCurrentLocation(), rank
);
156 } catch (IOException e
) {
167 public TmfContext
seekLocation(double ratio
) {
170 if (fTrace
!= null) {
171 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long((long) (ratio
* fTrace
.length())));
172 TmfContext context
= seekLocation(location
);
173 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
176 } catch (IOException e
) {
186 @SuppressWarnings("rawtypes")
187 public double getLocationRatio(ITmfLocation location
) {
190 if (fTrace
!= null) {
191 if (location
.getLocation() instanceof Long
) {
192 return (double) ((Long
) location
.getLocation()) / fTrace
.length();
195 } catch (IOException e
) {
204 public TmfLocation
<Long
> getCurrentLocation() {
207 if (fTrace
!= null) {
208 return new TmfLocation
<Long
>(fTrace
.getFilePointer());
210 } catch (IOException e
) {
219 public TmfEvent
parseEvent(TmfContext context
) {
222 // parseNextEvent will update the context
223 if (fTrace
!= null) {
224 TmfEvent event
= fParser
.parseNextEvent(this, context
.clone());
228 catch (IOException e
) {
237 public void setTimeRange(TmfTimeRange range
) {
238 super.setTimeRange(range
);
242 public void setStartTime(TmfTimestamp startTime
) {
243 super.setStartTime(startTime
);
247 public void setEndTime(TmfTimestamp endTime
) {
248 super.setEndTime(endTime
);
252 public void dispose() {
255 if (fTrace
!= null) {
259 } catch (IOException e
) {