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