Example usage for org.eclipse.jgit.util RawParseUtils parseEncoding

List of usage examples for org.eclipse.jgit.util RawParseUtils parseEncoding

Introduction

In this page you can find the example usage for org.eclipse.jgit.util RawParseUtils parseEncoding.

Prototype

public static Charset parseEncoding(byte[] b) 

Source Link

Document

Parse the "encoding " header into a character set reference.

Usage

From source file:com.google.gerrit.server.notedb.ChangeNotesParser.java

License:Apache License

private void parseChangeMessage(PatchSet.Id psId, Account.Id accountId, RevCommit commit) {
    byte[] raw = commit.getRawBuffer();
    int size = raw.length;
    Charset enc = RawParseUtils.parseEncoding(raw);

    int subjectStart = RawParseUtils.commitMessage(raw, 0);
    if (subjectStart < 0 || subjectStart >= size) {
        return;//w  w w  .  jav  a  2s.  c  o m
    }

    int subjectEnd = RawParseUtils.endOfParagraph(raw, subjectStart);
    if (subjectEnd == size) {
        return;
    }

    int changeMessageStart;

    if (raw[subjectEnd] == '\n') {
        changeMessageStart = subjectEnd + 2; //\n\n ends paragraph
    } else if (raw[subjectEnd] == '\r') {
        changeMessageStart = subjectEnd + 4; //\r\n\r\n ends paragraph
    } else {
        return;
    }

    int ptr = size - 1;
    int changeMessageEnd = -1;
    while (ptr > changeMessageStart) {
        ptr = RawParseUtils.prevLF(raw, ptr, '\r');
        if (ptr == -1) {
            break;
        }
        if (raw[ptr] == '\n') {
            changeMessageEnd = ptr - 1;
            break;
        } else if (raw[ptr] == '\r') {
            changeMessageEnd = ptr - 3;
            break;
        }
    }

    if (ptr <= changeMessageStart) {
        return;
    }

    String changeMsgString = RawParseUtils.decode(enc, raw, changeMessageStart, changeMessageEnd + 1);
    ChangeMessage changeMessage = new ChangeMessage(new ChangeMessage.Key(psId.getParentKey(), commit.name()),
            accountId, new Timestamp(commit.getCommitterIdent().getWhen().getTime()), psId);
    changeMessage.setMessage(changeMsgString);
    changeMessages.put(psId, changeMessage);
}

From source file:com.google.gerrit.server.notedb.CommentsInNotesUtil.java

License:Apache License

public static List<PatchLineComment> parseNote(byte[] note, Change.Id changeId, Status status)
        throws ConfigInvalidException {
    List<PatchLineComment> result = Lists.newArrayList();
    int sizeOfNote = note.length;
    Charset enc = RawParseUtils.parseEncoding(note);
    MutableInteger curr = new MutableInteger();
    curr.value = 0;//from   w w w. j ava2s  .  c o  m

    boolean isForBase = (RawParseUtils.match(note, curr.value, PATCH_SET.getBytes(UTF_8))) < 0;

    PatchSet.Id psId = parsePsId(note, curr, changeId, enc, isForBase ? BASE_PATCH_SET : PATCH_SET);

    RevId revId = new RevId(parseStringField(note, curr, changeId, enc, REVISION));

    PatchLineComment c = null;
    while (curr.value < sizeOfNote) {
        String previousFileName = c == null ? null : c.getKey().getParentKey().getFileName();
        c = parseComment(note, curr, previousFileName, psId, revId, isForBase, enc, status);
        result.add(c);
    }
    return result;
}

From source file:playRepository.GitCommit.java

License:Apache License

public static Charset parseEncoding(final byte[] b, Charset fallback) {
    try {/* ww w  . ja va2 s.com*/
        return RawParseUtils.parseEncoding(b);
    } catch (UnsupportedCharsetException badName) {
        return fallback;
    }
}

From source file:playRepository.GitCommit.java

License:Apache License

/**
 * Parse a name line (e.g. author, committer, tagger) into a PersonIdent.
 * <p>//  w w  w  .  j  a v  a  2s.  c  o m
 * When passing in a value for <code>nameB</code> callers should use the
 * return value of {@link RawParseUtils#author(byte[], int)} or
 * {@link RawParseUtils#committer(byte[], int)}, as these methods provide the proper
 * position within the buffer.
 *
 * @param raw
 *            the buffer to parse character data from.
 * @param nameB
 *            first position of the identity information. This should be the
 *            first position after the space which delimits the header field
 *            name (e.g. "author" or "committer") from the rest of the
 *            identity line.
 * @return the parsed identity or null in case the identity could not be
 *         parsed.
 */
public static PersonIdent parsePersonIdent(final byte[] raw, final int nameB, Charset fallback) {
    // This line and the third parameter of this method is added by Yi EungJun. 2013/12/10
    final Charset cs = (fallback == null) ? RawParseUtils.parseEncoding(raw) : parseEncoding(raw, fallback);

    final int emailB = RawParseUtils.nextLF(raw, nameB, '<');
    final int emailE = RawParseUtils.nextLF(raw, emailB, '>');
    if (emailB >= raw.length || raw[emailB] == '\n' || (emailE >= raw.length - 1 && raw[emailE - 1] != '>'))
        return null;

    final int nameEnd = emailB - 2 >= nameB && raw[emailB - 2] == ' ' ? emailB - 2 : emailB - 1;
    final String name = RawParseUtils.decode(cs, raw, nameB, nameEnd);
    final String email = RawParseUtils.decode(cs, raw, emailB, emailE - 1);

    // Start searching from end of line, as after first name-email pair,
    // another name-email pair may occur. We will ignore all kinds of
    // "junk" following the first email.
    //
    // We've to use (emailE - 1) for the case that raw[email] is LF,
    // otherwise we would run too far. "-2" is necessary to position
    // before the LF in case of LF termination resp. the penultimate
    // character if there is no trailing LF.
    final int tzBegin = lastIndexOfTrim(raw, ' ', RawParseUtils.nextLF(raw, emailE - 1) - 2) + 1;
    if (tzBegin <= emailE) // No time/zone, still valid
        return new PersonIdent(name, email, 0, 0);

    final int whenBegin = Math.max(emailE, lastIndexOfTrim(raw, ' ', tzBegin - 1) + 1);
    if (whenBegin >= tzBegin - 1) // No time/zone, still valid
        return new PersonIdent(name, email, 0, 0);

    final long when = RawParseUtils.parseLongBase10(raw, whenBegin, null);
    final int tz = RawParseUtils.parseTimeZoneOffset(raw, tzBegin);
    return new PersonIdent(name, email, when * 1000L, tz);
}