1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.patch;
13
14 import static org.eclipse.jgit.lib.Constants.encodeASCII;
15 import static org.eclipse.jgit.util.RawParseUtils.match;
16 import static org.eclipse.jgit.util.RawParseUtils.nextLF;
17 import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
18
19
20
21
22 public class BinaryHunk {
23 private static final byte[] LITERAL = encodeASCII("literal ");
24
25 private static final byte[] DELTA = encodeASCII("delta ");
26
27
28 public enum Type {
29
30 LITERAL_DEFLATED,
31
32
33 DELTA_DEFLATED;
34 }
35
36 private final FileHeader file;
37
38
39 final int startOffset;
40
41
42 int endOffset;
43
44
45 private Type type;
46
47
48 private int length;
49
50 BinaryHunk(FileHeader fh, int offset) {
51 file = fh;
52 startOffset = offset;
53 }
54
55
56
57
58
59
60 public FileHeader getFileHeader() {
61 return file;
62 }
63
64
65
66
67
68
69 public byte[] getBuffer() {
70 return file.buf;
71 }
72
73
74
75
76
77
78 public int getStartOffset() {
79 return startOffset;
80 }
81
82
83
84
85
86
87 public int getEndOffset() {
88 return endOffset;
89 }
90
91
92
93
94
95
96 public Type getType() {
97 return type;
98 }
99
100
101
102
103
104
105 public int getSize() {
106 return length;
107 }
108
109 int parseHunk(int ptr, int end) {
110 final byte[] buf = file.buf;
111
112 if (match(buf, ptr, LITERAL) >= 0) {
113 type = Type.LITERAL_DEFLATED;
114 length = parseBase10(buf, ptr + LITERAL.length, null);
115
116 } else if (match(buf, ptr, DELTA) >= 0) {
117 type = Type.DELTA_DEFLATED;
118 length = parseBase10(buf, ptr + DELTA.length, null);
119
120 } else {
121
122
123
124
125 return -1;
126 }
127 ptr = nextLF(buf, ptr);
128
129
130
131
132
133 while (ptr < end) {
134 final boolean empty = buf[ptr] == '\n';
135 ptr = nextLF(buf, ptr);
136 if (empty)
137 break;
138 }
139
140 return ptr;
141 }
142 }