|
1 | 1 | package org.kohsuke.github; |
2 | 2 |
|
| 3 | +import org.apache.commons.codec.binary.Base64InputStream; |
| 4 | + |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | +import java.net.URL; |
| 9 | + |
3 | 10 | /** |
4 | 11 | * @author Kanstantsin Shautsou |
| 12 | + * @author Kohsuke Kawaguchi |
| 13 | + * @see GHRepository#getBlob(String) |
5 | 14 | * @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a> |
6 | 15 | */ |
7 | 16 | public class GHBlob { |
8 | 17 | private String content, encoding, url, sha; |
9 | 18 | private long size; |
10 | 19 |
|
11 | | - public String getEncoding() { |
12 | | - return encoding; |
13 | | - } |
14 | | - |
15 | | - public String getUrl() { |
16 | | - return url; |
| 20 | + /** |
| 21 | + * API URL of this blob. |
| 22 | + */ |
| 23 | + public URL getUrl() { |
| 24 | + return GitHub.parseURL(url); |
17 | 25 | } |
18 | 26 |
|
19 | 27 | public String getSha() { |
20 | 28 | return sha; |
21 | 29 | } |
22 | 30 |
|
| 31 | + /** |
| 32 | + * Number of bytes in this blob. |
| 33 | + */ |
23 | 34 | public long getSize() { |
24 | 35 | return size; |
25 | 36 | } |
26 | 37 |
|
| 38 | + public String getEncoding() { |
| 39 | + return encoding; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Encoded content. You probably want {@link #read()} |
| 44 | + */ |
27 | 45 | public String getContent() { |
28 | 46 | return content; |
29 | 47 | } |
| 48 | + |
| 49 | + /** |
| 50 | + * Retrieves the actual bytes of the blob. |
| 51 | + */ |
| 52 | + public InputStream read() { |
| 53 | + if (encoding.equals("base64")) { |
| 54 | + try { |
| 55 | + return new Base64InputStream(new ByteArrayInputStream(content.getBytes("US-ASCII")), false); |
| 56 | + } catch (UnsupportedEncodingException e) { |
| 57 | + throw new AssertionError(e); // US-ASCII is mandatory |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + throw new UnsupportedOperationException("Unrecognized encoding: "+encoding); |
| 62 | + } |
30 | 63 | } |
0 commit comments