Skip to content

Commit

Permalink
Added a method to retrieve the actual bytes of BLOB
Browse files Browse the repository at this point in the history
... which is probably more useful than the getContent() method
  • Loading branch information
kohsuke committed Dec 17, 2016
1 parent 0780e10 commit 6380cf9
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/main/java/org/kohsuke/github/GHBlob.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
package org.kohsuke.github;

import org.apache.commons.codec.binary.Base64InputStream;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;

/**
* @author Kanstantsin Shautsou
* @author Kohsuke Kawaguchi
* @see GHRepository#getBlob(String)
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
*/
public class GHBlob {
private String content, encoding, url, sha;
private long size;

public String getEncoding() {
return encoding;
}

public String getUrl() {
return url;
/**
* API URL of this blob.
*/
public URL getUrl() {
return GitHub.parseURL(url);
}

public String getSha() {
return sha;
}

/**
* Number of bytes in this blob.
*/
public long getSize() {
return size;
}

public String getEncoding() {
return encoding;
}

/**
* Encoded content. You probably want {@link #read()}
*/
public String getContent() {
return content;
}

/**
* Retrieves the actual bytes of the blob.
*/
public InputStream read() {
if (encoding.equals("base64")) {
try {
return new Base64InputStream(new ByteArrayInputStream(content.getBytes("US-ASCII")), false);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // US-ASCII is mandatory
}
}

throw new UnsupportedOperationException("Unrecognized encoding: "+encoding);
}
}

0 comments on commit 6380cf9

Please sign in to comment.