1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.BufferedInputStream ; 14 import java.io.BufferedOutputStream ; 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 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 31 public class FileSystemExporter { 32 private static final int DEFAULT_BUFFER_SIZE = 16*1024; 33 34 40 public void createFolder(IPath destinationPath) { 41 new File (destinationPath.toOSString()).mkdir(); 42 } 43 44 52 public void write(IResource resource, IPath destinationPath) 53 throws CoreException, IOException { 54 if (resource.getType() == IResource.FILE) { 55 writeFile((IFile) resource, destinationPath); 56 } else { 57 writeChildren((IContainer) resource, destinationPath); 58 } 59 } 60 61 64 protected void writeChildren(IContainer folder, IPath destinationPath) 65 throws CoreException, IOException { 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 79 protected void writeFile(IFile file, IPath destinationPath) 80 throws IOException , CoreException { 81 OutputStream output = null; 82 InputStream contentStream = null; 83 84 try { 85 contentStream = new BufferedInputStream (file.getContents(false)); 86 output = new BufferedOutputStream (new FileOutputStream (destinationPath.toOSString())); 87 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 output.write(readBuffer, 0, n); 97 n = contentStream.read(readBuffer); 98 } 99 } finally { 100 if (contentStream != null) { 101 try { 103 contentStream.close(); 104 } 105 catch(IOException e){ 106 IDEWorkbenchPlugin 107 .log( 108 "Error closing input stream for file: " + file.getLocation(), e); } 110 } 111 if (output != null) { 112 output.close(); 114 } 115 } 116 } 117 118 121 protected void writeResource(IResource resource, IPath destinationPath) 122 throws CoreException, IOException { 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 |