Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.osgi; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | public abstract class AbstractResource implements Resource { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 6 | String extra; |
| 7 | byte[] calculated; |
| 8 | long lastModified; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 10 | protected AbstractResource(long modified) { |
| 11 | lastModified = modified; |
| 12 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 14 | public String getExtra() { |
| 15 | return extra; |
| 16 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 18 | public long lastModified() { |
| 19 | return lastModified; |
| 20 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 22 | public InputStream openInputStream() throws IOException { |
| 23 | return new ByteArrayInputStream(getLocalBytes()); |
| 24 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 25 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 26 | private byte[] getLocalBytes() throws IOException { |
| 27 | try { |
| 28 | if (calculated != null) |
| 29 | return calculated; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 31 | return calculated = getBytes(); |
| 32 | } |
| 33 | catch (IOException e) { |
| 34 | throw e; |
| 35 | } |
| 36 | catch (Exception e) { |
| 37 | IOException ee = new IOException("Opening resource"); |
| 38 | ee.initCause(e); |
| 39 | throw ee; |
| 40 | } |
| 41 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 43 | public void setExtra(String extra) { |
| 44 | this.extra = extra; |
| 45 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 47 | public void write(OutputStream out) throws IOException { |
| 48 | out.write(getLocalBytes()); |
| 49 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 51 | abstract protected byte[] getBytes() throws Exception; |
| 52 | |
| 53 | public long size() throws IOException { |
| 54 | return getLocalBytes().length; |
| 55 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | } |