KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > LogWriter


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.indexing;
12
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 class LogWriter {
19
20     protected FileOutputStream JavaDoc out;
21     protected PageStore pageStore;
22
23     /**
24      * Puts the modified pages to the log file.
25      */

26     public static void putModifiedPages(PageStore pageStore, Map JavaDoc modifiedPages) throws PageStoreException {
27         LogWriter writer = new LogWriter();
28         try {
29             writer.open(pageStore);
30             writer.putModifiedPages(modifiedPages);
31         } finally {
32             writer.close();
33         }
34     }
35
36     /**
37      * Opens the log.
38      */

39     protected void open(PageStore store) throws PageStoreException {
40         this.pageStore = store;
41         try {
42             out = new FileOutputStream JavaDoc(Log.name(store.getName()));
43         } catch (IOException JavaDoc e) {
44             throw new PageStoreException(PageStoreException.LogOpenFailure, e);
45         }
46     }
47
48     /**
49      * Closes the log.
50      */

51     protected void close() {
52         try {
53             if (out != null)
54                 out.close();
55         } catch (IOException JavaDoc e) {
56             // ignore
57
}
58         out = null;
59     }
60
61     /**
62      * Puts the modified pages into the log.
63      */

64     protected void putModifiedPages(Map JavaDoc modifiedPages) throws PageStoreException {
65         Buffer b4 = new Buffer(4);
66         byte[] pageBuffer = new byte[Page.SIZE];
67         int numberOfPages = modifiedPages.size();
68         b4.put(0, 4, numberOfPages);
69         try {
70             write(b4.getByteArray());
71             Iterator JavaDoc pageStream = modifiedPages.values().iterator();
72             while (pageStream.hasNext()) {
73                 Page page = (Page) pageStream.next();
74                 int pageNumber = page.getPageNumber();
75                 b4.put(0, 4, pageNumber);
76                 write(b4.getByteArray());
77                 page.toBuffer(pageBuffer);
78                 write(pageBuffer);
79             }
80         } catch (IOException JavaDoc e) {
81             throw new PageStoreException(PageStoreException.LogWriteFailure, e);
82         }
83     }
84
85     public void write(byte[] buffer) throws IOException JavaDoc {
86         out.write(buffer);
87     }
88
89 }
90
Popular Tags