1 16 package org.apache.commons.vfs; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 22 27 public class FileUtil 28 { 29 private FileUtil() 30 { 31 } 32 33 38 public static byte[] getContent(final FileObject file) 39 throws IOException 40 { 41 final FileContent content = file.getContent(); 42 final int size = (int) content.getSize(); 43 final byte[] buf = new byte[size]; 44 45 final InputStream in = content.getInputStream(); 46 try 47 { 48 int read = 0; 49 for (int pos = 0; pos < size && read >= 0; pos += read) 50 { 51 read = in.read(buf, pos, size - pos); 52 } 53 } 54 finally 55 { 56 in.close(); 57 } 58 59 return buf; 60 } 61 62 65 public static void writeContent(final FileObject file, 66 final OutputStream outstr) 67 throws IOException 68 { 69 final InputStream instr = file.getContent().getInputStream(); 70 try 71 { 72 final byte[] buffer = new byte[1024]; 73 while (true) 74 { 75 final int nread = instr.read(buffer); 76 if (nread < 0) 77 { 78 break; 79 } 80 outstr.write(buffer, 0, nread); 81 } 82 } 83 finally 84 { 85 instr.close(); 86 } 87 } 88 89 92 public static void copyContent(final FileObject srcFile, 93 final FileObject destFile) 94 throws IOException 95 { 96 final OutputStream outstr = destFile.getContent().getOutputStream(); 99 try 100 { 101 writeContent(srcFile, outstr); 102 } 103 finally 104 { 105 outstr.close(); 106 } 107 } 108 109 } 110 | Popular Tags |