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 | |
| 5 | public abstract class WriteResource implements Resource { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 6 | String extra; |
| 7 | volatile long size = -1; |
| 8 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | public InputStream openInputStream() throws Exception { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 10 | PipedInputStream pin = new PipedInputStream(); |
| 11 | final PipedOutputStream pout = new PipedOutputStream(pin); |
| 12 | Thread t = new Thread() { |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 13 | @Override |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | public void run() { |
| 15 | try { |
| 16 | write(pout); |
| 17 | pout.flush(); |
| 18 | } |
| 19 | catch (Exception e) { |
| 20 | e.printStackTrace(); |
| 21 | } |
| 22 | finally { |
| 23 | try { |
| 24 | pout.close(); |
| 25 | } |
| 26 | catch (IOException e) { |
| 27 | // Ignore |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | }; |
| 32 | t.start(); |
| 33 | return pin; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | public abstract void write(OutputStream out) throws IOException, Exception; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 37 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | public abstract long lastModified(); |
| 39 | |
| 40 | public String getExtra() { |
| 41 | return extra; |
| 42 | } |
| 43 | |
| 44 | public void setExtra(String extra) { |
| 45 | this.extra = extra; |
| 46 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 48 | static class CountingOutputStream extends OutputStream { |
| 49 | long size; |
| 50 | |
| 51 | @Override |
| 52 | public void write(int var0) throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 53 | size++; |
| 54 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public void write(byte[] buffer) throws IOException { |
| 58 | size += buffer.length; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 59 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 60 | |
| 61 | @Override |
| 62 | public void write(byte[] buffer, int start, int length) throws IOException { |
| 63 | size += length; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 66 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 67 | public long size() throws IOException, Exception { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | if (size == -1) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | CountingOutputStream cout = new CountingOutputStream(); |
| 70 | write(cout); |
| 71 | size = cout.size; |
| 72 | } |
| 73 | return size; |
| 74 | } |
| 75 | } |