Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.regex.*; |
| 5 | |
| 6 | public class FileResource implements Resource { |
| 7 | File file; |
| 8 | String extra; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 9 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | public FileResource(File file) { |
| 11 | this.file = file; |
| 12 | } |
| 13 | |
| 14 | public InputStream openInputStream() throws FileNotFoundException { |
| 15 | return new FileInputStream(file); |
| 16 | } |
| 17 | |
| 18 | public static void build(Jar jar, File directory, Pattern doNotCopy) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 19 | traverse(jar, directory.getAbsolutePath().length(), directory, doNotCopy); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | public String toString() { |
| 23 | return ":" + file.getName() + ":"; |
| 24 | } |
| 25 | |
| 26 | public void write(OutputStream out) throws Exception { |
| 27 | copy(this, out); |
| 28 | } |
| 29 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | static synchronized void copy(Resource resource, OutputStream out) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | InputStream in = resource.openInputStream(); |
| 32 | try { |
| 33 | byte buffer[] = new byte[20000]; |
| 34 | int size = in.read(buffer); |
| 35 | while (size > 0) { |
| 36 | out.write(buffer, 0, size); |
| 37 | size = in.read(buffer); |
| 38 | } |
| 39 | } |
| 40 | finally { |
| 41 | in.close(); |
| 42 | } |
| 43 | } |
| 44 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 45 | static void traverse(Jar jar, int rootlength, File directory, Pattern doNotCopy) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | if (doNotCopy != null && doNotCopy.matcher(directory.getName()).matches()) |
| 47 | return; |
| 48 | jar.updateModified(directory.lastModified(), "Dir change"); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 49 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | File files[] = directory.listFiles(); |
| 51 | for (int i = 0; i < files.length; i++) { |
| 52 | if (files[i].isDirectory()) |
| 53 | traverse(jar, rootlength, files[i], doNotCopy); |
| 54 | else { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 55 | String path = files[i].getAbsolutePath().substring(rootlength + 1); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | if (File.separatorChar != '/') |
| 57 | path = path.replace(File.separatorChar, '/'); |
| 58 | jar.putResource(path, new FileResource(files[i]), true); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public long lastModified() { |
| 64 | return file.lastModified(); |
| 65 | } |
| 66 | |
| 67 | public String getExtra() { |
| 68 | return extra; |
| 69 | } |
| 70 | |
| 71 | public void setExtra(String extra) { |
| 72 | this.extra = extra; |
| 73 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 74 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 75 | public long size() { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 76 | return (int) file.length(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 77 | } |
| 78 | } |