Skip to content

Commit e6ee278

Browse files
committed
Another version that directly reads BLOB without going through an intermediate object.
1 parent 6380cf9 commit e6ee278

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.io.FileNotFoundException;
3333
import java.io.IOException;
34+
import java.io.InputStream;
3435
import java.io.InputStreamReader;
3536
import java.io.InterruptedIOException;
3637
import java.io.Reader;
@@ -818,13 +819,30 @@ public GHTree getTreeRecursive(String sha, int recursive) throws IOException {
818819
}
819820

820821
/**
822+
* Obtains the metadata & the content of a blob.
823+
*
824+
* <p>
825+
* This method retrieves the whole content in memory, so beware when you are dealing with large BLOB.
826+
*
821827
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
828+
* @see #readBlob(String)
822829
*/
823830
public GHBlob getBlob(String blobSha) throws IOException {
824831
String target = getApiTailUrl("git/blobs/" + blobSha);
825832
return root.retrieve().to(target, GHBlob.class);
826833
}
827834

835+
/**
836+
* Reads the content of a blob as a stream for better efficiency.
837+
*
838+
* @see <a href="https://developer.github.com/v3/git/blobs/#get-a-blob">Get a blob</a>
839+
* @see #getBlob(String)
840+
*/
841+
public InputStream readBlob(String blobSha) throws IOException {
842+
String target = getApiTailUrl("git/blobs/" + blobSha);
843+
return root.retrieve().withHeader("Accept","application/vnd.github.VERSION.raw").asStream(target);
844+
}
845+
828846
/**
829847
* Gets a commit object in this repository.
830848
*/

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.kohsuke.github.GHOrganization.Permission;
1313

1414
import java.io.IOException;
15+
import java.io.InputStream;
1516
import java.net.URL;
1617
import java.util.*;
1718
import java.util.Map.Entry;
@@ -903,6 +904,26 @@ public void listOrgMemberships() throws Exception {
903904
}
904905
}
905906

907+
@Test
908+
public void blob() throws Exception {
909+
GHRepository r = gitHub.getRepository("kohsuke/github-api");
910+
String sha1 = "a12243f2fc5b8c2ba47dd677d0b0c7583539584d";
911+
912+
assertBlobContent(r.readBlob(sha1));
913+
914+
GHBlob blob = r.getBlob(sha1);
915+
assertBlobContent(blob.read());
916+
assertThat(blob.getSha(),is("a12243f2fc5b8c2ba47dd677d0b0c7583539584d"));
917+
assertThat(blob.getSize(),is(1104L));
918+
}
919+
920+
private void assertBlobContent(InputStream is) throws Exception {
921+
String content = new String(IOUtils.toByteArray(is),"UTF-8");
922+
assertThat(content,containsString("Copyright (c) 2011- Kohsuke Kawaguchi and other contributors"));
923+
assertThat(content,containsString("FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR"));
924+
assertThat(content.length(),is(1104));
925+
}
926+
906927
private void kohsuke() {
907928
String login = getUser().getLogin();
908929
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 commit comments

Comments
 (0)