Skip to content

Commit 6380cf9

Browse files
committed
Added a method to retrieve the actual bytes of BLOB
... which is probably more useful than the getContent() method
1 parent 0780e10 commit 6380cf9

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,63 @@
11
package org.kohsuke.github;
22

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+
310
/**
411
* @author Kanstantsin Shautsou
12+
* @author Kohsuke Kawaguchi
13+
* @see GHRepository#getBlob(String)
514
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
615
*/
716
public class GHBlob {
817
private String content, encoding, url, sha;
918
private long size;
1019

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);
1725
}
1826

1927
public String getSha() {
2028
return sha;
2129
}
2230

31+
/**
32+
* Number of bytes in this blob.
33+
*/
2334
public long getSize() {
2435
return size;
2536
}
2637

38+
public String getEncoding() {
39+
return encoding;
40+
}
41+
42+
/**
43+
* Encoded content. You probably want {@link #read()}
44+
*/
2745
public String getContent() {
2846
return content;
2947
}
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+
}
3063
}

0 commit comments

Comments
 (0)