Example usage for org.eclipse.jgit.lib ObjectLoader getCachedBytes

List of usage examples for org.eclipse.jgit.lib ObjectLoader getCachedBytes

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectLoader getCachedBytes.

Prototype

public byte[] getCachedBytes(int sizeLimit) throws LargeObjectException, MissingObjectException, IOException 

Source Link

Document

Obtain a reference to the (possibly cached) bytes of this object.

Usage

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * //from ww w . j a  va 2  s .com
 * @return Representative FilestoreModel if valid, otherwise null
 */
public static FilestoreModel getFilestoreItem(ObjectLoader obj) {
    try {
        final byte[] blob = obj.getCachedBytes(com.gitblit.Constants.LEN_FILESTORE_META_MAX);
        final String meta = new String(blob, "UTF-8");

        return FilestoreModel.fromMetaString(meta);

    } catch (LargeObjectException e) {
        //Intentionally failing silent
    } catch (Exception e) {
        error(e, null, "failed to retrieve filestoreItem " + obj.toString());
    }

    return null;
}

From source file:com.google.gerrit.rules.RulesCache.java

License:Apache License

private String read(Project.NameKey project, ObjectId rulesId) throws CompileException {
    try (Repository git = gitMgr.openRepository(project)) {
        try {/* w  ww.j  av  a2 s  .  c  o  m*/
            ObjectLoader ldr = git.open(rulesId, Constants.OBJ_BLOB);
            byte[] raw = ldr.getCachedBytes(SRC_LIMIT);
            return RawParseUtils.decode(raw);
        } catch (LargeObjectException e) {
            throw new CompileException("rules of " + project + " are too large", e);
        } catch (RuntimeException | IOException e) {
            throw new CompileException("Cannot load rules of " + project, e);
        }
    } catch (IOException e) {
        throw new CompileException("Cannot open repository " + project, e);
    }
}

From source file:com.google.gerrit.server.change.FileContentUtil.java

License:Apache License

public BinaryResult getContent(ProjectState project, ObjectId revstr, String path)
        throws ResourceNotFoundException, IOException {
    try (Repository repo = openRepository(project); RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(revstr);
        ObjectReader reader = rw.getObjectReader();
        TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
        if (tw == null) {
            throw new ResourceNotFoundException();
        }//from w w  w .j a  va 2 s  .co  m

        org.eclipse.jgit.lib.FileMode mode = tw.getFileMode(0);
        ObjectId id = tw.getObjectId(0);
        if (mode == org.eclipse.jgit.lib.FileMode.GITLINK) {
            return BinaryResult.create(id.name()).setContentType(X_GIT_GITLINK).base64();
        }

        ObjectLoader obj = repo.open(id, OBJ_BLOB);
        byte[] raw;
        try {
            raw = obj.getCachedBytes(MAX_SIZE);
        } catch (LargeObjectException e) {
            raw = null;
        }

        String type;
        if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
            type = X_GIT_SYMLINK;
        } else {
            type = registry.getMimeType(path, raw).toString();
            type = resolveContentType(project, path, FileMode.FILE, type);
        }

        return asBinaryResult(raw, obj).setContentType(type).base64();
    }
}

From source file:com.google.gerrit.server.change.FileContentUtil.java

License:Apache License

public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable String suffix)
        throws ResourceNotFoundException, IOException {
    suffix = Strings.emptyToNull(CharMatcher.inRange('a', 'z').retainFrom(Strings.nullToEmpty(suffix)));

    try (Repository repo = openRepository(project); RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(revstr);
        ObjectReader reader = rw.getObjectReader();
        TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
        if (tw == null) {
            throw new ResourceNotFoundException();
        }//from w  w  w  .  ja  v  a 2  s .c  o m

        int mode = tw.getFileMode(0).getObjectType();
        if (mode != Constants.OBJ_BLOB) {
            throw new ResourceNotFoundException();
        }

        ObjectId id = tw.getObjectId(0);
        ObjectLoader obj = repo.open(id, OBJ_BLOB);
        byte[] raw;
        try {
            raw = obj.getCachedBytes(MAX_SIZE);
        } catch (LargeObjectException e) {
            raw = null;
        }

        MimeType contentType = registry.getMimeType(path, raw);
        return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix)
                : zipBlob(path, obj, commit, suffix);
    }
}

From source file:com.google.gerrit.server.git.VersionedMetaData.java

License:Apache License

protected byte[] readFile(String fileName) throws IOException {
    if (revision == null) {
        return new byte[] {};
    }//from w ww.  ja v a 2  s  . co m

    TreeWalk tw = TreeWalk.forPath(reader, fileName, revision.getTree());
    if (tw != null) {
        ObjectLoader obj = reader.open(tw.getObjectId(0), Constants.OBJ_BLOB);
        return obj.getCachedBytes(Integer.MAX_VALUE);

    } else {
        return new byte[] {};
    }
}

From source file:com.google.gerrit.server.patch.Text.java

License:Apache License

public static byte[] asByteArray(ObjectLoader ldr)
        throws MissingObjectException, LargeObjectException, IOException {
    return ldr.getCachedBytes(bigFileThreshold);
}

From source file:com.google.gerrit.server.StarredChangesUtil.java

License:Apache License

private static TreeSet<String> readLabels(Repository repo, ObjectId id) throws IOException {
    if (ObjectId.zeroId().equals(id)) {
        return new TreeSet<>();
    }//  w  w  w . j  a  v  a2  s  . c  o m

    try (ObjectReader reader = repo.newObjectReader()) {
        ObjectLoader obj = reader.open(id, Constants.OBJ_BLOB);
        TreeSet<String> labels = new TreeSet<>();
        Iterables.addAll(labels, Splitter.on(CharMatcher.whitespace()).omitEmptyStrings()
                .split(new String(obj.getCachedBytes(Integer.MAX_VALUE), UTF_8)));
        return labels;
    }
}

From source file:com.google.gitiles.BlobSoyData.java

License:Open Source License

public Map<String, Object> toSoyData(String path, ObjectId blobId) throws MissingObjectException, IOException {
    Map<String, Object> data = Maps.newHashMapWithExpectedSize(4);
    data.put("sha", ObjectId.toString(blobId));

    ObjectLoader loader = walk.getObjectReader().open(blobId, Constants.OBJ_BLOB);
    String content;//from w  ww. j  av a2 s  . c o  m
    try {
        byte[] raw = loader.getCachedBytes(MAX_FILE_SIZE);
        content = !RawText.isBinary(raw) ? RawParseUtils.decode(raw) : null;
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        content = null;
    }

    data.put("data", content);
    if (content != null) {
        data.put("lang", guessPrettifyLang(path, content));
    } else if (content == null) {
        data.put("size", Long.toString(loader.getSize()));
    }
    if (path != null && view.getRevision().getPeeledType() == OBJ_COMMIT) {
        data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
    }
    return data;
}

From source file:com.google.gitiles.PathServlet.java

License:Open Source License

private void showSymlink(HttpServletRequest req, HttpServletResponse res, RevWalk rw, TreeWalk tw)
        throws IOException {
    GitilesView view = ViewFilter.getView(req);
    ObjectId id = tw.getObjectId(0);/*from   w w w  .  j  av a 2s  .  c  om*/
    Map<String, Object> data = Maps.newHashMap();

    ObjectLoader loader = rw.getObjectReader().open(id, OBJ_BLOB);
    String target;
    try {
        target = RawParseUtils.decode(loader.getCachedBytes(TreeSoyData.MAX_SYMLINK_SIZE));
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        data.put("sha", ObjectId.toString(id));
        data.put("data", null);
        data.put("size", Long.toString(loader.getSize()));
        render(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getTreePath(),
                "type", FileType.REGULAR_FILE.toString(), "data", data));
        return;
    }

    String url = resolveTargetUrl(
            GitilesView.path().copyFrom(view).setTreePath(dirname(view.getTreePath())).build(), target);
    data.put("title", view.getTreePath());
    data.put("target", target);
    if (url != null) {
        data.put("targetUrl", url);
    }

    // TODO(sop): Allow caching files by SHA-1 when no S cookie is sent.
    render(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getTreePath(),
            "type", FileType.SYMLINK.toString(), "data", data));
}

From source file:com.madgag.agit.diff.LineContextDiffer.java

License:Open Source License

private byte[] open(ObjectReader reader, FileMode mode, AbbreviatedObjectId id) throws IOException {
    if (mode == FileMode.MISSING)
        return new byte[] {};

    if (mode.getObjectType() != Constants.OBJ_BLOB)
        return new byte[] {};

    if (!id.isComplete()) {
        Collection<ObjectId> ids = reader.resolve(id);
        if (ids.size() == 1)
            id = AbbreviatedObjectId.fromObjectId(ids.iterator().next());
        else if (ids.size() == 0)
            throw new MissingObjectException(id, Constants.OBJ_BLOB);
        else/*ww w.  j a v  a  2 s .  co  m*/
            throw new AmbiguousObjectException(id, ids);
    }

    ObjectLoader ldr = reader.open(id.toObjectId());
    return ldr.getCachedBytes(bigFileThreshold);
}