[Bug304438] Improvement on ITmfLocation
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.trace;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.RandomAccessFile;
18
19 import org.eclipse.linuxtools.tmf.event.TmfEvent;
20 import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
21
22 /**
23 * <b><u>TmfTraceStub</u></b>
24 * <p>
25 * Dummy test trace. Use in conjunction with TmfEventParserStub.
26 */
27 public class TmfTraceStub extends TmfTrace<TmfEvent> {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 // The actual stream
34 private final RandomAccessFile fTrace;
35
36 // The associated event parser
37 private final ITmfEventParser fParser;
38
39 // ------------------------------------------------------------------------
40 // Constructors
41 // ------------------------------------------------------------------------
42
43 /**
44 * @param filename
45 * @throws FileNotFoundException
46 */
47 public TmfTraceStub(String filename) throws FileNotFoundException {
48 this(filename, DEFAULT_CACHE_SIZE, false);
49 }
50
51 /**
52 * @param filename
53 * @param cacheSize
54 * @throws FileNotFoundException
55 */
56 public TmfTraceStub(String filename, int cacheSize) throws FileNotFoundException {
57 this(filename, cacheSize, false);
58 }
59
60 /**
61 * @param filename
62 * @throws FileNotFoundException
63 */
64 public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
65 this(filename, DEFAULT_CACHE_SIZE, waitForCompletion);
66 }
67
68 /**
69 * @param filename
70 * @param cacheSize
71 * @throws FileNotFoundException
72 */
73 public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
74 super(TmfEvent.class, filename, cacheSize);
75 fTrace = new RandomAccessFile(filename, "r");
76 fParser = new TmfEventParserStub();
77 indexTrace(waitForCompletion);
78 }
79
80 // ------------------------------------------------------------------------
81 // Accessors
82 // ------------------------------------------------------------------------
83
84 public RandomAccessFile getStream() {
85 return fTrace;
86 }
87
88 // ------------------------------------------------------------------------
89 // Operators
90 // ------------------------------------------------------------------------
91
92 @Override
93 @SuppressWarnings("unchecked")
94 public TmfContext seekLocation(ITmfLocation<?> location) {
95 try {
96 synchronized(fTrace) {
97 // Position the trace at the requested location and
98 // returns the corresponding context
99 long loc = (location != null) ? ((TmfLocation<Long>) location).getLocation() : 0;
100 if (loc != fTrace.getFilePointer()) {
101 fTrace.seek(loc);
102 }
103 TmfContext context = new TmfContext(getCurrentLocation(), 0);
104 return context;
105 }
106 } catch (IOException e) {
107 e.printStackTrace();
108 }
109 return null;
110 }
111
112 @Override
113 public TmfLocation<Long> getCurrentLocation() {
114 try {
115 return new TmfLocation<Long>(fTrace.getFilePointer());
116 } catch (IOException e) {
117 e.printStackTrace();
118 }
119 return null;
120 }
121
122 @Override
123 public TmfEvent parseEvent(TmfContext context) {
124 try {
125 // paserNextEvent updates the context
126 TmfEvent event = fParser.parseNextEvent(this, context);
127 return event;
128 }
129 catch (IOException e) {
130 e.printStackTrace();
131 }
132 return null;
133 }
134
135 }
This page took 0.033087 seconds and 5 git commands to generate.