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