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