Example usage for org.eclipse.jgit.util TemporaryBuffer.Heap toByteArray

List of usage examples for org.eclipse.jgit.util TemporaryBuffer.Heap toByteArray

Introduction

In this page you can find the example usage for org.eclipse.jgit.util TemporaryBuffer.Heap toByteArray.

Prototype

public byte[] toByteArray() throws IOException 

Source Link

Document

Convert this buffer's contents into a contiguous byte array.

Usage

From source file:com.google.gerrit.server.mail.ChangeEmail.java

License:Apache License

/** Show patch set as unified difference. */
public String getUnifiedDiff() {
    PatchList patchList;// ww w . ja v a  2  s . c o m
    try {
        patchList = getPatchList();
        if (patchList.getOldId() == null) {
            // Octopus merges are not well supported for diff output by Gerrit.
            // Currently these always have a null oldId in the PatchList.
            return "[Octopus merge; cannot be formatted as a diff.]\n";
        }
    } catch (PatchListNotAvailableException e) {
        log.error("Cannot format patch", e);
        return "";
    }

    int maxSize = args.settings.maximumDiffSize;
    TemporaryBuffer.Heap buf = new TemporaryBuffer.Heap(Math.min(HEAP_EST_SIZE, maxSize), maxSize);
    try (DiffFormatter fmt = new DiffFormatter(buf)) {
        try (Repository git = args.server.openRepository(change.getProject())) {
            try {
                fmt.setRepository(git);
                fmt.setDetectRenames(true);
                fmt.format(patchList.getOldId(), patchList.getNewId());
                return RawParseUtils.decode(buf.toByteArray());
            } catch (IOException e) {
                if (JGitText.get().inMemoryBufferLimitExceeded.equals(e.getMessage())) {
                    return "";
                }
                log.error("Cannot format patch", e);
                return "";
            }
        } catch (IOException e) {
            log.error("Cannot open repository to format patch", e);
            return "";
        }
    }
}

From source file:com.google.gerrit.server.mail.NewChangeSender.java

License:Apache License

/** Show patch set as unified difference.  */
public String getUnifiedDiff() {
    PatchList patchList;/*  ww  w  . j  a v  a 2  s  .  co m*/
    try {
        patchList = getPatchList();
        if (patchList.getOldId() == null) {
            // Octopus merges are not well supported for diff output by Gerrit.
            // Currently these always have a null oldId in the PatchList.
            return "";
        }
    } catch (PatchListNotAvailableException e) {
        log.error("Cannot format patch", e);
        return "";
    }

    TemporaryBuffer.Heap buf = new TemporaryBuffer.Heap(args.settings.maximumDiffSize);
    DiffFormatter fmt = new DiffFormatter(buf);
    Repository git;
    try {
        git = args.server.openRepository(change.getProject());
    } catch (IOException e) {
        log.error("Cannot open repository to format patch", e);
        return "";
    }
    try {
        fmt.setRepository(git);
        fmt.setDetectRenames(true);
        fmt.format(patchList.getOldId(), patchList.getNewId());
        return RawParseUtils.decode(buf.toByteArray());
    } catch (IOException e) {
        if (JGitText.get().inMemoryBufferLimitExceeded.equals(e.getMessage())) {
            return "";
        }
        log.error("Cannot format patch", e);
        return "";
    } finally {
        fmt.release();
        git.close();
    }
}