KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > ObjectPlusFilesInputStream


1 /* ObjectPlusFilesInputStream
2 *
3 * $Id: ObjectPlusFilesInputStream.java,v 1.3.12.1 2007/01/13 01:31:32 stack-sf Exp $
4 *
5 * Created on Apr 28, 2004
6 *
7 * Copyright (C) 2004 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */

25 package org.archive.io;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33
34 import org.archive.util.FileUtils;
35
36
37 /**
38  * Enhanced ObjectOutputStream with support for restoring
39  * files that had been saved, in parallel with object
40  * serialization.
41  *
42  * @author gojomo
43  *
44  */

45 public class ObjectPlusFilesInputStream extends ObjectInputStream JavaDoc {
46     LinkedList JavaDoc<File JavaDoc> auxiliaryDirectoryStack = new LinkedList JavaDoc<File JavaDoc>();
47     LinkedList JavaDoc<Runnable JavaDoc> postRestoreTasks = new LinkedList JavaDoc<Runnable JavaDoc>();
48
49     /**
50      * Instantiate over the given stream and using the supplied
51      * auxiliary storage directory.
52      *
53      * @param in
54      * @param storeDir
55      * @throws IOException
56      */

57     public ObjectPlusFilesInputStream(InputStream JavaDoc in, File JavaDoc storeDir)
58     throws IOException JavaDoc {
59         super(in);
60         auxiliaryDirectoryStack.addFirst(storeDir);
61     }
62
63     /**
64      * Push another default storage directory for use
65      * until popped.
66      *
67      * @param dir
68      */

69     public void pushAuxiliaryDirectory(String JavaDoc dir) {
70         auxiliaryDirectoryStack.
71             addFirst(new File JavaDoc(getAuxiliaryDirectory(), dir));
72     }
73
74     /**
75      * Discard the top auxiliary directory.
76      */

77     public void popAuxiliaryDirectory() {
78         auxiliaryDirectoryStack.removeFirst();
79     }
80
81     /**
82      * Return the top auxiliary directory, from
83      * which saved files are restored.
84      *
85      * @return Auxillary directory.
86      */

87     public File JavaDoc getAuxiliaryDirectory() {
88         return (File JavaDoc)auxiliaryDirectoryStack.getFirst();
89     }
90
91     /**
92      * Restore a file from storage, using the name and length
93      * info on the serialization stream and the file from the
94      * current auxiliary directory, to the given File.
95      *
96      * @param destination
97      * @throws IOException
98      */

99     public void restoreFile(File JavaDoc destination) throws IOException JavaDoc {
100         String JavaDoc nameAsStored = readUTF();
101         long lengthAtStoreTime = readLong();
102         File JavaDoc storedFile = new File JavaDoc(getAuxiliaryDirectory(),nameAsStored);
103         FileUtils.copyFile(storedFile, destination, lengthAtStoreTime);
104     }
105
106     /**
107      * Restore a file from storage, using the name and length
108      * info on the serialization stream and the file from the
109      * current auxiliary directory, to the given File.
110      *
111      * @param directory
112      * @throws IOException
113      */

114     public void restoreFileTo(File JavaDoc directory) throws IOException JavaDoc {
115         String JavaDoc nameAsStored = readUTF();
116         long lengthAtStoreTime = readLong();
117         File JavaDoc storedFile = new File JavaDoc(getAuxiliaryDirectory(),nameAsStored);
118         File JavaDoc destination = new File JavaDoc(directory,nameAsStored);
119         FileUtils.copyFile(storedFile, destination, lengthAtStoreTime);
120     }
121
122     /**
123      * Register a task to be done when the ObjectPlusFilesInputStream
124      * is closed.
125      *
126      * @param task
127      */

128     public void registerFinishTask(Runnable JavaDoc task) {
129         postRestoreTasks.addFirst(task);
130     }
131
132     private void doFinishTasks() {
133         Iterator JavaDoc iter = postRestoreTasks.iterator();
134         while(iter.hasNext()) {
135             ((Runnable JavaDoc)iter.next()).run();
136         }
137     }
138
139     /**
140      * In addition to default, do any registered cleanup tasks.
141      *
142      * @see java.io.InputStream#close()
143      */

144     public void close() throws IOException JavaDoc {
145         super.close();
146         doFinishTasks();
147     }
148 }
149
Popular Tags