KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > SyncInfoWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.io.DataOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.internal.watson.IPathRequestor;
17 import org.eclipse.core.runtime.QualifiedName;
18
19 public class SyncInfoWriter {
20     protected Synchronizer synchronizer;
21     protected Workspace workspace;
22
23     // version number
24
public static final int SYNCINFO_SAVE_VERSION = 3;
25     public static final int SYNCINFO_SNAP_VERSION = 3;
26
27     // for sync info
28
public static final byte INDEX = 1;
29     public static final byte QNAME = 2;
30
31     public SyncInfoWriter(Workspace workspace, Synchronizer synchronizer) {
32         super();
33         this.workspace = workspace;
34         this.synchronizer = synchronizer;
35     }
36
37     public void savePartners(DataOutputStream JavaDoc output) throws IOException JavaDoc {
38         Set registry = synchronizer.getRegistry();
39         output.writeInt(registry.size());
40         for (Iterator i = registry.iterator(); i.hasNext();) {
41             QualifiedName qname = (QualifiedName) i.next();
42             output.writeUTF(qname.getQualifier());
43             output.writeUTF(qname.getLocalName());
44         }
45     }
46
47     /**
48      * SAVE_FILE -> VERSION_ID RESOURCE+
49      * VERSION_ID -> int
50      * RESOURCE -> RESOURCE_PATH SIZE SYNCINFO*
51      * RESOURCE_PATH -> String
52      * SIZE -> int
53      * SYNCINFO -> TYPE BYTES
54      * TYPE -> INDEX | QNAME
55      * INDEX -> byte int
56      * QNAME -> byte String
57      * BYTES -> byte[]
58      */

59     public void saveSyncInfo(ResourceInfo info, IPathRequestor requestor, DataOutputStream JavaDoc output, List writtenPartners) throws IOException JavaDoc {
60         Map table = info.getSyncInfo(false);
61         if (table == null)
62             return;
63         // if this is the first sync info that we have written, then
64
// write the version id for the file.
65
if (output.size() == 0)
66             output.writeInt(SYNCINFO_SAVE_VERSION);
67         output.writeUTF(requestor.requestPath().toString());
68         output.writeInt(table.size());
69         for (Iterator i = table.entrySet().iterator(); i.hasNext();) {
70             Map.Entry entry = (Map.Entry) i.next();
71             QualifiedName name = (QualifiedName) entry.getKey();
72             // if we have already written the partner name once, then write an integer
73
// constant to represent it instead to remove duplication
74
int index = writtenPartners.indexOf(name);
75             if (index == -1) {
76                 // FIXME: what to do about null qualifier?
77
output.writeByte(QNAME);
78                 output.writeUTF(name.getQualifier());
79                 output.writeUTF(name.getLocalName());
80                 writtenPartners.add(name);
81             } else {
82                 output.writeByte(INDEX);
83                 output.writeInt(index);
84             }
85             byte[] bytes = (byte[]) entry.getValue();
86             output.writeInt(bytes.length);
87             output.write(bytes);
88         }
89     }
90
91     /**
92      * SNAP_FILE -> [VERSION_ID RESOURCE]*
93      * VERSION_ID -> int
94      * RESOURCE -> RESOURCE_PATH SIZE SYNCINFO*
95      * RESOURCE_PATH -> String
96      * SIZE -> int
97      * SYNCINFO -> QNAME BYTES
98      * QNAME -> String String
99      * BYTES -> byte[]
100      */

101     public void snapSyncInfo(ResourceInfo info, IPathRequestor requestor, DataOutputStream JavaDoc output) throws IOException JavaDoc {
102         if (!info.isSet(ICoreConstants.M_SYNCINFO_SNAP_DIRTY))
103             return;
104         Map table = info.getSyncInfo(false);
105         if (table == null)
106             return;
107         // write the version id for the snapshot.
108
output.writeInt(SYNCINFO_SNAP_VERSION);
109         output.writeUTF(requestor.requestPath().toString());
110         output.writeInt(table.size());
111         for (Iterator i = table.entrySet().iterator(); i.hasNext();) {
112             Map.Entry entry = (Map.Entry) i.next();
113             QualifiedName name = (QualifiedName) entry.getKey();
114             output.writeUTF(name.getQualifier());
115             output.writeUTF(name.getLocalName());
116             byte[] bytes = (byte[]) entry.getValue();
117             output.writeInt(bytes.length);
118             output.write(bytes);
119         }
120         info.clear(ICoreConstants.M_SYNCINFO_SNAP_DIRTY);
121     }
122 }
123
Popular Tags