]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / BundleDocumentationSource.java
1 package org.simantics.scl.osgi.internal;\r
2 \r
3 import java.io.IOException;\r
4 import java.io.InputStream;\r
5 import java.net.URL;\r
6 import java.nio.charset.Charset;\r
7 import java.util.Arrays;\r
8 \r
9 import org.osgi.framework.Bundle;\r
10 \r
11 public class BundleDocumentationSource {\r
12     public static final Charset UTF8 = Charset.forName("UTF-8");  \r
13     \r
14     public final String documentationName;\r
15     public final Bundle bundle;\r
16     public final URL url;\r
17     \r
18     public BundleDocumentationSource(String documentationName, Bundle bundle,\r
19             URL url) {\r
20         this.documentationName = documentationName;\r
21         this.bundle = bundle;\r
22         this.url = url;\r
23     }\r
24 \r
25     public String getText() {\r
26         try {\r
27             InputStream stream = url.openStream();\r
28             try {\r
29                 byte[] buffer = new byte[1024];\r
30                 int pos = 0;\r
31                 while(true) {\r
32                     int count = stream.read(buffer, pos, buffer.length-pos);\r
33                     if(count <= 0)\r
34                         break;\r
35                     pos += count;\r
36                     if(pos == buffer.length)\r
37                         buffer = Arrays.copyOf(buffer, buffer.length*2);\r
38                 }\r
39                 return new String(buffer, 0, pos, UTF8);\r
40             } finally {\r
41                 stream.close();\r
42             }\r
43         } catch(IOException e) {\r
44             e.printStackTrace();\r
45             return null;\r
46         }\r
47     }\r
48 }\r