1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.synchronization
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.FileNotFoundException
;
18 import java
.io
.FileOutputStream
;
19 import java
.io
.IOException
;
20 import java
.io
.ObjectInputStream
;
21 import java
.io
.ObjectOutputStream
;
22 import java
.nio
.ByteBuffer
;
23 import java
.nio
.channels
.FileChannel
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
28 * Class to fetch and save synchronization information between traces
30 * @author Geneviève Bastien
33 public class SynchronizationBackend
{
35 private static final int SYNC_FILE_MAGIC_NUMBER
= 0x0DECAF00;
37 private static final int FILE_VERSION
= 1;
39 private static final int HEADER_SIZE
= 20;
41 private final File fSyncFile
;
47 * The file containing synchronization information
49 * If the file couldn't be opened for some reason
51 public SynchronizationBackend(File syncFile
) throws IOException
{
56 * Constructor with possibility to tell whether to throw errors on exception
60 * The file containing synchronization information
62 * Whether to throw exceptions or not
64 * If the file couldn't be opened for some reason
66 public SynchronizationBackend(File syncFile
, boolean throwErrors
) throws IOException
{
68 * Open the file ourselves, get the header information we need, then
69 * pass on the descriptor.
75 if (syncFile
== null) {
79 if (!syncFile
.exists()) {
81 throw new IOException("Selected synchronization file does not exist"); //$NON-NLS-1$
85 if (syncFile
.length() <= 0) {
87 throw new IOException("Invalid synchronization file selected, " + //$NON-NLS-1$
88 "target file is empty"); //$NON-NLS-1$
93 try (FileInputStream fis
= new FileInputStream(syncFile
);
94 FileChannel fc
= fis
.getChannel();) {
95 ByteBuffer buffer
= ByteBuffer
.allocate(HEADER_SIZE
);
101 * Check the magic number,to make sure we're opening the right type
104 res
= buffer
.getInt();
105 if (res
!= SYNC_FILE_MAGIC_NUMBER
) {
106 throw new IOException("Selected file does not" + //$NON-NLS-1$
107 "look like a synchronization file"); //$NON-NLS-1$
110 res
= buffer
.getInt(); /* Major version number */
111 if (res
!= FILE_VERSION
) {
112 throw new IOException("Select synchronization file is of an older " //$NON-NLS-1$
113 + "format. Synchronization will have to be computed again."); //$NON-NLS-1$
116 res
= buffer
.getInt(); /* Minor version number */
121 * Opens an existing synchronization file
123 * @return The synchronization algorithm contained in the file
124 * @throws IOException
125 * Exception returned file functions
127 public SynchronizationAlgorithm
openExistingSync() throws IOException
{
129 if (fSyncFile
== null) {
133 try (/* Set the position after the header */
134 FileInputStream fis
= new FileInputStream(fSyncFile
);
135 FileChannel fc
= fis
.getChannel().position(HEADER_SIZE
);
136 /* Read the input stream */
137 ObjectInputStream ois
= new ObjectInputStream(fis
);) {
139 return (SynchronizationAlgorithm
) ois
.readObject();
140 } catch (ClassNotFoundException e
) {
148 * Saves the synchronization algorithm object to file
151 * The algorithm to save
152 * @throws FileNotFoundException
153 * propagate callee's exceptions
155 public void saveSync(SynchronizationAlgorithm syncAlgo
) throws FileNotFoundException
{
157 if (fSyncFile
== null) {
161 /* Save the header of the file */
162 try (FileOutputStream fos
= new FileOutputStream(fSyncFile
, false);
163 FileChannel fc
= fos
.getChannel();) {
165 ByteBuffer buffer
= ByteBuffer
.allocate(HEADER_SIZE
);
170 buffer
.putInt(SYNC_FILE_MAGIC_NUMBER
);
172 buffer
.putInt(FILE_VERSION
);
175 int res
= fc
.write(buffer
);
176 assert (res
<= HEADER_SIZE
);
177 /* done writing the file header */
179 fc
.position(HEADER_SIZE
);
181 try (ObjectOutputStream oos
= new ObjectOutputStream(fos
);) {
182 oos
.writeObject(syncAlgo
);
185 } catch (FileNotFoundException e
) {
186 /* Send this upwards */
188 } catch (IOException e
) {
189 /* Handle other cases of IOException's */
190 Activator
.logError("Error saving trace synchronization data", e
); //$NON-NLS-1$