mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-10 08:21:21 +00:00
Another version that directly reads BLOB without going through an intermediate object.
This commit is contained in:
@@ -31,6 +31,7 @@ import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.io.Reader;
|
||||
@@ -818,13 +819,30 @@ public class GHRepository extends GHObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the metadata & the content of a blob.
|
||||
*
|
||||
* <p>
|
||||
* This method retrieves the whole content in memory, so beware when you are dealing with large BLOB.
|
||||
*
|
||||
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
|
||||
* @see #readBlob(String)
|
||||
*/
|
||||
public GHBlob getBlob(String blobSha) throws IOException {
|
||||
String target = getApiTailUrl("git/blobs/" + blobSha);
|
||||
return root.retrieve().to(target, GHBlob.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the content of a blob as a stream for better efficiency.
|
||||
*
|
||||
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
|
||||
* @see #getBlob(String)
|
||||
*/
|
||||
public InputStream readBlob(String blobSha) throws IOException {
|
||||
String target = getApiTailUrl("git/blobs/" + blobSha);
|
||||
return root.retrieve().withHeader("Accept","application/vnd.github.VERSION.raw").asStream(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a commit object in this repository.
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.kohsuke.github.GHCommit.File;
|
||||
import org.kohsuke.github.GHOrganization.Permission;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
@@ -903,6 +904,26 @@ public class AppTest extends AbstractGitHubApiTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blob() throws Exception {
|
||||
GHRepository r = gitHub.getRepository("kohsuke/github-api");
|
||||
String sha1 = "a12243f2fc5b8c2ba47dd677d0b0c7583539584d";
|
||||
|
||||
assertBlobContent(r.readBlob(sha1));
|
||||
|
||||
GHBlob blob = r.getBlob(sha1);
|
||||
assertBlobContent(blob.read());
|
||||
assertThat(blob.getSha(),is("a12243f2fc5b8c2ba47dd677d0b0c7583539584d"));
|
||||
assertThat(blob.getSize(),is(1104L));
|
||||
}
|
||||
|
||||
private void assertBlobContent(InputStream is) throws Exception {
|
||||
String content = new String(IOUtils.toByteArray(is),"UTF-8");
|
||||
assertThat(content,containsString("Copyright (c) 2011- Kohsuke Kawaguchi and other contributors"));
|
||||
assertThat(content,containsString("FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR"));
|
||||
assertThat(content.length(),is(1104));
|
||||
}
|
||||
|
||||
private void kohsuke() {
|
||||
String login = getUser().getLogin();
|
||||
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
|
||||
|
||||
Reference in New Issue
Block a user