Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Read content from InputStream in bitbucket api methods

David
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 25, 2018

I'm trying to read a file for a pre-receive hook with the bitbucket api. The point is, I need to check some concrete file and content stuff in order to aprove or reject the commit.  You can see the code here:

    request.getRefChanges().stream()
    .filter(change -> change.getRef().getId().equalsIgnoreCase("refs/heads/master"))
    .forEach(refChange -> {
         scmService.getBulkContentCommandFactory(request.getRepository())                
         .contents(new BulkContentCommandParameters.Builder(refChange.getToHash())
                .sinceCommitId(refChange.getFromHash())
                .build(), new BulkContentCallback() {
                        @Override
                        public void onFile(@Nonnull BulkFile file, @Nonnull InputStream content) {
                            this.processFile(file, content);
                        }
                            private void processFile(BulkFile file, InputStream content) {
                                log.info("Streaming file {} with content id {}, file: {}, size: {}",
                                        file.getPath(), file.getContentId(), file.getSize());
                                try {
                                    StringWriter writer = new StringWriter();
                                    //IOUtils.copy(content, writer, "UTF-8");
                                    log.info("file content: {}", readSB(content));
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    log.error(e.getMessage(), e);
                                }
                            }
                        }).call();});

The problem is, the InputStream I get from the `onFile` method of *BulkContentCallback* is like just giving me the first line of the file. I've tried with different implementations in the readSB method like
**InputStream first implementation**

        StringBuilder textBuilder = new StringBuilder();
        try (Reader reader = new BufferedReader(new InputStreamReader
                  (inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
                    int c = 0;
                    while ((c = reader.read()) != -1) {
                        textBuilder.append((char) c);
                    }
                }
        
        return textBuilder.toString();

Or **InputStream second implementation**:

     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     int nRead;
     byte[] data = new byte[1024];
     while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
        
     buffer.flush();
     byte[] byteArray = buffer.toByteArray();
          
     String text = new String(byteArray, StandardCharsets.UTF_8);
            
     return text;

Or **InputStream third implementation**:

    try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) {
            return buffer.lines().collect(Collectors.joining("\n"))
    }

But the result is always the same, just prints the first line.

Any idea?

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events