1 /*******************************************************************************
2 * Copyright (c) 2013 É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
.linuxtools
.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
.linuxtools
.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 FileInputStream fis
= new FileInputStream(syncFile
);
94 ByteBuffer buffer
= ByteBuffer
.allocate(HEADER_SIZE
);
95 FileChannel fc
= fis
.getChannel();
101 * Check the magic number,to make sure we're opening the right type of
104 res
= buffer
.getInt();
105 if (res
!= SYNC_FILE_MAGIC_NUMBER
) {
108 throw new IOException("Selected file does not" + //$NON-NLS-1$
109 "look like a synchronization file"); //$NON-NLS-1$
112 res
= buffer
.getInt(); /* Major version number */
113 if (res
!= FILE_VERSION
) {
116 throw new IOException("Select synchronization file is of an older " //$NON-NLS-1$
117 + "format. Synchronization will have to be computed again."); //$NON-NLS-1$
120 res
= buffer
.getInt(); /* Minor version number */
127 * Opens an existing synchronization file
129 * @return The synchronization algorithm contained in the file
130 * @throws IOException
131 * Exception returned file functions
133 public SynchronizationAlgorithm
openExistingSync() throws IOException
{
135 if (fSyncFile
== null) {
139 /* Set the position after the header */
140 FileInputStream fis
= new FileInputStream(fSyncFile
);
141 FileChannel fc
= fis
.getChannel().position(HEADER_SIZE
);
143 /* Read the input stream */
144 ObjectInputStream ois
= new ObjectInputStream(fis
);
145 SyncAlgorithmFullyIncremental syncAlgo
= null;
147 syncAlgo
= (SyncAlgorithmFullyIncremental
) ois
.readObject();
148 } catch (ClassNotFoundException e
) {
160 * Saves the synchronization algorithm object to file
163 * The algorithm to save
164 * @throws FileNotFoundException
165 * propagate callee's exceptions
167 public void saveSync(SynchronizationAlgorithm syncAlgo
) throws FileNotFoundException
{
169 if (fSyncFile
== null) {
174 FileOutputStream fos
;
175 ObjectOutputStream oos
;
179 fos
= new FileOutputStream(fSyncFile
, false);
180 fc
= fos
.getChannel();
182 buffer
= ByteBuffer
.allocate(HEADER_SIZE
);
185 /* Save the header of the file */
189 buffer
.putInt(SYNC_FILE_MAGIC_NUMBER
);
191 buffer
.putInt(FILE_VERSION
);
194 res
= fc
.write(buffer
);
195 assert (res
<= HEADER_SIZE
);
196 /* done writing the file header */
198 fc
.position(HEADER_SIZE
);
200 oos
= new ObjectOutputStream(fos
);
201 oos
.writeObject(syncAlgo
);
204 } catch (IOException e
) {
205 /* We should not have any problems at this point... */
206 Activator
.logError("Error saving trace synchronization data", e
); //$NON-NLS-1$
211 } catch (IOException e
) {
212 Activator
.logError("Error closing synchronization file", e
); //$NON-NLS-1$