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