KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > FileSystemExporter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.internal.wizards.datatransfer;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.BufferedOutputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
27
28 /**
29  * Helper class for exporting resources to the file system.
30  */

31 public class FileSystemExporter {
32     private static final int DEFAULT_BUFFER_SIZE = 16*1024;
33     
34     /**
35      * Creates the specified file system directory at <code>destinationPath</code>.
36      * This creates a new file system directory.
37      *
38      * @param destinationPath location to which files will be written
39      */

40     public void createFolder(IPath destinationPath) {
41         new File JavaDoc(destinationPath.toOSString()).mkdir();
42     }
43
44     /**
45      * Writes the passed resource to the specified location recursively.
46      *
47      * @param resource the resource to write out to the file system
48      * @param destinationPath location where the resource will be written
49      * @exception CoreException if the operation fails
50      * @exception IOException if an I/O error occurs when writing files
51      */

52     public void write(IResource resource, IPath destinationPath)
53             throws CoreException, IOException JavaDoc {
54         if (resource.getType() == IResource.FILE) {
55             writeFile((IFile) resource, destinationPath);
56         } else {
57             writeChildren((IContainer) resource, destinationPath);
58         }
59     }
60
61     /**
62      * Exports the passed container's children
63      */

64     protected void writeChildren(IContainer folder, IPath destinationPath)
65             throws CoreException, IOException JavaDoc {
66         if (folder.isAccessible()) {
67             IResource[] children = folder.members();
68             for (int i = 0; i < children.length; i++) {
69                 IResource child = children[i];
70                 writeResource(child, destinationPath.append(child.getName()));
71             }
72         }
73     }
74
75     /**
76      * Writes the passed file resource to the specified destination on the local
77      * file system
78      */

79     protected void writeFile(IFile file, IPath destinationPath)
80             throws IOException JavaDoc, CoreException {
81         OutputStream JavaDoc output = null;
82         InputStream JavaDoc contentStream = null;
83
84         try {
85             contentStream = new BufferedInputStream JavaDoc(file.getContents(false));
86             output = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(destinationPath.toOSString()));
87             // for large files, need to make sure the chunk size can be handled by the VM
88
int available = contentStream.available();
89             available = available <= 0 ? DEFAULT_BUFFER_SIZE : available;
90             int chunkSize = Math.min(DEFAULT_BUFFER_SIZE, available);
91             byte[] readBuffer = new byte[chunkSize];
92             int n = contentStream.read(readBuffer);
93
94             while (n > 0) {
95                 // only write the number of bytes read
96
output.write(readBuffer, 0, n);
97                 n = contentStream.read(readBuffer);
98             }
99         } finally {
100             if (contentStream != null) {
101                 // wrap in a try-catch to ensure attempt to close output stream
102
try {
103                     contentStream.close();
104                 }
105                 catch(IOException JavaDoc e){
106                     IDEWorkbenchPlugin
107                             .log(
108                                     "Error closing input stream for file: " + file.getLocation(), e); //$NON-NLS-1$
109
}
110             }
111             if (output != null) {
112                 // propogate this error to the user
113
output.close();
114             }
115         }
116     }
117
118     /**
119      * Writes the passed resource to the specified location recursively
120      */

121     protected void writeResource(IResource resource, IPath destinationPath)
122             throws CoreException, IOException JavaDoc {
123         if (resource.getType() == IResource.FILE) {
124             writeFile((IFile) resource, destinationPath);
125         } else {
126             createFolder(destinationPath);
127             writeChildren((IContainer) resource, destinationPath);
128         }
129     }
130 }
131
Popular Tags