1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import java.io.BufferedOutputStream;
47 import java.io.DataOutput;
48 import java.io.IOException;
49 import java.io.OutputStream;
50 import java.security.DigestOutputStream;
51 import java.text.MessageFormat;
52
53 import org.eclipse.jgit.internal.JGitText;
54 import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder.StoredEntry;
55 import org.eclipse.jgit.lib.Constants;
56
57 import com.googlecode.javaewah.EWAHCompressedBitmap;
58
59
60
61
62
63
64 public class PackBitmapIndexWriterV1 {
65 private final DigestOutputStream out;
66 private final DataOutput dataOutput;
67
68
69
70
71
72
73
74 public PackBitmapIndexWriterV1(final OutputStream dst) {
75 out = new DigestOutputStream(dst instanceof BufferedOutputStream ? dst
76 : new BufferedOutputStream(dst),
77 Constants.newMessageDigest());
78 dataOutput = new SimpleDataOutput(out);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public void write(PackBitmapIndexBuilder bitmaps, byte[] packDataChecksum)
97 throws IOException {
98 if (bitmaps == null || packDataChecksum.length != 20)
99 throw new IllegalStateException();
100
101 writeHeader(bitmaps.getOptions(), bitmaps.getBitmapCount(),
102 packDataChecksum);
103 writeBody(bitmaps);
104 writeFooter();
105
106 out.flush();
107 }
108
109 private void writeHeader(
110 int options, int bitmapCount, byte[] packDataChecksum)
111 throws IOException {
112 out.write(PackBitmapIndexV1.MAGIC);
113 dataOutput.writeShort(1);
114 dataOutput.writeShort(options);
115 dataOutput.writeInt(bitmapCount);
116 out.write(packDataChecksum);
117 }
118
119 private void writeBody(PackBitmapIndexBuilder bitmaps) throws IOException {
120 writeBitmap(bitmaps.getCommits());
121 writeBitmap(bitmaps.getTrees());
122 writeBitmap(bitmaps.getBlobs());
123 writeBitmap(bitmaps.getTags());
124 writeBitmaps(bitmaps);
125 }
126
127 private void writeBitmap(EWAHCompressedBitmap bitmap) throws IOException {
128 bitmap.serialize(dataOutput);
129 }
130
131 private void writeBitmaps(PackBitmapIndexBuilder bitmaps)
132 throws IOException {
133 int bitmapCount = 0;
134 for (StoredEntry entry : bitmaps.getCompressedBitmaps()) {
135 writeBitmapEntry(entry);
136 bitmapCount++;
137 }
138
139 int expectedBitmapCount = bitmaps.getBitmapCount();
140 if (expectedBitmapCount != bitmapCount)
141 throw new IOException(MessageFormat.format(
142 JGitText.get().expectedGot,
143 String.valueOf(expectedBitmapCount),
144 String.valueOf(bitmapCount)));
145 }
146
147 private void writeBitmapEntry(StoredEntry entry) throws IOException {
148
149 dataOutput.writeInt((int) entry.getObjectId());
150 out.write(entry.getXorOffset());
151 out.write(entry.getFlags());
152 writeBitmap(entry.getBitmap());
153 }
154
155 private void writeFooter() throws IOException {
156 out.on(false);
157 out.write(out.getMessageDigest().digest());
158 }
159 }