1 17 package org.apache.geronimo.system.repository; 18 19 import java.io.InputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.BufferedOutputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.BufferedInputStream ; 25 26 import org.apache.geronimo.kernel.repository.ArtifactTypeHandler; 27 import org.apache.geronimo.kernel.repository.Artifact; 28 import org.apache.geronimo.kernel.repository.FileWriteMonitor; 29 30 33 public class CopyArtifactTypeHandler implements ArtifactTypeHandler { 34 private final static int TRANSFER_NOTIFICATION_SIZE = 10240; private final static int TRANSFER_BUF_SIZE = 10240; 37 public void install(InputStream source, int size, Artifact artifact, FileWriteMonitor monitor, File target) throws IOException { 38 File parent = target.getParentFile(); 40 if (!parent.exists() && !parent.mkdirs()) { 41 throw new RuntimeException ("Unable to create directory " + parent.getAbsolutePath()); 42 } 43 44 if (monitor != null) { 46 monitor.writeStarted(artifact.toString(), size); 47 } 48 int total = 0; 49 BufferedOutputStream out = null; 50 BufferedInputStream in = null; 51 try { 52 int threshold = TRANSFER_NOTIFICATION_SIZE; 53 out = new BufferedOutputStream (new FileOutputStream (target)); 54 in = new BufferedInputStream (source); 55 byte[] buf = new byte[TRANSFER_BUF_SIZE]; 56 int count; 57 while ((count = in.read(buf)) > -1) { 58 out.write(buf, 0, count); 59 if (monitor != null) { 60 total += count; 61 if (total > threshold) { 62 threshold += TRANSFER_NOTIFICATION_SIZE; 63 monitor.writeProgress(total); 64 } 65 } 66 } 67 out.close(); out = null; 69 in.close(); 70 in = null; 71 } finally { 72 if (out != null) { 73 try { 74 out.close(); 75 } catch (IOException ignored) { 76 } 77 } 78 if (in != null) { 79 try { 80 in.close(); 81 } catch (IOException ignored) { 82 } 83 } 84 if (monitor != null) { 85 monitor.writeComplete(total); 86 } 87 } 88 } 89 } 90 | Popular Tags |