1 17 18 package org.apache.lenya.cms.cocoon.source; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 24 import org.apache.commons.io.CopyUtils; 25 import org.apache.commons.io.output.ByteArrayOutputStream; 26 import org.apache.excalibur.source.ModifiableSource; 27 import org.apache.excalibur.source.Source; 28 import org.apache.excalibur.source.SourceException; 29 import org.apache.excalibur.source.SourceResolver; 30 31 34 public final class SourceUtil { 35 36 47 public static void copy(Source source, ModifiableSource destination, boolean useBuffer) 48 throws IOException { 49 InputStream sourceInputStream = null; 50 OutputStream destOutputStream = null; 51 try { 52 sourceInputStream = source.getInputStream(); 53 destOutputStream = destination.getOutputStream(); 54 if(useBuffer) { 55 final ByteArrayOutputStream sourceBos = new ByteArrayOutputStream(); 56 CopyUtils.copy(sourceInputStream, sourceBos); 57 CopyUtils.copy(sourceBos.toByteArray(), destOutputStream); 58 } 59 else 60 CopyUtils.copy(sourceInputStream, destOutputStream); 61 } finally { 62 if(destOutputStream != null) { 63 destOutputStream.flush(); 64 destOutputStream.close(); 65 } 66 if(sourceInputStream != null) { 67 sourceInputStream.close(); 68 } 69 } 70 } 71 72 83 public static void copy(SourceResolver resolver, String sourceUri, String destUri, boolean useBuffer) 84 throws IOException , SourceException { 85 Source source = null; 86 Source dest = null; 87 try { 88 source = resolver.resolveURI(sourceUri); 89 dest = resolver.resolveURI(destUri); 90 91 if(!(dest instanceof ModifiableSource)) 92 throw new SourceException("Destination '"+ dest.getURI() + "' is not modifiable."); 93 94 copy(source, (ModifiableSource) dest, useBuffer); 95 } 96 finally { 97 if(source != null) 98 resolver.release(source); 99 if(dest != null) 100 resolver.release(dest); 101 } 102 } 103 104 112 public static void copy(SourceResolver resolver, String sourceUri, String destUri) 113 throws IOException { 114 copy(resolver, sourceUri, destUri, false); 115 } 116 } 117 | Popular Tags |