+package org.simantics.scl.osgi.internal;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URL;\r
+import java.nio.charset.Charset;\r
+import java.util.Arrays;\r
+\r
+import org.osgi.framework.Bundle;\r
+\r
+public class BundleDocumentationSource {\r
+ public static final Charset UTF8 = Charset.forName("UTF-8"); \r
+ \r
+ public final String documentationName;\r
+ public final Bundle bundle;\r
+ public final URL url;\r
+ \r
+ public BundleDocumentationSource(String documentationName, Bundle bundle,\r
+ URL url) {\r
+ this.documentationName = documentationName;\r
+ this.bundle = bundle;\r
+ this.url = url;\r
+ }\r
+\r
+ public String getText() {\r
+ try {\r
+ InputStream stream = url.openStream();\r
+ try {\r
+ byte[] buffer = new byte[1024];\r
+ int pos = 0;\r
+ while(true) {\r
+ int count = stream.read(buffer, pos, buffer.length-pos);\r
+ if(count <= 0)\r
+ break;\r
+ pos += count;\r
+ if(pos == buffer.length)\r
+ buffer = Arrays.copyOf(buffer, buffer.length*2);\r
+ }\r
+ return new String(buffer, 0, pos, UTF8);\r
+ } finally {\r
+ stream.close();\r
+ }\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ return null;\r
+ }\r
+ }\r
+}\r