|
| 1 | +package org.kohsuke.github; |
| 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 | + |
| 10 | +/** |
| 11 | + * @author Kanstantsin Shautsou |
| 12 | + * @author Kohsuke Kawaguchi |
| 13 | + * @see GHRepository#getBlob(String) |
| 14 | + * @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a> |
| 15 | + */ |
| 16 | +public class GHBlob { |
| 17 | + private String content, encoding, url, sha; |
| 18 | + private long size; |
| 19 | + |
| 20 | + /** |
| 21 | + * API URL of this blob. |
| 22 | + */ |
| 23 | + public URL getUrl() { |
| 24 | + return GitHub.parseURL(url); |
| 25 | + } |
| 26 | + |
| 27 | + public String getSha() { |
| 28 | + return sha; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Number of bytes in this blob. |
| 33 | + */ |
| 34 | + public long getSize() { |
| 35 | + return size; |
| 36 | + } |
| 37 | + |
| 38 | + public String getEncoding() { |
| 39 | + return encoding; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Encoded content. You probably want {@link #read()} |
| 44 | + */ |
| 45 | + public String getContent() { |
| 46 | + return content; |
| 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 | + } |
| 63 | +} |
0 commit comments