blob: 782e615176b52098228bb542e9750e750cc35cb9 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.osgi;
2
3import java.io.*;
4
5public abstract class AbstractResource implements Resource {
Stuart McCulloch2286f232012-06-15 13:27:53 +00006 String extra;
7 byte[] calculated;
8 long lastModified;
Stuart McCullochbb014372012-06-07 21:57:32 +00009
Stuart McCulloch2286f232012-06-15 13:27:53 +000010 protected AbstractResource(long modified) {
11 lastModified = modified;
12 }
Stuart McCullochbb014372012-06-07 21:57:32 +000013
Stuart McCulloch2286f232012-06-15 13:27:53 +000014 public String getExtra() {
15 return extra;
16 }
Stuart McCullochbb014372012-06-07 21:57:32 +000017
Stuart McCulloch2286f232012-06-15 13:27:53 +000018 public long lastModified() {
19 return lastModified;
20 }
Stuart McCullochbb014372012-06-07 21:57:32 +000021
Stuart McCulloch2286f232012-06-15 13:27:53 +000022 public InputStream openInputStream() throws IOException {
23 return new ByteArrayInputStream(getLocalBytes());
24 }
Stuart McCullochbb014372012-06-07 21:57:32 +000025
Stuart McCulloch2286f232012-06-15 13:27:53 +000026 private byte[] getLocalBytes() throws IOException {
27 try {
28 if (calculated != null)
29 return calculated;
Stuart McCullochbb014372012-06-07 21:57:32 +000030
Stuart McCulloch2286f232012-06-15 13:27:53 +000031 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 McCullochbb014372012-06-07 21:57:32 +000042
Stuart McCulloch2286f232012-06-15 13:27:53 +000043 public void setExtra(String extra) {
44 this.extra = extra;
45 }
Stuart McCullochbb014372012-06-07 21:57:32 +000046
Stuart McCulloch2286f232012-06-15 13:27:53 +000047 public void write(OutputStream out) throws IOException {
48 out.write(getLocalBytes());
49 }
Stuart McCullochbb014372012-06-07 21:57:32 +000050
Stuart McCulloch2286f232012-06-15 13:27:53 +000051 abstract protected byte[] getBytes() throws Exception;
52
53 public long size() throws IOException {
54 return getLocalBytes().length;
55 }
Stuart McCullochbb014372012-06-07 21:57:32 +000056}